Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- using namespace cv;
- using namespace std;
- int main()
- {
- Mat original = imread("res.jpg",0);//0 because we need a grayscale image by default
- Mat modified;
- 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)
- float sumx=0, sumy=0;
- float num_pixel = 0;
- for(int x=0; x<modified.cols; x++) {
- for(int y=0; y<modified.rows; y++) {
- int val = modified.at<uchar>(y,x);// a single channel grey scale image (type 8UC1) and pixel coordinates x and y
- 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)
- sumx += x;
- sumy += y;
- num_pixel++;
- }
- }
- }
- Point p(sumx/num_pixel, sumy/num_pixel);
- Moments m = moments(modified, false);
- Point p1(m.m10/m.m00, m.m01/m.m00);
- cout << Mat(p1) << endl;
- circle(modified, p, 5, Scalar(128,0,0), -1); //5 is the size of the centroid to be displayed //Scalar(red,green,blue)
- namedWindow("before",WINDOW_NORMAL);
- imshow("before",original);
- namedWindow("after",WINDOW_NORMAL);
- imshow("after", modified);
- waitKey(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement