Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <opencv2/opencv.hpp>
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <vector>
- 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)
- vector<vector<Point>> contours;
- //Here I find all the contours in my image
- findContours(modified.clone(),contours,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
- vector<Point2f> centers;
- for( ; idx >= 0; idx = hierarchy[idx][0] )
- {
- //Then I draw them one by one on a different image and find their centers
- Mat dst(original.rows, original.cols, CV_8UC1, Scalar(0,0,0));
- drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
- unsigned int nbPoints = 0; //this to count the nb of points of the current circle
- Point2f currentCenter(0,0); //This will contain the coordinates of the current center, that is the mean of all the points of the circle
- //I go through the image to find the points of the circle
- for(int x=0; x<dst.cols; x++) {
- for(int y=0; y<dst.rows; y++) {
- int val = dst.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)
- nbPoints++; //I increment because i found a point
- currentCenter += Point2f(x,y); //I add the point to the center
- }
- }
- }
- currentCenter /= nbPoints; //I divide by the number of points to have a mean
- //I don't know what you want to do but here in current center you have the center of the current circle
- centers.push_back(); // I add it in a vector
- }
- //In centers you know have every center.
- waitKey(0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement