Implementation Running length Smoothing Algorithm

Hi, I am new in C++ programming.
I use c++ and opencv. The type of image is Mat structure.

In matlab, I did this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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


In C++ I tried this


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
                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 ..

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

1
2
tmpText(cv::Range(j - zero_count, j), cv::Range(i,
i+1)).setTo(cv::Scalar::all(255));


Anyidea???

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

Thank
What is tmpText? A typo perhaps? Should you be using tmpImg?

Edit: If it is a Mat type, you are creating a temporary and it is immediately being destroyed, so line 13 is essentially a no-op.

That may not be correct. I'll have to do some more reading.
After further reading, that definitely was not correct.
Last edited on
Topic archived. No new replies allowed.