x

 

 

 

 

 

 

 1 #include <opencv2/opencv.hpp>
 2 #include <iostream>
 3 #include <math.h>
 4 
 5 using namespace cv;
 6 using namespace std;
 7 
 8 
 9 int main(int argc, char** argv)
10 {
11     Mat src, src_gray, dst;
12     src = imread("test1.jpg");
13 
14     char INPUT_TITLE[] = "input image";
15 
16     imshow(INPUT_TITLE, src);
17 
18     Canny(src, src_gray, 150, 200);
19     cvtColor(src_gray, dst, CV_GRAY2BGR);
20     imshow("edge image", src_gray);
21     imshow("gray", dst);
22     
23     //方法1(标准霍夫变换)
24     //vector<Vec2f> lines;
25     //HoughLines(src_gray, lines, 1, CV_PI / 180, 150, 0, 0);
26     //for (size_t i = 0; i < lines.size(); i++) {
27     //    float rho = lines[i][0]; // 极坐标中的r长度
28     //    float theta = lines[i][1]; // 极坐标中的角度
29     //    Point pt1, pt2;
30     //    double a = cos(theta), b = sin(theta);
31     //    double x0 = a * rho, y0 = b * rho;
32     //    // 转换为平面坐标的四个点
33     //    pt1.x = cvRound(x0 + 1000 * (-b));//对一个double型的数进行四舍五入,并返回一个整型数!
34     //    pt1.y = cvRound(y0 + 1000 * (a));
35     //    pt2.x = cvRound(x0 - 1000 * (-b));
36     //    pt2.y = cvRound(y0 - 1000 * (a));
37     //    line(dst, pt1, pt2, Scalar(0, 0, 255), 1, CV_AA);
38     //}
39     
40         
41     //第二种方法(概率霍夫变换)
42     vector<Vec4f> plines;
43     HoughLinesP(src_gray, plines, 1, CV_PI / 180.0, 10, 0, 10);
44     Scalar color = Scalar(0, 0, 255);
45     for (size_t i = 0; i < plines.size(); i++) {
46     Vec4f hline = plines[i];
47     line(dst, Point(hline[0], hline[1]), Point(hline[2], hline[3]), color, 3, LINE_AA);
48     }
49 
50     imshow("效果图",dst);
51     
52     waitKey(0);
53     return 0;
54 
55 }

 

 

 

 1 #include <opencv2/opencv.hpp>
 2 #include <iostream>
 3 #include <math.h>
 4 
 5 using namespace cv;
 6 using namespace std;
 7 
 8 
 9 int main(int argc, char** argv)
10 {
11     Mat src, src_gray;
12     src = imread("3 input.bmp");
13 
14     char INPUT_TITLE[] = "input image";
15 
16     imshow(INPUT_TITLE, src);
17 
18   //转成灰度图
19     cvtColor(src, src_gray, COLOR_BGR2GRAY);
20  
21     GaussianBlur(src_gray, src_gray, Size(9, 9), 2, 2);
22 
23     //进行霍夫圆变换
24     vector<Vec3f> circles;
25     HoughCircles(src_gray, circles, HOUGH_GRADIENT, 1.5, 10, 200, 100, 0, 0);
26 
27     for (size_t i = 0; i < circles.size(); i++)
28     {
29         Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
30         int radius = cvRound(circles[i][2]);
31      
       //绘制圆心 32 circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);
       //绘制圆的轮廓
33 circle(src, center, radius, Scalar(155, 50, 255), 3, 8, 0); 34 35 } 36 37 imshow("效果图", src); 38 39 waitKey(0); 40 return 0; 41 42 }

霍夫圆检测一般只会找出最大的一个圆

posted on 2018-09-23 10:35  一抹烟霞  阅读(10846)  评论(0编辑  收藏  举报

Live2D