12

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

Community
  • 1
  • 1
LJSilver
  • 583
  • 7
  • 20
  • there is not problem since if u notice the function, it is passed via reference therefore, the whole array / Mat gets re-written at the end of the function. (This is in simplest explanation) – kcc__ Mar 27 '14 at 03:02
  • 1
    Sorry, I did not understand your comment. The problem was if the processing image were re-written during the filtering – LJSilver Mar 27 '14 at 09:40
  • This is being called as `in-place mode`: https://answers.opencv.org/question/24/do-all-opencv-functions-support-in-place-mode-for-their-arguments/ https://stackoverflow.com/questions/15343791/can-the-opencv-function-cvtcolor-be-used-to-convert-a-matrix-in-place – n0099 Feb 15 '23 at 03:39

1 Answers1

7

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);
}
skm
  • 5,015
  • 8
  • 43
  • 104
  • 1
    thanks for replying. When I used it this way I saw a good result too, but actually I could not tell whether it is really what it seems just by looking at it. So yes it looks ok but you know, I need to be sure of that. Are you sure that there isn't any problem (also using the other functions) ? thanks again – LJSilver Mar 26 '14 at 23:49
  • Yes, most image filters in OpenCV allow the same input and output. But be careful when you're converting images (e.g. color to grayscale) you'll need two Mats – azer89 Mar 27 '14 at 02:48
  • 2
    @mb_: As long as the type of two images which are used in the conversion is same, there wont't be any problem. But if you want to convert a `color image` which have 3 channels into a `grayimage` which has single channel, then you will get segmentation fault. So, make sure that whether your `src` is suitable to become `dst` also. – skm Mar 27 '14 at 06:22
  • 1
    Yes ok, but i'm not talking about size or channels problems, I know that the images must be equal in dimension and depth, my questions was on the fact that the filtering operation needs another image, filtering can't be done in a single image. So I was wondering wether opencv used this second image internally or if I just have to enter different images. – LJSilver Mar 27 '14 at 09:53
  • ps. for bilateralFilter it gives: `OpenCV Error: Assertion failed ((src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.type() == dst.type() && src.size() == dst.size() && src.data != dst.data) in bilateralFilter_8u, file /build/opencv/src/opencv-2.4.7/modules/imgproc/src/smooth.cpp, line 1925 terminate called after throwing an instance of 'cv::Exception' what(): /.../smooth.cpp:1925: error: (-215) (src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.type() == dst.type() && src.size() == dst.size() && src.data != dst.data in function bilateralFilter_8u` – LJSilver Mar 27 '14 at 17:52
  • 4
    Can you link to openCV documentation confirming that it's ok for the dst image to be the same as one of the src images? – dinosaur Jul 06 '16 at 22:44
  • I can't find an authoritative answer, but from my own code, and looking at the source code for say GaussianBlur, it has lines like `if (src.data == dst.data) src = src.clone();`. So they seem to clone the src as needed. – bramp Nov 27 '21 at 19:48