实现运行长度平滑算法在C ++ [英] Implementation Run Length Smoothing Algorithm in C++
问题描述
我是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
如果u(i,j)== 1
if(j-c)<= 5
u(i,c:j)
end
c = j;
end
end
if(b-c)<= 5
u(i,c:b)= 1;
end
end
imshow(u,[]);任何有C ++经验的人都可以用OpenCV实现它,C ++使用Mat Structure? / strong>
已修改
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 for(int i = 0; i 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));
//我想在Matlab中做同样的事情,这个图像(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)
Anyidea ???
在Matlab中索引从1开始,而C ++从0开始。
感谢
解决方案< int hor_thres = 22;
int zero_count = 0;
int one_flag = 0;
for(int i = 0; i for(int j = 0; j< tmpImg.cols; j ++){
if(tmpImg.at< uchar>(i,j)== 255)
{
if(one_flag == 255)
{
if(zero_count <= hor_thres)
{
tmpImg(cv :: Range(i,i + 1),cv :: Range(j - zero_count,j))setTo(cv :: Scalar :: all );
}
else
{
one_flag = 0;
}
zero_count = 0;
}
one_flag = 255;
}
else
{
if(one_flag == 255)
{
zero_count = zero_count + 1;
}
}
}
}
strong>未来的建议是改进此实现,而不使用循环。
I am new with C++ and OpenCV.
I came across an interesting article:
http://crblpocr.blogspot.fr/2007/06/run-length-smoothing-algorithm-rlsa.html
http://crblpocr.blogspot.fr/2007/06/determination-of-run-length-smoothing.html
There is RLSA implementation in Matlab by this thread :
http://mathworks.cn/matlabcentral/newsreader/view_thread/318198
In link above : Matlab Code
Vector Version by 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)
Normal Version by 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,[]);
Anyone has experience in C++ could implement it with OpenCV, C++ using Mat Structure??
EDITED
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;
}
}
}
}
This time no error but the result is not expected ..
The issue is the way i want to write c++ code the same thing as
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));
Anyidea???
Another thing is in Matlab the index start from 1 while C++ start from 0.
Thank
解决方案 I finally implement this algorithm, hope it could help those who need it.
int hor_thres = 22;
int zero_count = 0;
int one_flag = 0;
for (int i = 0; i<tmpImg.rows; i++){
for (int j = 0; j<tmpImg.cols; j++){
if (tmpImg.at<uchar>(i, j) == 255)
{
if (one_flag == 255)
{
if (zero_count <= hor_thres)
{
tmpImg(cv::Range(i, i + 1), cv::Range(j - zero_count, j)).setTo(cv::Scalar::all(255));
}
else
{
one_flag = 0;
}
zero_count = 0;
}
one_flag = 255;
}
else
{
if (one_flag == 255)
{
zero_count = zero_count + 1;
}
}
}
}
Future suggestion is to improve this implementation without using loop.
这篇关于实现运行长度平滑算法在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文