Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

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

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

like in this tutorial

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

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

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

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

for(int i = 0; i < contours.size(); i++)
{
    approxPolyDP(Mat(contours[i]), cv::approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 3, true);
    boundRect[i] cv::boundRect[i] = boundingRect(Mat(contours_poly[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_TREE, cv::CHAIN_APPROX_SIMPLE, Point(0, 0));

For better understanding the findContours functions see this

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_TREE, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE, Point(0, 0));

For better understanding the findContours functions see this