#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
char c = 0;
VideoCapture capture(0);//打开摄像头
while(1)
{
Mat frame;
capture >>frame;
Mat midImage;
cvtColor(frame,midImage, CV_BGR2GRAY);//转化为灰度图
GaussianBlur( midImage, midImage, Size(9, 9), 2, 2 );//进行霍夫圆变换
vector<Vec3f> circles;
HoughCircles( midImage, circles, CV_HOUGH_GRADIENT,1.5, 10, 200, 100, 0, 0 );//霍夫圆变换
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);//绘制圆心
circle( frame, center, 3, Scalar(0,255,0), -1, 8, 0 );//打印圆心坐标
circle( frame, center, radius, Scalar(255,50,255), 3, 8, 0 );//绘制圆轮廓
printf("x = %d,y = %d\n",cvRound(circles[i][0]),cvRound(circles[i][1]));}
namedWindow("检测结果");
imshow("检测结果", frame);
c = cvWaitKey(30);
if (c == 27)
{
break;
}
}
return 0;
}
代码有缺陷,霍夫圆测量并不准确