Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a const Mat reference in OpenCV make sense?

In the following function

foo(const Mat& img)

img can be changed in the function without even a warning by the compiler. Why? Does it mean const Mat reference don't make any sense?

like image 516
beaver Avatar asked Sep 05 '25 03:09

beaver


1 Answers

That is because a Mat contains a pointer to the actual image data. The const applies only to the Mat object itself (e.g. attributes like rows, cols) and not to the data referred to by the pointer. Note: even if the function was

foo(Mat img)

you could still change the image data.

There are a number of advantages to passing a Mat as const reference. It tells programmers something about how to use foo () and how to modify foo(). Also, it stops you doing things like:

void foo(const cv::Mat& img)
{
    img.create(5, 6, CV_8UC3);
}

This will get a compiler error.

like image 196
Bull Avatar answered Sep 09 '25 16:09

Bull