笔画宽度变化(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
% 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
笔画宽度变化(C++和matlab算法)的更多相关文章
- 应用笔画宽度变换(SWT)来检测自然场景中的文本
Introduction: 应用背景:是盲人辅助系统,城市环境中的机器导航等计算机视觉系统应用的重要一步.获取文本能够为许多视觉任务提供上下文的线索,并且,图像检索算法的性能很大部分都依赖于对应的文本 ...
- 遗传学详解及Matlab算法实现
遗传学算法概述 从之前转载的博客<非常好的理解遗传算法的例子>中可以知道,遗传学算法主要有6个步骤: 1. 个体编码 2. 初始群体 3. 适应度计算 4. 选择运算 5. 交叉运算 6. ...
- 通过sessionStorage来根据屏幕宽度变化来加载不同的html页面
因为项目需要,分别写了移动端和PC端的两个html页面,现在需要根据不同的屏幕宽度来加载对应的页面. 先说一下本人的思路-- 刚开始我直接在加载页面的时候判断屏幕宽度,然后加载相应的页面,大家是不是也 ...
- 边缘检测matlab算法汇总
边缘检测matlab算法汇总 1. 基于一阶微分算子检测边缘图像 一阶微分边缘算子又称梯度边缘算子,它是利用图像在边缘处的阶跃性,及图像梯度在边缘去得极大值得特征性进行边缘检测. Sobel ...
- matlab算法转为c语言注意事项
matlab算法转为c语言后,影响c语言效率的关键在于multiword的产生,基于此会有multiword加减法和乘除法,极大消耗资源,减少甚至消除multiword很重要,需注意的是:算法中尽量减 ...
- vue 如何重绘父组件,当子组件的宽度变化时候
vue 如何重绘父组件,当子组件的宽度变化时候 vue & dynamic el-popover position demo https://codepen.io/xgqfrms/pen/wv ...
- matlab算法
流水线型车间作业调度问题遗传算法Matlab源码流水线型车间作业调度问题可以描述如下:n个任务在流水线上进行m个阶段的加工,每一阶段至少有一台机器且至少有一个阶段存在多台机器,并且同一阶段上各机器的处 ...
- 数字图像处理:基于MATLAB的车牌识别项目 标签: 图像处理matlab算法 2017-06-24 09:17 98人阅读 评论(0)
学过了数字图像处理,就进行一个综合性强的小项目来巩固一下知识吧.前阵子编写调试了一套基于MATLAB的车牌识别的项目的代码.今天又重新改进了一下代码,识别的效果好一点了,也精简了一些代码.这里没有使用 ...
- 将caffe训练时loss的变化曲线用matlab绘制出来
1. 首先是提取 训练日志文件; 2. 然后是matlab代码: clear all; close all; clc; log_file = '/home/wangxiao/Downloads/43_ ...
随机推荐
- 62. Search in Rotated Sorted Array【medium】
62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...
- HTTP解读
使用Telnet工具访问web资源 Windows中没有telnet这一工具,下面在Linux下演示: telnet www.baidu.com 80 Trying 61.135.169.125... ...
- nginx服务器部署
nginx(“engine x”)是一个高性能的HTTP和反向代理服务器. 安装nginx Linux下 sudo apt-get install nginx windows下 下载 nginx ...
- spring boot 打包报错
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.0.RELEASE:repac ...
- centos httpd服务做yum本地源,以及安装Mysql
step0 首先centos的iso文件是有两张的,dvd1和dvd2,dvd2是额外的软件,常有的一些软件都在dvd1里面,而且repodata配置文件也在dvd1里面,如果直接把dvd2当做镜像文 ...
- MFC使用自带的MSXML6.dll解析xml(开发环境vc2010)
程序是win32控制台程序 // msxml.cpp : 定义控制台应用程序的入口点. #include "stdafx.h" #include <iostream> ...
- ext2文件系统了解
一个磁盘可以划分成多个分区,每个分区必须先用格式化工具(例如mkfs命令)格式化成某种格式的文件系统,然后才能存储文件,格式化的过程会在磁盘上写一些管理存储布局的信息.下以ext2文件系统为例说明文件 ...
- 日志系统之扩展Flume-LineDeserializer
本人博客文章如未特别注明皆为原创.如有转载请注明出处:http://blog.csdn.net/yanghua_kobe/article/details/46595401 继续闲聊日志系统,在之前的博 ...
- JavaScript 框架(库)
JavaScript 高级程序设计(特别是对浏览器差异的复杂处理),通常很困难也很耗时. 为了应对这些调整,许多的 JavaScript (helper) 库应运而生. 这些 JavaScript 库 ...
- CSS3之body背景图平铺
你再也不需要因为屏幕大小不同而去选择多张图片作为背景图了,css3教你做人: body { background: url('xx.jpg')top center no-repeat; backgro ...