【opencv2】直線hough變換,opencv2hough變換
在opencv2中,直線Hough變換有以下兩種形式:
標準霍夫線變換(HoughLines)
原理在這裡的部落格已經說了。此函數能給我們提供一組參數對(θ,rθ) 的集合來表示檢測到的直線
統計機率霍夫線變換(HoughLinesP )
這是執行起來效率更高的霍夫線變換. 它輸出檢測到的直線的端點(x0,y0,xn,xn)
先看程式,後面會講一下每個函數的用法:
#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"#include <iostream>using namespace cv;using namespace std;int main(int argc, char** argv){string imageName = "1.jpg";Mat src, dst, color_dst;src = imread("1.jpg", 0); //讀取映像,以灰階的方式 Canny(src, dst, 50, 200, 3);//使用canny運算元進行邊緣檢測cvtColor(dst, color_dst, CV_GRAY2BGR);//將單通道的灰階圖轉化為3通到的圖,用於後面用彩色畫筆畫線#if 0 //標準Hough變換vector<Vec2f> lines;HoughLines( dst, lines, 1, CV_PI/180, 100 );for( size_t i = 0; i < lines.size(); i++ ){float rho = lines[i][0];float theta = lines[i][1];double a = cos(theta), b = sin(theta);double x0 = a*rho, y0 = b*rho;Point pt1(cvRound(x0 + 1000*(-b)),cvRound(y0 + 1000*(a)));Point pt2(cvRound(x0 - 1000*(-b)),cvRound(y0 - 1000*(a)));line( color_dst, pt1, pt2, Scalar(0,0,255), 3, 8 );}#else //統計機率Hough變換vector<Vec4i> lines;HoughLinesP(dst, lines, 1, CV_PI / 180, 200, 30, 10);for (size_t i = 0; i < lines.size(); i++){line(color_dst, Point(lines[i][0], lines[i][1]),Point(lines[i][2], lines[i][3]), Scalar(0, 0, 255), 1, 8);}#endifnamedWindow( "Source", 1 );imshow( "Source", src );namedWindow( "Detected Lines", 1 );imshow("Detected Lines", color_dst);waitKey(0);return 0;}
這是opencv手冊中給出的參數解釋,有些特殊符號打不出來,可以參見這裡:
標準hough變換函數:
void HoughLines(InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, double stn=0 )
· image – 8-bit, single-channel binary source image. The image may be modified by the function.
· lines – Output vector of lines. Each line is represented by a two-element vector(ρ,θ)
ρis the distance from the coordinate origin (topleft corner of the image). θis the line rotation angle in radians (0,pi).
· rho – Distance resolution of the accumulator in pixels.
· theta – Angle resolution of the accumulator in radians.
· threshold – Accumulator threshold parameter. Only those lines are returned that get enough votes .
· srn – For the multi-scale Hough transform, it is a divisor for the distance resolution rho . The coarse accumulator distance resolution is rho and the accurate accumulator resolution is rho/srn . If both srn=0 and stn=0 , the classical Hough transform is used. Otherwise, both these parameters should be positive.
· stn – For the multi-scale Hough transform, it is a divisor for the distance resolution theta.
下面是我的翻譯和理解:
dst: 邊緣檢測的輸出映像. 它應該是個灰階圖 (但事實上是個二值化圖)
lines: 儲存著檢測到的直線的參數對(ρ,θ)的容器
rho : 參數極徑 ρ 以像素值為單位的解析度. 程式裡使用的是 1 像素.
theta: 參數極角 θ 以弧度為單位的解析度. 我們使用 1度 (即CV_PI/180)
threshold: 確認一條直線的累加器的最小累加值。即認為累加器累加的次數超過threshold後,其參數對應的線才為直線。
srn和stn一組額外的參數,用於多尺度hough變換。當這兩個參數都為0時才執行標準hough演算法。
統計機率hough變換函數:
void HoughLinesP(InputArray image, OutputArray lines, double rho, double theta, int threshold, double minLineLength=0, double maxLineGap=0 )
前面的幾個參數都與標準hough含義一致,只有最後兩個不一樣。
這裡是opencv手冊給出的解釋:
minLineLength – Minimum line length. Line segments shorter than that are rejected.
maxLineGap – Maximum allowed gap between points on the same line to link them.
下面是我的翻譯和理解:
minLinLength:直線的最小長度。即當檢測出的直線長度大於minLinLength,才認為這是一條直線。
maxLineGap: 能夠允許一條直線所存在缺口(中斷)點數的最大值。即假若直線從中間某處斷開,那麼所允許的缺口的最大長度。
調節上面兩個參數可以減少映像中幹擾直線。
下面給出原圖:
檢測後的:
具體效果要看參數的選擇。