【问题标题】:Implementation Run Length Smoothing Algorithm in C++在 C++ 中实现运行长度平滑算法
【发布时间】: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


    【解决方案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;
                                }
                            }
                        }
                    }
    

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

    【讨论】:

      猜你喜欢
      • 2014-03-03
      • 1970-01-01
      • 1970-01-01
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      • 2012-10-10
      相关资源
      最近更新 更多