Ask Your Question
0

find contours in an image [closed]

asked 2016-01-05 08:57:14 -0500

elp14sma gravatar image

Hi

I want to detect contours in this image below , so that the output is like the second image

shuold I use canny detection , how can I get this output

Thanks

Input image

output image

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by elp14sma
close date 2016-01-06 03:50:43.540309

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-01-05 09:18:55 -0500

thdrksdfthmn gravatar image

updated 2016-01-05 10:31:44 -0500

If you already have a binary image, then you need just to do findContours, then for each contour you can do a boundingRect:

std::vector< std::vector< cv::Point > > contours;
std::vector< cv::Vec4i > hierarchy;

cv::findContours(threshold_output, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE, cv::Point(0, 0));

std::vector< std::vector< cv::Point > > contours_poly(contours.size());
std::vector< cv::Rect > boundRect(contours.size());

for(int i = 0; i < contours.size(); i++)
{
    cv::approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 3, true);
    cv::boundRect[i] = cv::boundingRect(cv::Mat(contours_poly[i]));
}

like in this tutorial


What you can do is to play a little with the flags of the functions:

cv::threshold(image, threshold_output, thr, 255, cv::THRESH_BINARY_INV);

And when searching for contours, then use

cv::findContours(threshold_output, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE, Point(0, 0));

For better understanding the findContours functions see this

edit flag offensive delete link more

Comments

Thanks for answer , I have made some changes to the code in the tutorial :

code

the out was like this image

how can I remove the pink border around the image and the black pixels in the original image showing only borders

elp14sma gravatar imageelp14sma ( 2016-01-05 09:39:23 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-01-05 08:57:14 -0500

Seen: 224 times

Last updated: Jan 05 '16