title: Attempt to implementation Running Length Smoothing Algorithm in C++
body: <p>This is my old question related to <a href="https://stackoverflow.com/questions/21554431/implementation-run-length-smoothing-algorithm-in-c">RLSA in C++</a>, but I havent got any help yet.</p>
  
  <p>I tried to implement the code from Matlab to C++</p>
  
  <p>The description of this algorithm :</p>
  
  <p><a href="http://crblpocr.blogspot.fr/2007/06/run-length-smoothing-algorithm-rlsa.html" rel="nofollow noreferrer">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 noreferrer">http://crblpocr.blogspot.fr/2007/06/determination-of-run-length-smoothing.html</a></p>
  
  <p>There is RLSA implementation in Matlab by this thread :</p>
  
  <p><a href="http://mathworks.cn/matlabcentral/newsreader/view_thread/318198" rel="nofollow noreferrer">http://mathworks.cn/matlabcentral/newsreader/view_thread/318198</a></p>
  
  <p>MatLabCode</p>
  
  <pre><code>    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&lt;=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
  </code></pre>
  
  <p>I tried to implement in C++ Code       </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><strong>Another thing is in Matlab the index start from 1 while C++ start from 0.</strong></p>
  
  <p>Thank</p>