Sunday, 13 April 2014

#11b. Hough Transform: Detecting Circles


#include<iostream>
#include<highgui.h>
#include<cv.h>
using namespace std;
using namespace cv;
Mat img;

int thresh=100; //accumulator threshold;

void on_trackbar(int, void *){
 Mat img_gray;
 //convert image to grayscale
 cvtColor(img,img_gray,CV_RGB2GRAY);

 //create a 3 element floating vector
 //this vector stores 3 floating values:
 //[0]:x-value of center of to-be-detected circle
 //[1]:y-value of center of to-be-detected circle
 //[2]:radius of to-be-detected circle
 vector<Vec3f> circles;

 //see text for explanation of following line
 HoughCircles(img_gray,circles,CV_HOUGH_GRADIENT,1,10,100,thresh,5);

 Mat img_show=img.clone();

 for(int i=0;i<circles.size();i++){
  Point center(cvRound(circles[i][0]),cvRound(circles[i][1]));
  int radius=cvRound(circles[i][2]);

  //draw center of the detected circle
  circle(img_show,center,3,Scalar(0,0,255),-1);

  //draw the outline of circle
  circle(img_show,center,radius,Scalar(0,0,255),3,8,0);
 }
 imshow("Shapes",img_show);
}
int main()
{
 //read image
 img =imread("images/linescircles.jpg");
 namedWindow("Shapes");
 imshow("Shapes",img);

 //create trackbar for threshold
 //threshold defines the minimum circle radius to be detected
 //see text for further explanation
 createTrackbar("Acc. threshold","Shapes",&thresh,300,on_trackbar);

 //initialize the window
 on_trackbar(0,0);

 while(char(waitKey(0))!='q'){}
 return 0;
}

HoughCircles(img_gray,circles,CV_HOUGH_GRADIENT,1,10,100,thresh,5);

Format:
HoughCircles(InputArray image, OutputArray circles, int method, double dp, double minDist, double param1=100, double param2=100, intminRadius=0, int maxRadius=0 )
Parameters:
  • image – 8-bit, single-channel, grayscale input image.
-->Here, our input image is 'img_gray'

  • circles – Output vector of found circles. Each vector is encoded as a 3-element floating-point vector(x, y, radius) .
-->vector<Vec3f> circles;
-->This vector stores data of circles detected

  • circle_storage – In C function this is a memory storage that will contain the output sequence of found circles.
-->Not relevant here.

  • method – Detection method to use. Currently, the only implemented method is CV_HOUGH_GRADIENT , which is basically 21HT , described in[Yuen90].
-->Detection Method

  • dp – Inverse ratio of the accumulator resolution to the image resolution. For example, ifdp=1 , the accumulator has the same resolution as the input image. If dp=2 , the accumulator has half as big width and height.
-->In our case, dp=1;

  • minDist – Minimum distance between the centers of the detected circles. If the parameter is too small, multiple neighbor circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed.
-->In our case, minDist=10;

  • param1 – First method-specific parameter. In case of CV_HOUGH_GRADIENT , it is the higher threshold of the two passed to the Canny() edge detector (the lower one is twice smaller).
-->In our case, param1=100;

  • param2 – Second method-specific parameter. In case of CV_HOUGH_GRADIENT , it is the accumulator threshold for the circle centers at the detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding to the larger accumulator values, will be returned first.
-->In our case, param2=thresh; which is what we are varying from trackbar. 


  • minRadius – Minimum circle radius.
  • maxRadius – Maximum circle radius.
-->Not relevant here.


You may go through the documentation HERE.

One more point:
cvRound() Rounds floating-point number to the nearest integer.

No comments:

Post a Comment