首页 > 笔画宽度变化(C++和matlab算法)

笔画宽度变化(C++和matlab算法)

最近一直在看工作方面的书籍,把论文的事情搁置了,之前承诺的贴代码的事一直拖。现在把代码整理发上来,只有核心部分的,都不是我写的,我是网上整理下载的,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 3
%   searchDirection = gradient direction is either 1 to detect dark text on light
%   background or -1 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,1),size(edgeMap,2));% Calculating theta, gradient direction, for each pixel on the image.
% ***This can be optimized by using edgePointCols and edgePointRows
% instead.***
for i=1:size(edgeMap,1)for j=1:size(edgeMap,2)if edgeMap(i,j) == 1theta(i,j) = atan2(dy(i,j),dx(i,j));endend
end% Getting size of the image
[m,n] = size(edgeMap);% Initializing Stoke Width array with infinity
swtMap = zeros(m,n);
for i=1:mfor j=1:nswtMap(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 = 350;% Initialize container for all stoke points found
strokePointsX = zeros(size(edgePointCols));
strokePointsY = zeros(size(strokePointsX));
sizeOfStrokePoints = 0;% Iterate through all edge points and compute stoke widths
for i=1:size(edgePointRows)step = 1;initialX = edgePointRows(i);initialY = edgePointCols(i);isStroke = 0;initialTheta = theta(initialX,initialY);sizeOfRay = 0;pointOfRayX = zeros(maxStrokeWidth,1);pointOfRayY = zeros(maxStrokeWidth,1);% Record first point of the raypointOfRayX(sizeOfRay+1) = initialX;pointOfRayY(sizeOfRay+1) = initialY;% Increase the size of the raysizeOfRay = sizeOfRay + 1;% Follow the raywhile step < maxStrokeWidthnextX = round(initialX + cos(initialTheta) * searchDirection * step);nextY = round(initialY + sin(initialTheta) * searchDirection * step);step = step + 1;% Break loop if out of bounds.  For some reason this is really% slow.if nextX < 1 | nextY < 1 | nextX > m | nextY > nbreakend% Record next point of the raypointOfRayX(sizeOfRay+1) = nextX;pointOfRayY(sizeOfRay+1) = nextY;% Increase size of the raysizeOfRay = sizeOfRay + 1;% Another edge pixel has been foundif edgeMap(nextX,nextY)oppositeTheta = theta(nextX,nextY);% Gradient direction roughtly oppositeif abs(abs(initialTheta - oppositeTheta) - pi) < pi/2isStroke = 1;strokePointsX(sizeOfStrokePoints+1) = initialX;strokePointsY(sizeOfStrokePoints+1) = initialY;sizeOfStrokePoints = sizeOfStrokePoints + 1;endbreakendend% Edge pixel is part of strokeif isStroke% Calculate stoke widthstrokeWidth = sqrt((nextX - initialX)^2 + (nextY - initialY)^2);% Iterate all ray points and populate with the minimum stroke widthfor j=1:sizeOfRayswtMap(pointOfRayX(j),pointOfRayY(j)) = min(swtMap(pointOfRayX(j),pointOfRayY(j)),strokeWidth);endend
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=1:sizeOfStrokePointsstep = 1;initialX = strokePointsX(i);initialY = strokePointsY(i);initialTheta = theta(initialX,initialY);sizeOfRay = 0;pointOfRayX = zeros(maxStrokeWidth,1);pointOfRayY = zeros(maxStrokeWidth,1);swtValues = zeros(maxStrokeWidth,1);sizeOfSWTValues = 0;% Record first point of the raypointOfRayX(sizeOfRay+1) = initialX;pointOfRayY(sizeOfRay+1) = initialY;% Increase the size of the raysizeOfRay = sizeOfRay + 1;% Record the swt value of first stoke pointswtValues(sizeOfSWTValues+1) = swtMap(initialX,initialY);sizeOfSWTValues = sizeOfSWTValues + 1;% Follow the raywhile step < maxStrokeWidthnextX = round(initialX + cos(initialTheta) * searchDirection * step);nextY = round(initialY + sin(initialTheta) * searchDirection * step);step = step + 1;% Record next point of the raypointOfRayX(sizeOfRay+1) = nextX;pointOfRayY(sizeOfRay+1) = nextY;% Increase size of the raysizeOfRay = sizeOfRay + 1;% Record the swt value of next stoke pointswtValues(sizeOfSWTValues+1) = swtMap(nextX,nextY);sizeOfSWTValues = sizeOfSWTValues + 1;% Another edge pixel has been foundif edgeMap(nextX,nextY)breakendend% Calculate stoke width as the median value of all swtValues seen.strokeWidth = median(swtValues(1:sizeOfSWTValues));% Iterate all ray points and populate with the minimum stroke widthfor j=1:sizeOfRayswtMap(pointOfRayX(j),pointOfRayY(j)) = min(swtMap(pointOfRayX(j),pointOfRayY(j)),strokeWidth);endend%figure, imshow(swtMap, []), title('Stroke Width Transform: Second Pass');end

 

转载于:https://www.cnblogs.com/dawnminghuang/p/3906622.html

更多相关:

  • 上篇笔记中梳理了一把 resolver 和 balancer,这里顺着前面的流程走一遍入口的 ClientConn 对象。ClientConn// ClientConn represents a virtual connection to a conceptual endpoint, to // perform RPCs. // //...

  • 我的实验是基于PSPNet模型实现二维图像的语义分割,下面的代码直接从得到的h5文件开始往下做。。。 也不知道是自己的检索能力出现了问题还是咋回事,搜遍全网都没有可以直接拿来用的语义分割代码,东拼西凑,算是搞成功了。 实验平台:Windows、VS2015、Tensorflow1.8 api、Python3.6 具体的流程为:...

  • Path Tracing 懒得翻译了,相信搞图形学的人都能看得懂,2333 Path Tracing is a rendering algorithm similar to ray tracing in which rays are cast from a virtual camera and traced through a s...

  • configure_file( [COPYONLY] [ESCAPE_QUOTES] [@ONLY][NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ]) 我遇到的是 configure_file(config/config.in ${CMAKE_SOURCE_DIR}/...

  •     直接复制以下代码创建一个名为settings.xml的文件,放到C:UsersAdministrator.m2下即可