single_blob.cpp 813 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include "opencv2/highgui/highgui.hpp"
  2. #include "opencv2/imgproc/imgproc.hpp"
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. using namespace cv;
  7. using namespace std;
  8. int main(int argc, char** argv)
  9. {
  10. // Declare Mat type images
  11. Mat src, gray,thr;
  12. //Load source image, convert it to gray
  13. src = imread(argv[1], 1 );
  14. // convert image to grayscale
  15. cvtColor( src, gray, COLOR_BGR2GRAY );
  16. // convert grayscale to binary image
  17. threshold( gray, thr, 100,255,THRESH_BINARY );
  18. // find moments of the image
  19. Moments m = moments(thr,true);
  20. Point p(m.m10/m.m00, m.m01/m.m00);
  21. // coordinates of centroid
  22. cout<< Mat(p)<< endl;
  23. // show the image with a point mark at the centroid
  24. circle(src, p, 5, Scalar(128,0,0), -1);
  25. imshow("Image with center",src);
  26. waitKey(0);
  27. }