Ask Your Question
0

FindContours behaves unexpectedly

asked 2014-11-06 02:50:10 -0600

bupthebroker gravatar image

updated 2017-03-10 02:54:00 -0600

Hi, i am trying to find the contours of a white letter on black background. cv::findContours(img_threshold, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

The problem is that when i display the output Mat (img_threshold) i can see that it only detects contours partially. I cannot draw a bounding rectangle based on such contours.

Left side: original Right side: detected contours

image description

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2014-11-06 03:27:34 -0600

berak gravatar image

updated 2014-11-06 03:37:17 -0600

this is probably an 'expectation mismatch'.

the outcome of findContours is not your img_threshold, but the contours vector.

if you care to look at the docs again you will see: "The function modifies the image while extracting the contours." again, this is just a by-product, not the desired result.

if you need your img_threshold for further operations, supply a clone() for findContours:

vector<vector<Point>>contours;
cv::findContours(img_threshold.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

// let's draw some things, assuming, Mat ocv is the original rgb image:
drawContours(ocv, contours, 0, Scalar(200,0,0), 2);
rectangle(ocv, boundingRect(contours[0]), Scalar(0,0,200), 2);

image description

edit flag offensive delete link more
0

answered 2014-11-06 03:13:06 -0600

juanmanpr gravatar image

I think the image you are refering to is the Canny output and I think is only showing the positive values. Why dont you use the contours in your vector<vector<Point> > contours? You can plot them with cv::drawContours and you can find the bounding box by using cv::boundingRect. You may refer to this tutorial: http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html

edit flag offensive delete link more

Comments

Whoa, didn't expect such quick responses. Thanks, that clears it up and i managed to draw my bounding rect.

It's a bit strange though that it fills the original Mat with semi-garbage.

bupthebroker gravatar imagebupthebroker ( 2014-11-06 03:30:55 -0600 )edit

Question Tools

Stats

Asked: 2014-11-06 02:50:10 -0600

Seen: 6,999 times

Last updated: Nov 06 '14