Advertisement
Guest User

Untitled

a guest
Jan 20th, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <opencv2/highgui/highgui.hpp>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. using namespace cv;
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     Mat original = imread("res.jpg",0);//0 because we need a grayscale image by default
  13.     Mat modified;
  14.     threshold(original, modified,200 ,255, CV_8UC1);//this is called a threshold function //8UC1 means single channel grey scale image(200 for center and 255 for outer circle)
  15.     float sumx=0, sumy=0;
  16.     float num_pixel = 0;
  17.     for(int x=0; x<modified.cols; x++) {
  18.         for(int y=0; y<modified.rows; y++) {
  19.             int val = modified.at<uchar>(y,x);// a single channel grey scale image (type 8UC1) and pixel coordinates x and y
  20.             if( val>0) {//after thresholding,the picture will become black and white. Therefore,we will easily identify the white pixels(255) with the black pixels(0)
  21.  
  22.                 sumx += x;
  23.                 sumy += y;
  24.                 num_pixel++;
  25.             }
  26.         }
  27.     }
  28.     Point p(sumx/num_pixel, sumy/num_pixel);
  29.  
  30.     Moments m = moments(modified, false);
  31.     Point p1(m.m10/m.m00, m.m01/m.m00);
  32.     cout << Mat(p1) << endl;
  33.  
  34.     circle(modified, p, 5, Scalar(128,0,0), -1); //5 is the size of the centroid to be displayed //Scalar(red,green,blue)
  35.    
  36.     namedWindow("before",WINDOW_NORMAL);
  37.     imshow("before",original);
  38.     namedWindow("after",WINDOW_NORMAL);
  39.     imshow("after", modified);
  40.     waitKey(0);
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement