最近一直在看工作方面的书籍,把论文的事情搁置了,之前承诺的贴代码的事一直拖。现在把代码整理发上来,只有核心部分的,都不是我写的,我是网上整理下载的,matlab代码的效果比较差。

全部文件网盘下载地址:http://pan.baidu.com/s/1qWwNMfM;

1.C++代码

下载地址:

需要先安装opencv和boost库。

boost库下载地址:http://www.boost.org/users/download/

boost的安装:http://www.cnblogs.com/pangxiaodong/archive/2011/05/05/2037006.html

装这个boost库,我只是把文件复制到vs安装目录的include文件夹下。

GitHub repository:https://github.com/aperrau/DetectText

2.matlab代码
function [ swtMap ] = swt( im, searchDirection )
%swt Preforms stoke width transform on input image
% A novel image operator that seeks to find the value of stroke width
% for each image pixel. It's use is meant for the task of text
% detection in natural images.
%
% im = RGB input image of size m x n x
% searchDirection = gradient direction is either to detect dark text on light
% background or - to detect light text on dark background.
%
% swtMap = resulting mapping of stroke withs for image pixels % Convert image to gray scale
im = im2double(rgb2gray(im));
%figure, imshow(im), title('Black and White Image'); % Find edges using canny edge dector
edgeMap = edge(im, 'canny');
%figure, imshow(edgeMap), title('Edges Using Canny'); % Get all edge pixel postitions
[edgePointRows, edgePointCols] = find(edgeMap); % Find gradient horizontal and vertical gradient
sobelMask = fspecial('sobel');
dx = imfilter(im,sobelMask);
dy = imfilter(im,sobelMask');
%figure, imshow(dx, []), title('Horizontal Gradient Image');
%figure, imshow(dy, []), title('Vertical Gradient Image'); % Initializing matrix of gradient direction
theta = zeros(size(edgeMap,),size(edgeMap,)); % Calculating theta, gradient direction, for each pixel on the image.
% ***This can be optimized by using edgePointCols and edgePointRows
% instead.***
for i=:size(edgeMap,)
for j=:size(edgeMap,)
if edgeMap(i,j) ==
theta(i,j) = atan2(dy(i,j),dx(i,j));
end
end
end % Getting size of the image
[m,n] = size(edgeMap); % Initializing Stoke Width array with infinity
swtMap = zeros(m,n);
for i=:m
for j=:n
swtMap(i,j) = inf;
end
end % Set the maximum stroke width, this number is variable for now but must be
% made to be more dynamic in the future
maxStrokeWidth = ; % Initialize container for all stoke points found
strokePointsX = zeros(size(edgePointCols));
strokePointsY = zeros(size(strokePointsX));
sizeOfStrokePoints = ; % Iterate through all edge points and compute stoke widths
for i=:size(edgePointRows)
step = ;
initialX = edgePointRows(i);
initialY = edgePointCols(i);
isStroke = ;
initialTheta = theta(initialX,initialY);
sizeOfRay = ;
pointOfRayX = zeros(maxStrokeWidth,);
pointOfRayY = zeros(maxStrokeWidth,); % Record first point of the ray
pointOfRayX(sizeOfRay+) = initialX;
pointOfRayY(sizeOfRay+) = initialY; % Increase the size of the ray
sizeOfRay = sizeOfRay + ; % Follow the ray
while step < maxStrokeWidth
nextX = round(initialX + cos(initialTheta) * searchDirection * step);
nextY = round(initialY + sin(initialTheta) * searchDirection * step); step = step + ; % Break loop if out of bounds. For some reason this is really
% slow.
if nextX < | nextY < | nextX > m | nextY > n
break
end % Record next point of the ray
pointOfRayX(sizeOfRay+) = nextX;
pointOfRayY(sizeOfRay+) = nextY; % Increase size of the ray
sizeOfRay = sizeOfRay + ; % Another edge pixel has been found
if edgeMap(nextX,nextY) oppositeTheta = theta(nextX,nextY); % Gradient direction roughtly opposite
if abs(abs(initialTheta - oppositeTheta) - pi) < pi/
isStroke = ;
strokePointsX(sizeOfStrokePoints+) = initialX;
strokePointsY(sizeOfStrokePoints+) = initialY;
sizeOfStrokePoints = sizeOfStrokePoints + ;
end break
end
end % Edge pixel is part of stroke
if isStroke % Calculate stoke width
strokeWidth = sqrt((nextX - initialX)^ + (nextY - initialY)^); % Iterate all ray points and populate with the minimum stroke width
for j=:sizeOfRay
swtMap(pointOfRayX(j),pointOfRayY(j)) = min(swtMap(pointOfRayX(j),pointOfRayY(j)),strokeWidth);
end
end
end %figure, imshow(swtMap, []), title('Stroke Width Transform: First Pass'); % Iterate through all stoke points for a refinement pass. Refer to figure
% 4b in the paper. for i=:sizeOfStrokePoints
step = ;
initialX = strokePointsX(i);
initialY = strokePointsY(i);
initialTheta = theta(initialX,initialY);
sizeOfRay = ;
pointOfRayX = zeros(maxStrokeWidth,);
pointOfRayY = zeros(maxStrokeWidth,);
swtValues = zeros(maxStrokeWidth,);
sizeOfSWTValues = ; % Record first point of the ray
pointOfRayX(sizeOfRay+) = initialX;
pointOfRayY(sizeOfRay+) = initialY; % Increase the size of the ray
sizeOfRay = sizeOfRay + ; % Record the swt value of first stoke point
swtValues(sizeOfSWTValues+) = swtMap(initialX,initialY);
sizeOfSWTValues = sizeOfSWTValues + ; % Follow the ray
while step < maxStrokeWidth
nextX = round(initialX + cos(initialTheta) * searchDirection * step);
nextY = round(initialY + sin(initialTheta) * searchDirection * step); step = step + ; % Record next point of the ray
pointOfRayX(sizeOfRay+) = nextX;
pointOfRayY(sizeOfRay+) = nextY; % Increase size of the ray
sizeOfRay = sizeOfRay + ; % Record the swt value of next stoke point
swtValues(sizeOfSWTValues+) = swtMap(nextX,nextY);
sizeOfSWTValues = sizeOfSWTValues + ; % Another edge pixel has been found
if edgeMap(nextX,nextY)
break
end
end % Calculate stoke width as the median value of all swtValues seen.
strokeWidth = median(swtValues(:sizeOfSWTValues)); % Iterate all ray points and populate with the minimum stroke width
for j=:sizeOfRay
swtMap(pointOfRayX(j),pointOfRayY(j)) = min(swtMap(pointOfRayX(j),pointOfRayY(j)),strokeWidth);
end end %figure, imshow(swtMap, []), title('Stroke Width Transform: Second Pass'); end

最新文章

  1. 【swift】BlockOperation和GCD实用代码块
  2. 16.The Effect of Advertisement 广告的影响
  3. 【MML】华为MML AAA接口联调,Java版本
  4. django之简单验证码实现与form表单钩子函数补充
  5. 程序猿必备的8款web前端开发插件三
  6. zombodb sql functions 说明
  7. 简介 - RESTful
  8. Linux之IRQ domain
  9. Sterling B2B Integrator与SAP交互 - 02 安装配置
  10. json-server(copy)
  11. win10 无法打开 APICloud Studio 2 的解决方案
  12. git恢复本地删除的文件夹取消增加的文件
  13. hdu-1394(线段树)
  14. C++字符串转化为数字的库函数
  15. Tensorflow - Implement for a Convolutional Neural Network on MNIST.
  16. POJ 1222 EXTENDED LIGHTS OUT(反转)
  17. My SQL 和SQL Server区别
  18. go语言基础之安装go开发环境和beego
  19. 蓝牙(CoreBluetooth)-概述
  20. J - 迷宫问题(BFS)

热门文章

  1. 自己动手开发更好用的markdown编辑器-06(自动更新)
  2. php对象序列化总出错false
  3. svn的外网设置访问方法
  4. 知也atitit.解决struts2&#160;SpringObjectFactory.getClassInstance&#160;NullPointerException&#160;&#160;v2&#160;q31无涯&#160;-&#160;I
  5. script
  6. jquery的text()
  7. 让你的程序通过XP防火墙
  8. iOS valueForKeyPath快速计算求和、平均值、最大、最小
  9. MapReduce源码分析之JobSubmitter(一)
  10. Say goodbye to 重复代码---Eclipse代码模板的使用