1:
2: for (int i = 0; i < contours.size(); i++) { 3: cout << "contour # " << i << endl;
4: imgMask = Mat::zeros(imgSrc.size(), CV_8UC1);
5: drawContours(imgMask, contours, i, Scalar(255), -1);
6: Mat imgROI;
7: imgSrc.copyTo(imgROI, imgMask);
8: string name = std::to_string(i);
9: //imshow(name, imgROI);
10: cout << "mean color (BGR) = " << mean(imgSrc, imgMask) << endl;
11: Moments m = moments(contours[i]);
12: cout << "area = " << m.m00 <<endl;
13: double contArea = contourArea(contours[i]);
14: cout << "contour area = " << contArea << endl;
15: double cx = m.m10 / m.m00;
16: double cy = m.m01 / m.m00;
17: line(imgROI, Point(cx, cy), Point(cx, cy), Scalar(255, 0, 0), 5, 8);
18: char label[50];
19: sprintf_s(label, "(%.0f,%.0f)", cx, cy);
20: int offset = 100;
21: putText(imgROI, label, Point(cx-offset, cy), FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 1, 8);
22: double perimeter = arcLength(contours[i], true);
23: cout << "perimeter = " << perimeter << endl;
24: vector<Point> polyCurves;
25: double epsilon = 0.01;
26: approxPolyDP(contours[i], polyCurves, epsilon*arcLength(contours[i], true), true);
27:
28: cout << "polygonal curves = " << polyCurves.size() << endl;
29: for (int j = 0; j < polyCurves.size()-1; j++)
30: { 31: Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
32: line(imgROI, polyCurves[j], polyCurves[j + 1], color, 3, 8);
33: }
34:
35: line(imgROI, polyCurves[0], polyCurves[polyCurves.size()-1], Scalar(0, 0,255), 3, 8);
36:
37: imshow(name, imgROI);
38: cout << "------------------------------" << endl;
39: }