霍夫圆检测。
从直角坐标系转为极坐标系后圆心比较亮。
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace std;
using namespace cv;
Mat src, dst;
Mat gray_img;
int main(){
// Mat src1;
// src1 = imread("/Users/ming/Documents/test.jpg");
// resize(src1, src, Size(src1.cols/2, src1.rows/2));
src = imread("/Users/ming/Documents/win7logo.jpeg");
src.copyTo(dst);
if (!src.data){
printf("cannot load image ...");
return -1;
}
namedWindow("src img", CV_WINDOW_AUTOSIZE);
imshow("src img", src);
medianBlur(src, src, 3);
// imshow("medianBulr", src);
cvtColor(src, gray_img, CV_WINDOW_AUTOSIZE);
Mat canny_img;
Canny(gray_img, canny_img, 80, 200);
imshow("canny img", canny_img);
//霍夫圆检测
vector<Vec3f> pcircle;
HoughCircles(canny_img, pcircle, CV_HOUGH_GRADIENT, 1.0, 5);
// HoughCircles(canny_img, pcircle, CV_HOUGH_GRADIENT, 1, gray_img.rows / 5, 150, 70, 0, 0);
Scalar color = Scalar(0, 0, 255);
for (size_t i = 0; i < pcircle.size(); i++){
Vec3f temp = pcircle[i];
cout<<"circle: "<< temp << endl;
circle(dst, Point(temp[0], temp[1]), temp[2], color); //画圆
circle(dst, Point(temp[0], temp[1]), 2, Scalar(255,0,0)); //画圆心
}
imshow("houghcircle", dst);
waitKey(0);
return 0;
}