Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++, opencv: Is it safe to use the same Mat for both source and destination images in filtering operation?

Filtering operations involve convolutions and the filtered value at position (x,y) will also depend on the intensities of pixels (x-a,y-b) with a,b >0.

So using directly as destination the same image will lead to unexpected behaviors because during calculation I'm taking some already-filtered data instead of original ones.

Question

Does opencv manage this issue internally in functions like cv::GaussianBlur(.) , cv::blur, etc? Is it safe to give a reference to the same Mat to both src and dst parameters? thanks

like image 292
LJSilver Avatar asked Mar 26 '14 18:03

LJSilver


People also ask

What is mat () function in OpenCV?

Working of Mat () Function in OpenCV Mat is a class in OpenCV consisting of two data parts namely matrix header and a pointer to the matrix. The information like size of the matrix, the method used for storing the matrix, the address at which the matrix must be stored etc. is available in the matrix header.

What is scalar&s in OpenCV mat?

Scalar &s specifies the value to be stored in the matrix. Mat is a class in OpenCV consisting of two data parts namely matrix header and a pointer to the matrix. The information like size of the matrix, the method used for storing the matrix, the address at which the matrix must be stored etc. is available in the matrix header.

How to create a matrix in OpenCV using C++?

OpenCV program in C++ to create a matrix using Mat function and display the matrix as the output on the screen. In the above program, we are including the necessary headers. Then we are defining the namespaces std and cv.

Why does OpenCV use a reference counting system?

To tackle this issue OpenCV uses a reference counting system. The idea is that each Mat object has its own header, however a matrix may be shared between two Mat objects by having their matrix pointers point to the same address. Moreover, the copy operators will only copy the headers and the pointer to the large matrix, not the data itself.


1 Answers

Yes, there would not be any problem if you do so. I have done such thing several time. openCV will automatically take care of it.

I tested the following code and it works perfect:

int main(int argc, char* argv[])
{
    Mat src;
    src = imread("myImage.jpeg", 1);
    imshow("src", src); //Original src

    cv::blur( src, src, Size(25,25) , Point(-1,-1), BORDER_DEFAULT );

    imshow("dst", src); //src after blurring

    waitKey(0);
}
like image 156
skm Avatar answered Sep 23 '22 19:09

skm