hough變換是圖像處理中的一種特征提取技術,該過程在一個參數空間中通過計算累計結果的局部最大值得到一個符合特定形狀的集合作為hough變換結果。
發展史:
1962年由paulhough首次提出,用來檢測直線和曲線。
1972年由richard duda & peter hart推廣使用,擴展到任意形狀物體的識別。
原理:
一條直線在直角坐標系下的表示形式為y=k*x+b,而在極坐標系下表示為r=x*cos(theta)+y*sin(theta)。hough變換的思想為在直角坐標系下的一個點對應極坐標系下的一條直線,同樣,極坐標系下的一個點對應直角坐標系下的一條直線。在直角坐標系中的直線,斜率和截距是一定的,這樣這條直線上的所有點在極坐標系中聚焦于一點,這樣的聚焦點就代表了直角坐標系中的直線。
對于直線x=c,在實際應用中,是采用參數方程p=x*cos(theta)+y*sin(theta)。這樣,圖像平面上的一個點就對應到參數r—theta平面上的一條曲線上,其它的還是一樣。
標準hough變換:
imgproc.houghlines(mat image, mat lines, double rho, double theta, int threshold, double srn, double stn, double min_theta, double max_theta)
參數說明:
image:源圖像
lines:hough變換后儲存檢測到的線條的輸出矢量
rho:以像素為單位的距離精度
theta:以弧度為單位的角度精度
threshold:識別某部分為一條直線時必須達到的值
srn:rho參數的除數距離,有默認值0
stn:theta參數的除數距離,默認值0
min_theta:檢測到的直線的最小角度
max_theta:檢測到的直線的最大角度
示例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
public static void main(string[] args) { system.loadlibrary(core.native_library_name); mat srcimage = imgcodecs.imread( "f:\\6597210504144579394.jpg" ); mat dstimage = srcimage.clone(); imgproc.canny(srcimage, dstimage, 400 , 500 , 5 , false ); mat storage = new mat(); imgproc.houghlines(dstimage, storage, 1 , math.pi / 180 , 200 , 0 , 0 , 0 , 10 ); for ( int x = 0 ; x < storage.rows(); x++) { double [] vec = storage.get(x, 0 ); double rho = vec[ 0 ]; double theta = vec[ 1 ]; point pt1 = new point(); point pt2 = new point(); double a = math.cos(theta); double b = math.sin(theta); double x0 = a * rho; double y0 = b * rho; pt1.x = math.round(x0 + 1000 * (-b)); pt1.y = math.round(y0 + 1000 * (a)); pt2.x = math.round(x0 - 1000 * (-b)); pt2.y = math.round(y0 - 1000 * (a)); if (theta >= 0 ) { imgproc.line(srcimage, pt1, pt2, new scalar( 255 , 255 , 255 , 255 ), 1 , imgproc.line_4, 0 ); } } imgcodecs.imwrite( "f:\\dst2.jpg" , srcimage); } |
累計概率hough變換:
imgproc.houghlinesp(mat image, mat lines, double rho, double theta, int threshold, double minlinelength, double maxlinegap)
參數說明:
image:源圖像
lines:hough變換后儲存檢測到的線條的輸出矢量
rho:以像素為單位的距離精度
theta:以弧度為單位的角度精度
threshold:識別某部分為一條直線時必須達到的值
minlinelength:最低線段的長度,默認為0
maxlinegap:允許將同一行點與點之間連接起來的最大的距離,默認為0
示例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public static void main(string[] args) { system.loadlibrary(core.native_library_name); mat srcimage = imgcodecs.imread( "f:\\6597210504144579394.jpg" ); mat dstimage = srcimage.clone(); imgproc.canny(srcimage, dstimage, 400 , 500 , 5 , false ); mat storage = new mat(); imgproc.houghlinesp(dstimage, storage, 1 , math.pi / 180 , 50 , 0 , 0 ); for ( int x = 0 ; x < storage.rows(); x++) { double [] vec = storage.get(x, 0 ); double x1 = vec[ 0 ], y1 = vec[ 1 ], x2 = vec[ 2 ], y2 = vec[ 3 ]; point start = new point(x1, y1); point end = new point(x2, y2); imgproc.line(srcimage, start, end, new scalar( 255 , 255 , 255 , 255 ), 1 , imgproc.line_4, 0 ); } imgcodecs.imwrite( "f:\\dst2.jpg" , srcimage); } |
源圖片:
標準hough變換結果:
累計概率hough變換結果:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/m1109048058/article/details/77334309