C ++中实现游程长度平滑算法

时间:2014-02-04 13:53:54

标签: c++ algorithm matlab opencv

我是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吗?

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;
            }
        }
    }
}

这次没有错误但结果不合预期..

问题在于我想要编写与

相同的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开始。

感谢

1 个答案:

答案 0 :(得分:1)

我最终实现了这个算法,希望它能帮助那些需要它的人。

                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;
                            }
                        }
                    }
                }

未来的建议是在不使用循环的情况下改进此实现。