Ask Your Question
0

CvMat zero initialization confusion with cv::Mat

asked 2016-08-05 01:46:15 -0500

supertramp-sid gravatar image

updated 2016-08-05 01:46:54 -0500

I was reading a code which was like this :

CvMat * MyMat[10];

for(int i = 0; i<10; i++)
{
    MyMat[i] = 0;
}

what does this do ?

Does it initialize the MyMat to 0.

And if I want to do the same in cv::Mat , can I do

cv::Mat MyMat[10];

for(int i = 0; i<10; i++)
{
    MyMat[i].setTo(Scalar::all(0));
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-08-05 02:12:06 -0500

berak gravatar image

first, please do not use CvMat, IplImage or anything from the deprecated opencv1.0 c-api. please stick with cv::Mat and c++.

then, neither example probably does , what you want. before you can set values of a Mat, you have to allocate memory, like:

Mat m(3,3,CV_8U);
m.setTo(Scalar::all(17));

or, all in one go:

Mat m(3,3,CV_8U,Scalar::all(17));

(and again, you should ignore the 1st example entirely, it is no more valid today)

please also have a look at the tutorials

edit flag offensive delete link more

Comments

I wasn't using CvMat, but I was trying to understand a code snippet which used CvMat and convert it to cv::Mat. If MyMat[i] = 0; does not set matrix to zero then what does it do ?

supertramp-sid gravatar imagesupertramp-sid ( 2016-08-05 02:26:22 -0500 )edit

you have an array of 10 uninitialized pointers, and it sets the pointers to 0 (there is no matrix or anything yet, just pointers.

berak gravatar imageberak ( 2016-08-05 02:28:02 -0500 )edit

Thanks. Got it

supertramp-sid gravatar imagesupertramp-sid ( 2016-08-05 02:53:43 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-08-05 01:46:15 -0500

Seen: 680 times

Last updated: Aug 05 '16