Advertisement
Guest User

Untitled

a guest
Jan 21st, 2014
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <opencv2/opencv.hpp>
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <vector>
  6.  
  7. using namespace cv;
  8. using namespace std;
  9.  
  10.  
  11.  
  12. int main()
  13. {
  14.     Mat original = imread("res.jpg",0);//0 because we need a grayscale image by default
  15.     Mat modified;
  16.     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)
  17.    
  18.     vector<vector<Point>> contours;
  19.     //Here I find all the contours in my image
  20.     findContours(modified.clone(),contours,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
  21.    
  22.     vector<Point2f> centers;
  23.     for( ; idx >= 0; idx = hierarchy[idx][0] )
  24.     {
  25.         //Then I draw them one by one on a different image and find their centers
  26.         Mat dst(original.rows, original.cols, CV_8UC1, Scalar(0,0,0));
  27.         drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
  28.         unsigned int nbPoints = 0; //this to count the nb of points of the current circle
  29.         Point2f currentCenter(0,0); //This will contain the coordinates of the current center, that is the mean of all the points of the circle
  30.        
  31.         //I go through the image to find the points of the circle
  32.         for(int x=0; x<dst.cols; x++) {
  33.             for(int y=0; y<dst.rows; y++) {
  34.                 int val = dst.at<uchar>(y,x);// a single channel grey scale image (type 8UC1) and pixel coordinates x and y
  35.                 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)     
  36.                   nbPoints++; //I increment because i found a point
  37.                   currentCenter += Point2f(x,y); //I add the point to the center
  38.                 }
  39.             }
  40.         }
  41.         currentCenter /= nbPoints; //I divide by the number of points to have a mean
  42.         //I don't know what you want to do but here in current center you have the center of the current circle
  43.         centers.push_back(); // I add it in a vector
  44.     }
  45.    
  46.     //In centers you know have every center.
  47.    
  48.     waitKey(0);
  49.     return 0;
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement