Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Detect contours. Cycle through contours and build rectangle, which contour fit.

Use some CV lib for this task, for example OpenCV (highly recommeded). You will need do something like that:

// contours of targets
std::vector<std::vector<cv::Point> > contours;

// get contours from binary image
cv::findContours(binImg,contours,CV_RETR_TREE,CV_CHAIN_APPROX_SIMPLE);

// poly contours
std::vector<std::vector<cv::Point> > contours_poly(contours.size());

// bounded rectangles
std::vector<cv::Rect> boundRect(contours.size());

for (int i=0; i<contours.size(); i++) {
    // get polyDPcontour and bound rectangle of it
        cv::approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 3, true);
        boundRect[i] = cv::boundingRect(cv::Mat(contours_poly[i]));
}