Ask Your Question

How to get to inside contour in c++,opencv

asked 2017-02-02 02:55:01 -0500

KBH gravatar image

Hello, I have a one question.

I want to use to inside contour areas.

from my program, i did like this

"original image -> grayscale image -> gaussianblur -> Canny -> findcontours".

But I don't know how to get to inside contour pixel data segment.

if I find many contours, i want covert it into "inside contours area" and "outside contours area".

Moreover, I used Visual Studio 2013 and opencv 2.4.11.

Hoping for your favorable actions.

Thank you very much.

Best Regards


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-02-02 05:59:30 -0500

pklab gravatar image

Welcome on board @KBH !

Supposing you are talking about closed contours, you can create masks for your contours with drawContours using thickness=cv::FILLED to fill the area bounded by the contours.

cv::Mat mask = cv::zeros(edges.Size(),cv::CV_8UC1);
for(size_t i;i<contours.size();i++)
    cv::drawContours( mask, contours, i, cv::Scalar(255,255,255), cv::FILLED);

Then, if you need the index points of INSIDE areas you can use findNonZero on your mask

std::vector<cv::Point> insidePoints;   // output, locations of non-zero pixels
cv::findNonZero(mask, locations);

finally, if you need the index points of OUTSIDE areas you can invert your mask than repeat findNonZero

mask = 255-mask;
std::vector<cv::Point> outsidePoints;
cv::findNonZero(mask, outsidePoints);
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-02-02 02:54:22 -0500

Seen: 3,855 times

Last updated: Feb 02 '17