【发布时间】:2014-02-28 12:53:26
【问题描述】:
我是 C++ 和 OpenCV 的新手。
我偶然发现了一篇有趣的文章:
http://crblpocr.blogspot.fr/2007/06/run-length-smoothing-algorithm-rlsa.html http://crblpocr.blogspot.fr/2007/06/determination-of-run-length-smoothing.html
此线程在 Matlab 中有 RLSA 实现:
http://mathworks.cn/matlabcentral/newsreader/view_thread/318198
在上面的链接中:Matlab 代码 Bruno Luong 的矢量版
% Data
x=[0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0;
0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0]
C = 4;
% Engine
[m n] = size(x);
xx = [ones(m,1) x ones(m,1)];
xx = reshape(xx',1,[]);
d = diff(xx);
start = find(d==-1);
stop = find(d==1);
lgt = stop-start;
b = lgt <= C;
d(start(b)) = 0;
d(stop(b)) = 0;
yy = cumsum([1 d]);
yy = reshape(yy, [], m)';
y = yy(:,2:end-1)
Yumnam Kirani Singh 的普通版
clear;clc;
x=imread('Picture.jpg');
y=rgb2gray(x) ;
z=histeq(y);
t=im2bw(z);
u=double(t);
[a b]=size(u);
for i=1:a
c=1;
for j=1:b
if u(i,j)==1
if (j-c)<=5
u(i,c:j)=1;
end
c=j;
end
end
if (b-c)<=5
u(i,c:b)=1;
end
end
imshow(u,[]);
任何有 C++ 经验的人都可以用 OpenCV 实现它,C++ 使用 Mat Structure??
已编辑
int hor_thres = 22;
int one_count = 0;
int zero_flag = 0;
Mat tmpImg = Mat(Img.size(), CV_8UC1, Scalar(0, 0, 0));
for (int j = 0; j<Img.rows; j++){
for (int i = 0; i<Img.cols; j++){
if (Img.at<uchar>(j, i) == 0)
{
if (zero_flag == 1)
{
if (one_count <= hor_thres)
{
tmpText(cv::Range(j - zero_count, j), cv::Range(i, i+1)).setTo(cv::Scalar::all(255));
// I want to do the same thing in Matlab as this image(i,j-one_count:j-1)=0;
}
else
{
zero_flag = 1;
}
one_count = 0;
}
zero_flag = 1;
}
else
{
if (zero_flag == 1)
{
one_count = one_count + 1;
}
}
}
}
这次没有错误但结果不是预期的..
问题是我想编写与
相同的 c++ 代码的方式Matlab
tmpImg(i,j-one_count:j-1)=0;
C++
tmpText(cv::Range(j - zero_count, j), cv::Range(i, i+1)).setTo(cv::Scalar::all(255));
知道吗???
另一件事是在 Matlab 中索引从 1 开始,而 C++ 从 0 开始。
谢谢
【问题讨论】:
标签: c++ algorithm matlab opencv