在本教程中,您将学习如何:
注意
下面的解释属于 Bradski 和 Kaehler 的 Learning OpenCV 一书。
对于 Hough 变换,我们将在 Polar 系统中表示线。因此,直线方程可以写成:
排列项:
一般来说,对于每个点 \((x_{0}, y_{0})\),我们可以将通过该点的线族定义为:
这意味着每对 \(r_{\theta},\theta)\) 表示经过 \((x_{0}, y_{0})\) 的每条线。
我们只考虑(r<0)和这样的点。
这三个图在一个点(0.925,9.6)相交,这些坐标是参数(Q,r)或(x0,y0)和(x1,y1)所在的线。
OpenCV 实现了两种类型的 Hough 线变换:
a. 标准 Hough 变换
b. 概率霍夫线变换
们将要解释的示例代码可以从这里下载。可以在此处找到一个稍微花哨的版本(它显示了 Hough 标准和概率,带有用于更改阈值的跟踪栏)。
- #include "opencv2/imgcodecs.hpp"
- #include "opencv2/highgui.hpp"
- #include "opencv2/imgproc.hpp"
-
- using namespace cv;
- using namespace std;
-
- int main(int argc, char** argv)
- {
- // Declare the output variables
- Mat dst, cdst, cdstP;
-
- const char* default_file = "sudoku.png";
- const char* filename = argc >=2 ? argv[1] : default_file;
-
- // Loads an image
- Mat src = imread( samples::findFile( filename ), IMREAD_GRAYSCALE );
-
- // Check if image is loaded fine
- if(src.empty()){
- printf(" Error opening image\n");
- printf(" Program Arguments: [image_name -- default %s] \n", default_file);
- return -1;
- }
-
- // Edge detection
- Canny(src, dst, 50, 200, 3);
-
- // Copy edges to the images that will display the results in BGR
- cvtColor(dst, cdst, COLOR_GRAY2BGR);
- cdstP = cdst.clone();
-
- // Standard Hough Line Transform
- vector
lines; // will hold the results of the detection - HoughLines(dst, lines, 1, CV_PI/180, 150, 0, 0 ); // runs the actual detection
- // Draw the lines
- for( size_t i = 0; i < lines.size(); i++ )
- {
- float rho = lines[i][0], theta = lines[i][1];
- Point pt1, pt2;
- double a = cos(theta), b = sin(theta);
- double x0 = a*rho, y0 = b*rho;
- pt1.x = cvRound(x0 + 1000*(-b));
- pt1.y = cvRound(y0 + 1000*(a));
- pt2.x = cvRound(x0 - 1000*(-b));
- pt2.y = cvRound(y0 - 1000*(a));
- line( cdst, pt1, pt2, Scalar(0,0,255), 3, LINE_AA);
- }
-
- // Probabilistic Line Transform
- vector
linesP; // will hold the results of the detection - HoughLinesP(dst, linesP, 1, CV_PI/180, 50, 50, 10 ); // runs the actual detection
- // Draw the lines
- for( size_t i = 0; i < linesP.size(); i++ )
- {
- Vec4i l = linesP[i];
- line( cdstP, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, LINE_AA);
- }
-
- // Show results
- imshow("Source", src);
- imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst);
- imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP);
-
- // Wait and Exit
- waitKey();
- return 0;
- }
- const char* default_file = "sudoku.png";
- const char* filename = argc >=2 ? argv[1] : default_file;
-
- // Loads an image
- Mat src = imread( samples::findFile( filename ), IMREAD_GRAYSCALE );
-
- // Check if image is loaded fine
- if(src.empty()){
- printf(" Error opening image\n");
- printf(" Program Arguments: [image_name -- default %s] \n", default_file);
- return -1;
- }
使用 Canny 检测器检测图像的边缘:
- // Edge detection
- Canny(src, dst, 50, 200, 3);
现在我们将应用 Hough 线变换。我们将解释如何使用可用于此目的的两个 OpenCV 函数。
首先,应用转换:
- // Standard Hough Line Transform
- vector
lines; // will hold the results of the detection - HoughLines(dst, lines, 1, CV_PI/180, 150, 0, 0 ); // runs the actual detection
然后通过绘制线条来显示结果。
- // Draw the lines
- for( size_t i = 0; i < lines.size(); i++ )
- {
- float rho = lines[i][0], theta = lines[i][1];
- Point pt1, pt2;
- double a = cos(theta), b = sin(theta);
- double x0 = a*rho, y0 = b*rho;
- pt1.x = cvRound(x0 + 1000*(-b));
- pt1.y = cvRound(y0 + 1000*(a));
- pt2.x = cvRound(x0 - 1000*(-b));
- pt2.y = cvRound(y0 - 1000*(a));
- line( cdst, pt1, pt2, Scalar(0,0,255), 3, LINE_AA);
- }
首先,应用转换:
- // Probabilistic Line Transform
- vector
linesP; // will hold the results of the detection - HoughLinesP(dst, linesP, 1, CV_PI/180, 50, 50, 10 ); // runs the actual detection
- // Draw the lines
- for( size_t i = 0; i < linesP.size(); i++ )
- {
- Vec4i l = linesP[i];
- line( cdstP, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, LINE_AA);
- }
- // Show results
- imshow("Source", src);
- imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst);
- imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP);
- // Wait and Exit
- waitKey();
- return 0;
注意
下面的结果是使用我们在代码部分提到的稍微花哨的版本获得的。它仍然实现与上述相同的内容,只是为阈值添加了跟踪栏。使用输入图像,例如数独图像。我们通过使用标准 Hough 线变换得到以下结果:
通过使用概率 Hough 线变换:
您可能会发现,在更改阈值时,检测到的行数会发生变化。解释是显而易见的:如果建立更高的阈值,则检测到的行将减少(因为您将需要更多的点来声明检测到的行)。
参考文献:
1、《Hough Line Transform》-----Ana Huamán