Ask Your Question

The contours in drawcontours is closed or not?

asked 2013-06-18 08:14:55 -0500

wuling gravatar image

updated 2013-06-18 08:17:06 -0500

Hi all, this is my test picture:

image description

As i know, if want to calculate the area or others , the contours should be closed. But I test findcontours and drawcontours , i cann't verify. The following code is true or not. Thanks.

  Mat temp,blob;
    cv::findContours(hole.clone(),contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
    for(int idx=0; idx<contours.size(); ++idx)    
            drawContours(blob, contours, idx, Scalar::all(255), CV_FILLED, 8, hierarchy);       

            for(int i=0;i<contours[idx].size();i++)
                cv::Point p=contours[idx][i];


Finally I get:

image description---->result.bmp

image description--->Contours(temp.bmp) is not closed.

edit retag flag offensive close merge delete



you should use CV_CHAIN_APPROX_NONE flag

cv::findContours(hole.clone(),contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
sturkmen gravatar imagesturkmen ( 2016-09-02 09:46:37 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-06-18 10:15:54 -0500

berak gravatar image

drawContours() actually only draws the points, not line segments.

if you want that, try:

std::vector<cv::Point> contour_line;
cv::approxPolyDP(cv::Mat(contours[i]), contour_line, 5, true); // last arg is "closed line" 
int nc = contour_line.size();
const cv::Point * pc = &(contour_line[0]);
cv::polylines( img, &pc, &nc, 1, 0, cv::Scalar(255) );
edit flag offensive delete link more


Hi berak,in fact i find out the output of the contourarea is not extract correct. I use cvblob & Commercial Software ,the answer all is 975 ,but the contourarea is 925. so I check the countours.But i find it 's ok. so...............finally, i use cvblob to calcuate blob's area, and hole's area.

wuling gravatar imagewuling ( 2013-06-18 22:11:12 -0500 )edit

Question Tools


Asked: 2013-06-18 08:14:55 -0500

Seen: 2,437 times

Last updated: Jun 18 '13