- 例子:摄像头实时检测圆形物体的中心(用的霍夫圆变换)
#include <opencv2\opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
VideoCapture capture(1);
while (1)
{
Mat frame;
Mat midImage, dstImage;
capture >> frame;
cvtColor(frame, midImage, COLOR_BGR2GRAY);
GaussianBlur(midImage, midImage, Size(9, 9), 2, 2);
vector<Vec3f> circles;
HoughCircles(midImage, circles, 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(155, 50, 255), 3, 8, 0);
}
imshow("读取视频", frame);
waitKey(10);
}
return 0;
}