find contours in an image [closed]
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
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
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
Asked: 2016-01-05 08:57:14 -0600
Seen: 314 times
Last updated: Jan 05 '16