2014-02-06 76 views
1

這是我的老問題,涉及RLSA in C++,但我還沒有得到任何幫助。試圖在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

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實現++代碼

   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

謝謝

回答

0

所有的感謝@羅格羅蘭我終於實現了這個算法,希望它能幫助那些需要它的人。

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

未來的建議是在不使用循環的情況下改進此實現。

2

由行/列,而不是X/Y的OpenCV索引開始,所以這樣做,而不是:

if (tmpText.at<uchar>(j, i) == 0) 
         ^^^^ 

您需要修復使用at<T>(row,col)函數的其他代碼。

+0

謝謝,我覺得也有這個以及0123發行Matlab的 'hor_image(I,J-one_count:J-1)= 0;' 當我在C++實現 '爲(INT COL = j的 - one_count - 1; col (i-1,col)= 0; }' – sayvortana

+0

@sayvortana是的,就像我上面說的*「你需要修復你的代碼的其餘部分......」* ... :-) –

+0

嗨@RogerRowland,我編輯了問題中的C++代碼,我得到的新錯誤請看看謝謝 – sayvortana

1

如果你有黑色的前景和白色背景,那麼希望你會發現我的實現有用。

Horisontal RLSA

void horizontalRLSA(Mat &input, Mat &output, int thresh) 
    { 
     for (int j = 0; j < input.rows; j++) 
     { 
      int count = 0; 
      int flag = 0; 
      for (int i = 0; i < input.cols; i++) 
      { 
       if (input.at<uchar>(j, i) == 255) 
       { 
        flag = 255; 
        count++; 
       } 
       else 
       { 
        if (flag == 255 && count <= thresh) 
        { 
         output(Rect(i - count, j, count, 1)).setTo(Scalar::all(0)); 
        } 
        flag = 0; 
        count = 0; 
       } 
      } 
     } 
    } 

垂直RLSA

void verticalRLSA(Mat &input, Mat &output, int thresh) 
    { 
     for (int i = 0; i < input.cols; i++) 
     { 
      int count = 0; 
      int flag = 0; 
      for (int j = 0; j < input.rows; j++) 
      { 
       if (input.at<uchar>(j, i) == 255) 
       { 
        flag = 255; 
        count++; 
       } 
       else 
       { 
        if (flag == 255 && count <= thresh) 
        { 
         output(Rect(i, j - count, 1, count)).setTo(Scalar::all(0)); 
        } 
        flag = 0; 
        count = 0; 
       } 
      } 
     } 
    } 

使用

Mat input_binary_image; 
Mat hrlsa = input_binary_image.clone(); 
horizontalRLSA(input_binary_image, hrlsa, 50);