opencv学习笔记5 霍夫变换 漫水填充

一。霍夫线变换

1.标准霍夫变换(StandardHough Transform,SHT),由HoughLines函数调用。

2.多尺度霍夫变换(Multi-ScaleHough Transform,MSHT),由HoughLines函数调用。

void HoughLines(InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, double stn=0 )

 1 #include<opencv.hpp>
 2 #include<vector>
 3 using namespace std;
 4 using namespace cv;
 5 int main()
 6 {
 7     Mat src = imread("E:/house.png");
 8     Mat gray,dst;
 9     Canny(src, gray, 50, 200);
10     cvtColor(gray, dst, CV_GRAY2BGR);
11     vector<Vec2f> lines; //Vec2f存放2个float的向量 vector<Vec2f>类似二维数组
12     HoughLines(gray, lines, 1, CV_PI / 180, 160, 0, 0); //对二值图像进行霍夫变换,保存直线信息于lines
13     for (size_t i = 0; i < lines.size(); i++)
14     {
15         float rho = lines[i][0], theta = lines[i][1]; //rho为原点到直线的最短距离r,theta为距离的角度θ
16         Point pt1, pt2;
17         double a = cos(theta), b = sin(theta);
18         double x0 = a * rho, y0 = b * rho;  //x=rcosθ,y=rsinθ
19         pt1.x = cvRound(x0 + 1000 * (-b));
20         pt1.y = cvRound(y0 + 1000 * (a));
21         pt2.x = cvRound(x0 - 1000 * (-b));
22         pt2.y = cvRound(y0 - 1000 * (a));
23         line(dst, pt1, pt2, Scalar(55, 100, 195), 1, CV_AA);
24     }
25     imshow("src", src);
26     imshow("dst,",dst);
27     waitKey();
28     return 0;
29 }

3.累计概率霍夫变换(ProgressiveProbabilistic Hough Transform,PPHT),由HoughLinesP函数调用。

void HoughLinesP(InputArray image, OutputArray lines, double rho, double theta, int threshold, double minLineLength=0, double maxLineGap=0 )

 1 #include<opencv.hpp>
 2 #include<vector>
 3 using namespace std;
 4 using namespace cv;
 5 int main()
 6 {
 7     Mat src = imread("E:/house.png");
 8     Mat gray, dst;
 9     Canny(src, gray, 50, 200);
10     cvtColor(gray, dst, CV_GRAY2BGR);
11     vector<Vec4i> lines; 
12     HoughLinesP(gray, lines, 1, CV_PI / 180, 50, 50, 10);
13     for (size_t i = 0; i < lines.size(); i++)
14     {
15         Vec4i l = lines[i];
16         line(dst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(200, 150, 30), 1, 8);
17     }
18     imshow("src", src);
19     imshow("dst,", dst);
20     waitKey();
21     return 0;
22 }

二。霍夫圆变换

void HoughCircles(InputArray image,OutputArray circles, int method, double dp, double minDist, double param1=100,double param2=100, int minRadius=0, int maxRadius=0 )

 1 #include<opencv.hpp>
 2 #include<vector>
 3 using namespace std;
 4 using namespace cv;
 5 int main()
 6 {
 7     Mat src = imread("E:/circle.png");
 8     Mat gray, blur;
 9     cvtColor(src, gray, CV_BGR2GRAY);
10     GaussianBlur(gray, blur, Size(9, 9), 2, 2);
11     vector<Vec3f>Circles;
12     HoughCircles(blur, Circles, CV_HOUGH_GRADIENT, 1, 10, 200, 50, 0, 0);
13     for (size_t i = 0; i < Circles.size(); i++)
14     {
15         Point center(cvRound(Circles[i][0]), cvRound(Circles[i][1]));
16         int radius = cvRound(Circles[i][2]);
17         //绘制圆轮廓
18         circle(src, center, radius,Scalar(0,0,255),3,8);
19     }
20     imshow("src", src);
21     waitKey();
22     return 0;
23 }

 

三。漫水填充

1.带mask版本

int floodFill(InputOutputArray image, InputOutputArray mask, Point seedPoint,Scalar newVal, Rect* rect=0, Scalar loDiff=Scalar(), Scalar upDiff=Scalar(), int flags=4 )

2.不带mask版本

int floodFill(InputOutputArray image, Point seedPoint, Scalar newVal, Rect* rect=0, Scalar loDiff=Scalar(), Scalar upDiff=Scalar(), int flags=4 )

1     Mat src = imread("E:/test.jpg");
2     imshow("src", src);
3     Rect rect;
4     floodFill(src, Point(50, 50), Scalar(255, 255, 255), &rect,Scalar(20, 20, 20), Scalar(20, 20, 20));
5     imshow("dst", src);
6     waitKey();
7     return 0;

 

参考:https://blog.csdn.net/poem_qianmo/article/details/28261997

原文地址:https://www.cnblogs.com/sclu/p/11510997.html