2011-06-29 03:32:48 +08:00
|
|
|
/**
|
|
|
|
* @file HoughCircle_Demo.cpp
|
|
|
|
* @brief Demo code for Hough Transform
|
|
|
|
* @author OpenCV team
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
2013-11-11 13:55:12 +08:00
|
|
|
#include <iostream>
|
2011-06-29 03:32:48 +08:00
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
|
2013-11-11 13:47:54 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// windows and trackbars name
|
|
|
|
const std::string windowName = "Hough Circle Detection Demo";
|
|
|
|
const std::string cannyThresholdTrackbarName = "Canny threshold";
|
|
|
|
const std::string accumulatorThresholdTrackbarName = "Accumulator Threshold";
|
|
|
|
|
|
|
|
// initial and max values of the parameters of interests.
|
|
|
|
const int cannyThresholdInitialValue = 200;
|
|
|
|
const int accumulatorThresholdInitialValue = 50;
|
|
|
|
const int maxAccumulatorThreshold = 200;
|
|
|
|
const int maxCannyThreshold = 255;
|
|
|
|
|
|
|
|
void HoughDetection(const Mat& src_gray, const Mat& src_display, int cannyThreshold, int accumulatorThreshold)
|
|
|
|
{
|
2013-11-13 15:21:09 +08:00
|
|
|
// will hold the results of the detection
|
|
|
|
std::vector<Vec3f> circles;
|
|
|
|
// runs the actual detection
|
2013-11-28 10:42:44 +08:00
|
|
|
HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, cannyThreshold, accumulatorThreshold, 0, 0 );
|
2013-11-11 13:47:54 +08:00
|
|
|
|
2013-11-13 15:21:09 +08:00
|
|
|
// clone the colour, input image for displaying purposes
|
|
|
|
Mat display = src_display.clone();
|
|
|
|
for( size_t i = 0; i < circles.size(); i++ )
|
|
|
|
{
|
2013-11-11 13:47:54 +08:00
|
|
|
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
|
|
|
|
int radius = cvRound(circles[i][2]);
|
|
|
|
// circle center
|
|
|
|
circle( display, center, 3, Scalar(0,255,0), -1, 8, 0 );
|
|
|
|
// circle outline
|
|
|
|
circle( display, center, radius, Scalar(0,0,255), 3, 8, 0 );
|
2013-11-13 15:21:09 +08:00
|
|
|
}
|
2013-11-11 13:47:54 +08:00
|
|
|
|
2013-11-13 15:21:09 +08:00
|
|
|
// shows the results
|
|
|
|
imshow( windowName, display);
|
2013-11-11 13:47:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-07 22:21:20 +08:00
|
|
|
int main(int, char** argv)
|
2011-06-29 03:32:48 +08:00
|
|
|
{
|
2013-11-13 15:21:09 +08:00
|
|
|
Mat src, src_gray;
|
2011-06-29 03:32:48 +08:00
|
|
|
|
2013-11-11 13:47:54 +08:00
|
|
|
// Read the image
|
|
|
|
src = imread( argv[1], 1 );
|
2011-06-29 03:32:48 +08:00
|
|
|
|
2013-11-13 15:21:09 +08:00
|
|
|
if( !src.data )
|
|
|
|
{
|
|
|
|
std::cerr<<"Invalid input image\n";
|
|
|
|
std::cout<<"Usage : tutorial_HoughCircle_Demo <path_to_input_image>\n";
|
|
|
|
return -1;
|
|
|
|
}
|
2011-06-29 03:32:48 +08:00
|
|
|
|
2013-11-11 13:47:54 +08:00
|
|
|
// Convert it to gray
|
2013-10-30 20:34:27 +08:00
|
|
|
cvtColor( src, src_gray, COLOR_BGR2GRAY );
|
2011-06-29 03:32:48 +08:00
|
|
|
|
2013-11-11 13:47:54 +08:00
|
|
|
// Reduce the noise so we avoid false circle detection
|
2011-06-29 03:32:48 +08:00
|
|
|
GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );
|
|
|
|
|
2013-11-11 13:47:54 +08:00
|
|
|
//declare and initialize both parameters that are subjects to change
|
|
|
|
int cannyThreshold = cannyThresholdInitialValue;
|
|
|
|
int accumulatorThreshold = accumulatorThresholdInitialValue;
|
2011-06-29 03:32:48 +08:00
|
|
|
|
2013-11-11 13:47:54 +08:00
|
|
|
// create the main window, and attach the trackbars
|
|
|
|
namedWindow( windowName, WINDOW_AUTOSIZE );
|
|
|
|
createTrackbar(cannyThresholdTrackbarName, windowName, &cannyThreshold,maxCannyThreshold);
|
|
|
|
createTrackbar(accumulatorThresholdTrackbarName, windowName, &accumulatorThreshold, maxAccumulatorThreshold);
|
2011-06-29 03:32:48 +08:00
|
|
|
|
2013-11-13 15:13:47 +08:00
|
|
|
// infinite loop to display
|
2013-11-11 13:47:54 +08:00
|
|
|
// and refresh the content of the output image
|
2013-11-13 15:13:47 +08:00
|
|
|
// until the user presses q or Q
|
2013-11-11 13:47:54 +08:00
|
|
|
int key = 0;
|
|
|
|
while(key != 'q' && key != 'Q')
|
2011-06-29 03:32:48 +08:00
|
|
|
{
|
2013-11-11 13:47:54 +08:00
|
|
|
// those paramaters cannot be =0
|
|
|
|
// so we must check here
|
|
|
|
cannyThreshold = std::max(cannyThreshold, 1);
|
|
|
|
accumulatorThreshold = std::max(accumulatorThreshold, 1);
|
|
|
|
|
|
|
|
//runs the detection, and update the display
|
|
|
|
HoughDetection(src_gray, src, cannyThreshold, accumulatorThreshold);
|
2011-06-29 03:32:48 +08:00
|
|
|
|
2013-11-11 13:47:54 +08:00
|
|
|
// get user key
|
|
|
|
key = waitKey(10);
|
|
|
|
}
|
2011-06-29 03:32:48 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|