#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: |
-->Here, our input image is 'img_gray'
-->vector<Vec3f> circles;
-->This vector stores data of circles detected
-->Not relevant here.
-->Detection Method
-->In our case, dp=1;
-->In our case, minDist=10;
-->In our case, param1=100;
-->In our case, param2=thresh; which is what we are varying from trackbar.
-->Not relevant here.
You may go through the documentation HERE.
|
|---|
One more point:
cvRound() Rounds floating-point number to the nearest integer.
cvRound() Rounds floating-point number to the nearest integer.
.
No comments:
Post a Comment