【发布时间】:2014-03-03 15:23:15
【问题描述】:
这是我与RLSA in C++ 相关的老问题,但我还没有得到任何帮助。
我尝试将代码从 Matlab 实现到 C++
该算法的描述:
http://crblpocr.blogspot.fr/2007/06/run-length-smoothing-algorithm-rlsa.htmlhttp://crblpocr.blogspot.fr/2007/06/determination-of-run-length-smoothing.html
此线程在 Matlab 中有 RLSA 实现:
http://mathworks.cn/matlabcentral/newsreader/view_thread/318198
MatLab 代码
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++ 代码实现
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