尝试执行运行长度平滑算法的C ++ [英] Attempt to implementation Running Length Smoothing Algorithm in C++
问题描述
这是我的老问题涉及到<一个href="http://stackoverflow.com/questions/21554431/implementation-run-length-smoothing-algorithm-in-c">RLSA在C ++中,但我还没有得到任何帮助呢。
This is my old question related to RLSA in C++, but I havent got any help yet.
我试图实现从MATLAB中code到C ++
I tried to implement the code from Matlab to C++
该算法的描述:
http://crblpocr.blogspot.fr/ 2007/06 /游程平滑算法,rlsa.html 的http://crblpocr.blogspot.fr/2007/06/determination-of-run-length-smoothing.html
目前在Matlab RLSA执行此线程:
There is RLSA implementation in Matlab by this thread :
http://mathworks.cn/matlabcentral/newsreader/view_thread/318198
MatLab的code
MatLabCode
hor_thresh=20;
zeros_count=0;
one_flag=0;
hor_image=image;
for i=1:m
for j=1:n
if(image(i,j)==1)
if(one_flag==1)
if(zeros_count<=hor_thresh)
hor_image(i,j-zeros_count:j-1)=1;
else
one_flag=0;
end
zeros_count=0;
end
one_flag=1;
else
if(one_flag==1)
zeros_count=zeros_count+1;
end
end
end
end
我想用C ++ code
I tried to implement in C++ Code
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 ..
问题是我想写的C ++ $ C C同样的事情$为
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 ++
C++
tmpText(cv::Range(j - zero_count, j), cv::Range(i, i+1)).setTo(cv::Scalar::all(255));
Anyidea ???
Anyidea???
另一件事是在Matlab 1索引开始而C ++从0开始。
感谢
推荐答案
所有谢谢@Roger罗兰我终于实现这个算法,希望可以帮助那些谁需要它。
all thank you @Roger Rowland 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;
}
}
}
}
未来的建议是,以改善这个实现在不使用循环。
这篇关于尝试执行运行长度平滑算法的C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!