Ask Your Question
1

Using colone Matlab in c++ opencv Mat Structure

asked 2014-02-06 09:06:57 -0500

vsay gravatar image

updated 2014-02-07 09:36:45 -0500

Hi everyone,

Are there any ways in c++ I can access Mat structure like colon in Matlab.

in Matlab

image(i,j-one_count:j-1)=0;

how can I write the same thing in C++?

EDIT

According to Running length smoothing algorithm The whole code

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

I tried to implement in C++ 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<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++

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

Anyidea???

Thank

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-02-06 09:29:26 -0500

Guanta gravatar image

updated 2014-02-06 11:22:24 -0500

Not tested, but you'll get the spirit:

cv::Mat tmp(image, cv::Range(i,i+1), cv::Range(j-one_count, j-1)).setTo(cv::Scalar::all(0));

see http://docs.opencv.org/modules/core/doc/basic_structures.html?highlight=setto#mat for more mat-operations. Note, the same could be achieved with cv::Rect, instead of cv::Range. Also note, Matlab indices start with 1, C/C++ (and others) start with 0 (so maybe the indices from above are not correct).

EDIT

My solution (see above) is slightly wrong (the temporary matrix t isn't necessary), the following way works:

image(cv::Range(i,i+1), cv::Range(j-one_count, j-1)).setTo(cv::Scalar::all(0));

Example:

cv::Mat1i image = (cv::Mat1i(3,4) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
std::cout << image << std::endl;
image(cv::Range(1,2), cv::Range(1, 3)).setTo(cv::Scalar::all(0));
std::cout << image << std::endl;

Output

[1, 2, 3, 4;
  5, 6, 7, 8;
  9, 10, 11, 12]
[1, 2, 3, 4;
  5, 0, 0, 8;
  9, 10, 11, 12]
edit flag offensive delete link more

Comments

Thank, I upload my whole code in new question where there are both Matlab and C++ http://answers.opencv.org/question/27866/implementation-run-length-smoothing-algorithm-in-c/

vsay gravatar imagevsay ( 2014-02-06 09:35:40 -0500 )edit

Error C2039: 'setTo' : is not a member of 'cv::Range'

vsay gravatar imagevsay ( 2014-02-06 09:59:40 -0500 )edit

guess you forgot the second bracket...

Guanta gravatar imageGuanta ( 2014-02-06 11:23:46 -0500 )edit

Thank, @Guanta, i edited the question please have a look

vsay gravatar imagevsay ( 2014-02-07 06:50:21 -0500 )edit

if j is 0 the range is wrong

Guanta gravatar imageGuanta ( 2014-02-07 08:26:01 -0500 )edit

@Guanta you are right, I edited the C++ code I want to write c++ code to do the same thing as this matlab code tmpImg(i,j-one_count:j-1)=0;

vsay gravatar imagevsay ( 2014-02-07 09:17:40 -0500 )edit

Guess you get confused by your own changes of zero_count and one_count and no I'm really not here to transfer your matlab code to C++, i.e. you should figure it out yourself now.

Guanta gravatar imageGuanta ( 2014-02-07 14:24:40 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-02-06 09:06:57 -0500

Seen: 445 times

Last updated: Feb 07 '14