霍夫圆变换:
1 #include <opencv2\opencv.hpp> 2 #include <iostream> 3 #include <string> 4 #include <vector> 5 6 #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) 7 8 using namespace std; 9 using namespace cv; 10 11 void showImg(const string &win_name, const Mat &img) 12 { 13 namedWindow(win_name, CV_WINDOW_AUTOSIZE); 14 imshow(win_name, img); 15 } 16 17 int main(void) 18 { 19 Mat src = imread("panzi.jpg"); 20 if (src.empty()) 21 return -1; 22 Mat dst; 23 cvtColor(src, dst, CV_BGR2GRAY); 24 showImg("dst", dst); 25 GaussianBlur(dst, dst, Size(9, 9), 2, 2); 26 vector<Vec3f> circles; 27 HoughCircles(dst, circles, CV_HOUGH_GRADIENT, 1, dst.rows / 8, 200, 100, 0, 0); 28 for (size_t i = 0; i < circles.size(); ++i) 29 { 30 Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); 31 int radius = cvRound(circles[i][2]); 32 circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);//绘制圆心 33 circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);//绘制圆 34 } 35 showImg("after ciecle decete", src); 36 waitKey(); 37 return 0; 38 }
检测结果:
可见左侧的盘子没有被检出,但是却不知道是怎么回事,可能左侧不是正圆
分割检测的结果:
右侧:
左侧:
,因为用肉眼无法分辨,所以上述结果不好确定。
以上。
时间: 2024-11-05 22:33:14