1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace cv; using namespace std;
#define WINDOW_NAME1 "Original" #define WINDOW_NAME2 "Result"
Mat g_srcImage; Mat g_grayImage; int g_nThresh = 100; int g_nMaxThresh = 255; RNG g_rng(12345); Mat g_cannyMat_output; vector<vector<Point>> g_vContours; vector<Vec4i> g_vHierarchy;
void on_ThreshChange(int, void *);
int main(int argc, char **argv) { g_srcImage = imread("1.jpg", 1);
cvtColor(g_srcImage, g_grayImage, COLOR_BGR2GRAY); blur(g_grayImage, g_grayImage, Size(3, 3));
namedWindow(WINDOW_NAME1, WINDOW_AUTOSIZE); imshow(WINDOW_NAME1, g_srcImage);
createTrackbar(" 阈值", WINDOW_NAME1, &g_nThresh, g_nMaxThresh, on_ThreshChange); on_ThreshChange(0, 0);
waitKey(0); return (0); }
void on_ThreshChange(int, void *) { Canny(g_grayImage, g_cannyMat_output, g_nThresh, g_nThresh * 2, 3);
findContours(g_cannyMat_output, g_vContours, g_vHierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<Moments> mu(g_vContours.size()); for (unsigned int i = 0; i < g_vContours.size(); i++) { mu[ i ] = moments(g_vContours[ i ], false); }
vector<Point2f> mc(g_vContours.size()); for (unsigned int i = 0; i < g_vContours.size(); i++) { mc[ i ] = Point2f(static_cast<float>(mu[ i ].m10 / mu[ i ].m00), static_cast<float>(mu[ i ].m01 / mu[ i ].m00)); }
Mat drawing = Mat::zeros(g_cannyMat_output.size(), CV_8UC3); for (unsigned int i = 0; i < g_vContours.size(); i++) { Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)); drawContours(drawing, g_vContours, i, color, 2, 8, g_vHierarchy, 0, Point()); circle(drawing, mc[ i ], 4, color, -1, 8, 0); ; }
namedWindow(WINDOW_NAME2, WINDOW_AUTOSIZE); imshow(WINDOW_NAME2, drawing);
printf("\t 输出内容: 面积和轮廓长度\n"); for (unsigned int i = 0; i < g_vContours.size(); i++) { printf(" >通过m00计算出轮廓[%d]的面积: (M_00) = %.2f \n OpenCV函数计算出的面积=%.2f , " "长度: %.2f \n\n", i, mu[ i ].m00, contourArea(g_vContours[ i ]), arcLength(g_vContours[ i ], true)); Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)); drawContours(drawing, g_vContours, i, color, 2, 8, g_vHierarchy, 0, Point()); circle(drawing, mc[ i ], 4, color, -1, 8, 0); } }
|