Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I initialize a cv::Mat

Tags:

c++

opencv

I have this code:

mapx.create(image.size(), CV_32FC1);
mapy.create(image.size(), CV_32FC1);

what is the values in the mapx and mapy after this? Are all data is zero?

what about this type of initialization:

  cv::Mat  mapx(image.size(), CV_32FC1);

Do I need explicitly set the value of each element to zero?

How can I set the value of each element to say -1?

like image 901
mans Avatar asked Feb 17 '26 23:02

mans


1 Answers

Data after create should be undefined. In fact, your are just allocating memory.

cv::Mat  mapx(image.size(), CV_32FC1);

is exactly as

cv::Mat1f mapx(image.size());

and

cv::Mat mapy;
mapy.create(image.size(), CV_32FC1);

You can assign an initial value (e.g. -1) like this:

cv::Mat1f(images.size(), -1.f);

Regarding you main question Should I initialize a cv::Mat, the answer is that in general you don't need to. From OpenCV doc:

Instead of writing:

Mat color;
...
Mat gray(color.rows, color.cols, color.depth());
cvtColor(color, gray, CV_BGR2GRAY);

you can simply write:

Mat color;
...
Mat gray;
cvtColor(color, gray, CV_BGR2GRAY);
like image 147
Miki Avatar answered Feb 20 '26 11:02

Miki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!