笔画宽度变化(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_ ...
随机推荐
- kafka 部分问题处理记录
转载请注明原创地址:http://www.cnblogs.com/dongxiao-yang/p/7600561.html 一 broker启动后ReplicaFetcherThread OOM 版 ...
- css之常用属性
背景属性: background-attachment 设置背景图像是否固定或者随着页面的其余部分滚动 值: scroll 默认值.背景图像会随着页面其余部分的滚动而移动. fixed 当页面的其余部 ...
- Unity学习笔记 - Assets, Objects and Serialization
Assets和Objects Asset是存储在硬盘上的文件,保存在Unity项目的Assets文件夹内.比如:纹理贴图.材质和FBX都是Assets.一些Assets以Unity原生格式保存数据,例 ...
- 微信全局获取并缓存Accesstoken的值
由于本项目中使用自定义菜单接口.获取用户信息接口.用户分组接口.消息发送接口等,都需要传入一个相同的参数access_token,其有效期 是7200秒(两小时),在有效期内可以使用,一旦access ...
- GoWeb编程之多路复用
GoWeb编程多路复用 在web编程中,比如我们要一个Url对应一个处理函数,或者一个页面对应一个静态文件,这些都需要进行处理,这个时候就是我们多路复用派上用场了. package main impo ...
- ueditor 控制上传图片的显示尺寸
使用UEditor的编辑框插入图片的时候,如果图片尺寸比较大,则图片会超出编辑器边框出现滚动条,特别不方便. 解决办法:在ueditor 的 themes 文件夹下有个iframe.css 文件,在该 ...
- bat遍历当前目录下的文件,批量重命名
@echo off setlocal enabledelayedexpansion for %%x in (*) do ( if not "%%x"=="demo.bat ...
- echarts(3.0)的基本使用(标签式导入)
function loadRainFallCharts(msg) { var obj = {}; obj.x = []; obj.y = []; obj.line = []; var accumula ...
- Laravel5.1 模型初探
Laravel的模型也是访问数据库的,它更加面向对象,一个模型对应着一张表 我们可以使用模型对数据做一些增删改查的操作. 1 创建模型 创建模型是可以使用Artisan控制台的: php artisa ...
- 【网络优化|渣速必看】合理设置MTU,提升网络速度
可能很少网友注意过“本机.网络”的“MTU”值对自己网络性能产生的影响.对于追求更快的下载速度来说,MTU值设置不当,就仿佛穿着高跟鞋跑步一般. MTU是什么? “MTU=最大传输单元 单位:字节” ...