实施运行长度平滑算法的C ++

人气:1,055 发布:2022-09-11 标签: algorithm c++ matlab opencv

问题描述

我是新使用C ++和OpenCV。

I am new with C++ and OpenCV.

我读到一篇有趣的文章:

I came across an interesting article:

http://crblpocr.blogspot.fr/ 2007/06 /运行长度平滑算法-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的code 矢量版本布鲁诺陈德良

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)

普通版由Yumnam Kirani辛格

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实现它++使用垫结构??

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 ..

问题是我想写的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开始。

Another thing is in Matlab the index start from 1 while C++ start from 0.

感谢

推荐答案

我终于实现这个算法,希望能帮助那些谁需要它。

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

未来的建议是,以改善这个实现在不使用循环。

881