1 Mat cannyImage; 2 /// Detect edges using canny 3 Canny(src, cannyImage, 125, 250, 3); 4 vector<vector<Point> > contours; 5 vector<Vec4i> hierarchy; 6 /// Find contours 7 findContours(cannyImage, contours, hierarchy, CV_RETR_EXTERNAL /* CV_RETR_EXTERNAL CV_RETR_LIST CV_RETR_CCOMP RETR_TREE CV_RETR_FLOODFILL*/, CHAIN_APPROX_SIMPLE, Point(0, 0)); 8 9 /// Approximate contours to polygons + get bounding rects and circles 10 vector<vector<Point> > contours_poly(contours.size()); 11 vector<Rect> boundRect(contours.size()); 12 13 for (size_t i = 0; i < contours.size(); i++) 14 { 15 approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true); 16 boundRect[i] = boundingRect(Mat(contours_poly[i])); 17 }