1 /**
2  * @function moments_demo.cpp
3  * @brief Demo code to calculate moments
4  * @author OpenCV team
5  */
6 
7 #include "opencv2/imgcodecs.hpp"
8 #include "opencv2/highgui/highgui.hpp"
9 #include "opencv2/imgproc/imgproc.hpp"
10 #include <iostream>
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 using namespace cv;
15 using namespace std;
16 
17 Mat src; Mat src_gray;
18 int thresh = 100;
19 int max_thresh = 255;
20 RNG rng(12345);
21 
22 /// Function header
23 void thresh_callback(int, void* );
24 
25 /**
26  * @function main
27  */
main(int,char ** argv)28 int main( int, char** argv )
29 {
30   /// Load source image and convert it to gray
31   src = imread( argv[1], 1 );
32 
33   /// Convert image to gray and blur it
34   cvtColor( src, src_gray, COLOR_BGR2GRAY );
35   blur( src_gray, src_gray, Size(3,3) );
36 
37   /// Create Window
38   const char* source_window = "Source";
39   namedWindow( source_window, WINDOW_AUTOSIZE );
40   imshow( source_window, src );
41 
42   createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
43   thresh_callback( 0, 0 );
44 
45   waitKey(0);
46   return(0);
47 }
48 
49 /**
50  * @function thresh_callback
51  */
thresh_callback(int,void *)52 void thresh_callback(int, void* )
53 {
54   Mat canny_output;
55   vector<vector<Point> > contours;
56   vector<Vec4i> hierarchy;
57 
58   /// Detect edges using canny
59   Canny( src_gray, canny_output, thresh, thresh*2, 3 );
60   /// Find contours
61   findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
62 
63   /// Get the moments
64   vector<Moments> mu(contours.size() );
65   for( size_t i = 0; i < contours.size(); i++ )
66      { mu[i] = moments( contours[i], false ); }
67 
68   ///  Get the mass centers:
69   vector<Point2f> mc( contours.size() );
70   for( size_t i = 0; i < contours.size(); i++ )
71      { mc[i] = Point2f( static_cast<float>(mu[i].m10/mu[i].m00) , static_cast<float>(mu[i].m01/mu[i].m00) ); }
72 
73   /// Draw contours
74   Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
75   for( size_t i = 0; i< contours.size(); i++ )
76      {
77        Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
78        drawContours( drawing, contours, (int)i, color, 2, 8, hierarchy, 0, Point() );
79        circle( drawing, mc[i], 4, color, -1, 8, 0 );
80      }
81 
82   /// Show in a window
83   namedWindow( "Contours", WINDOW_AUTOSIZE );
84   imshow( "Contours", drawing );
85 
86   /// Calculate the area with the moments 00 and compare with the result of the OpenCV function
87   printf("\t Info: Area and Contour Length \n");
88   for( size_t i = 0; i< contours.size(); i++ )
89      {
90        printf(" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f \n", (int)i, mu[i].m00, contourArea(contours[i]), arcLength( contours[i], true ) );
91        Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
92        drawContours( drawing, contours, (int)i, color, 2, 8, hierarchy, 0, Point() );
93        circle( drawing, mc[i], 4, color, -1, 8, 0 );
94      }
95 }
96