title: Implementation Run Length Smoothing Algorithm in C++
body: <p>I am new with C++ and OpenCV.</p>
  
  <p>I came across an interesting article:</p>
  
  <p><a href="http://crblpocr.blogspot.fr/2007/06/run-length-smoothing-algorithm-rlsa.html" rel="nofollow">http://crblpocr.blogspot.fr/2007/06/run-length-smoothing-algorithm-rlsa.html</a>
     <a href="http://crblpocr.blogspot.fr/2007/06/determination-of-run-length-smoothing.html" rel="nofollow">http://crblpocr.blogspot.fr/2007/06/determination-of-run-length-smoothing.html</a></p>
  
  <p><strong>There is RLSA implementation in Matlab by this thread :</strong> </p>
  
  <p><a href="http://mathworks.cn/matlabcentral/newsreader/view_thread/318198" rel="nofollow">http://mathworks.cn/matlabcentral/newsreader/view_thread/318198</a></p>
  
  <p><strong>In link above : Matlab Code
  Vector Version by Bruno Luong</strong></p>
  
  <pre><code>% 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 &lt;= C;
  d(start(b)) = 0;
  d(stop(b)) = 0;
  yy = cumsum([1 d]);
  yy = reshape(yy, [], m)';
  y = yy(:,2:end-1)
  </code></pre>
  
  <p><strong>Normal Version by Yumnam Kirani Singh</strong></p>
  
  <pre><code>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)&lt;=5 
      u(i,c:j)=1;
  end
  c=j;
   end
   end
  if (b-c)&lt;=5
     u(i,c:b)=1;
     end
      end
  imshow(u,[]); 
  </code></pre>
  
  <p><strong>Anyone has experience in C++ could implement it with OpenCV, C++ using Mat Structure??</strong></p>
  
  <p><strong>EDITED</strong></p>
  
  <pre><code>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&lt;Img.rows; j++){
      for (int i = 0; i&lt;Img.cols; j++){
          if (Img.at&lt;uchar&gt;(j, i) == 0)
          {
              if (zero_flag == 1)
              {
                  if (one_count &lt;= 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;
              }
          }
      }
  }
  </code></pre>
  
  <p>This time no error but the result is not expected ..</p>
  
  <p>The issue is the way i want to write c++ code the same thing as</p>
  
  <p>Matlab</p>
  
  <pre><code>tmpImg(i,j-one_count:j-1)=0;
  </code></pre>
  
  <p>C++</p>
  
  <pre><code>tmpText(cv::Range(j - zero_count, j), cv::Range(i, i+1)).setTo(cv::Scalar::all(255));
  </code></pre>
  
  <p>Anyidea???</p>
  
  <p>Another thing is in Matlab the index start from 1 while C++ start from 0.</p>
  
  <p>Thank</p>