Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cv::findContours is modifying source image OpenCV 2.3

From OpenCV documentation, source image in cv::findContours is aquired as const, but something strange is going on with my application. I'm using cv::inRange function to get thresholded image over specific color, and after that, using cv::moments, I can get the center of white pixels in thresholded image and this is working ok.

In addition, I would like to implement the code for finding biggest contour and locating central moment in that contour. After adding just cv::findContours in the code, I spotted strange behavior in the output and after that I wanted to check what is going on with source image using this code:

cv::Mat contourImage;
threshedImage.copyTo(contourImage); // threshedImage is the output from inRange
cv::findContours(threshedImage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cv::Point(0,0));
cv::Mat temp;
cv::absdiff(threshedImage,contourOutput, temp);
cv::namedWindow("absdiff");
cv::imshow("absdiff",temp);

After this, output is showing that there is a difference between threshedImage and contourImage. How this is possible? Does anyone have similar results with cv::findContours?

like image 951
Banana Avatar asked Sep 25 '12 18:09

Banana


1 Answers

Wrong! The docs clear states that:

Source image is modified by this function.

So if you need the original image intact, make a copy of this image and pass the copy to cv::findContours().

like image 85
karlphillip Avatar answered Sep 28 '22 17:09

karlphillip