Cv :: findContours modifies original OpenCV 2.3 image

From the OpenCV documentation, the original image in cv :: findContours is acquired as const , but something strange is happening with my application. I use the cv :: inRange function to get the threshold image by a specific color, and after that, using cv :: moments, I can get the center of the white pixels in the threshold image, and this works fine.

In addition, I would like to implement code to find a large contour and determine the central moment in this contour. After adding only cv :: findContours to the code, I found strange behavior in the output, after which I wanted to check what happens with the original 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 that, the output shows that there is a difference between threshedImage and contourImage. How is this possible? Does anyone have similar results with cv :: findContours?

+7
source share
1 answer

Wrong! The documents clearly indicate that:

The original image is changed by this function.

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

+5
source

All Articles