Click here to Skip to main content
15,942,795 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
Hi I can't understand how does work this line of following code:

Mat sampleMat = (Mat_<float>(1,2) << i,j);

what is "Mat_" ?
Why (1,2)?



C++
Vec3b green(0,255,0), blue (255,0,0);

for (int i = 0; i < image.rows; ++i)
    for (int j = 0; j < image.cols; ++j)
    {
    Mat sampleMat = (Mat_<float>(1,2) << i,j);
    float response = SVM.predict(sampleMat);

    if (response == 1)
       image.at<Vec3b>(j, i)  = green;
    else
    if (response == -1)
       image.at<Vec3b>(j, i)  = blue;
    }


Also why in if-else conditions it uses (j,i) not (i,j) ?

The complete explanation is exist here : http://docs.opencv.org/doc/tutorials/ml/introduction_to_svm/introduction_to_svm.html
Posted

1 solution

Mat is short form for "Matrice" and it is the central type used to store images and other things in openCV. You should really learn those very basics if you want to work with openCV. The documentation of openCV is quite good. See for example here:
http://docs.opencv.org/modules/core/doc/basic_structures.html[^]

With the constructor as shown in your code:
Mat sampleMat = (Mat_<float>(1,2) << i,j);
you create a matrice with 2 elements (1 row, 2 columns) and store the values of i and j into it.

"Also why in if-else conditions it uses (j,i) not (i,j) ?"
You're right it should be indexed with (i, j) and in fact that's what is done in the tutorial you'
ve linked. So it seems you made a mistake there when copying the code.
 
Share this answer
 
v3
Comments
[no name] 12-Jun-14 9:37am    
5ed. Nicely explained.
hor_313 12-Jun-14 10:57am    
Thank you Legor!
I only wanted to know about Mat_ type, and your link helped me.
Also I've copied the code from OpenCV document directly and the mistake is in the tutorial code.
[no name] 12-Jun-14 11:00am    
You have replied to me! Click the "Have a Question or Comment?" widget to reply the the person who answered!
hor_313 12-Jun-14 14:12pm    
Ok, thanks!
hor_313 12-Jun-14 14:12pm    
Thank you Legor!
I only wanted to know about Mat_ type, and your link helped me.
Also I've copied the code from OpenCV document directly and the mistake is in the tutorial code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900