moments_demo.cpp 2.9 KB

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