在圖片處理中,霍夫變換主要是用來檢測圖片中的幾何形狀,包括直線、圓、橢圓等。
在skimage中,霍夫變換是放在tranform模塊內(nèi),本篇主要講解霍夫線變換。
對(duì)于平面中的一條直線,在笛卡爾坐標(biāo)系中,可用y=mx+b來表示,其中m為斜率,b為截距。但是如果直線是一條垂直線,則m為無窮大,所有通常我們?cè)诹硪蛔鴺?biāo)系中表示直線,即極坐標(biāo)系下的r=xcos(theta)+ysin(theta)。即可用(r,theta)來表示一條直線。其中r為該直線到原點(diǎn)的距離,theta為該直線的垂線與x軸的夾角。如下圖所示。
對(duì)于一個(gè)給定的點(diǎn)(x0,y0), 我們?cè)跇O坐標(biāo)下繪出所有通過它的直線(r,theta),將得到一條正弦曲線。如果將圖片中的所有非0點(diǎn)的正弦曲線都繪制出來,則會(huì)存在一些交點(diǎn)。所有經(jīng)過這個(gè)交點(diǎn)的正弦曲線,說明都擁有同樣的(r,theta), 意味著這些點(diǎn)在一條直線上。
發(fā)上圖所示,三個(gè)點(diǎn)(對(duì)應(yīng)圖中的三條正弦曲線)在一條直線上,因?yàn)檫@三個(gè)曲線交于一點(diǎn),具有相同的(r, theta)。霍夫線變換就是利用這種方法來尋找圖中的直線。
函數(shù):skimage.transform.hough_line(img)
返回三個(gè)值:
h: 霍夫變換累積器
theta: 點(diǎn)與x軸的夾角集合,一般為0-179度
distance: 點(diǎn)到原點(diǎn)的距離,即上面的所說的r.
例:
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
|
import skimage.transform as st import numpy as np import matplotlib.pyplot as plt # 構(gòu)建測試圖片 image = np.zeros(( 100 , 100 )) #背景圖 idx = np.arange( 25 , 75 ) #25-74序列 image[idx[:: - 1 ], idx] = 255 # 線條\ image[idx, idx] = 255 # 線條/ # hough線變換 h, theta, d = st.hough_line(image) #生成一個(gè)一行兩列的窗口(可顯示兩張圖片). fig, (ax0, ax1) = plt.subplots( 1 , 2 , figsize = ( 8 , 6 )) plt.tight_layout() #顯示原始圖片 ax0.imshow(image, plt.cm.gray) ax0.set_title( 'Input image' ) ax0.set_axis_off() #顯示hough變換所得數(shù)據(jù) ax1.imshow(np.log( 1 + h)) ax1.set_title( 'Hough transform' ) ax1.set_xlabel( 'Angles (degrees)' ) ax1.set_ylabel( 'Distance (pixels)' ) ax1.axis( 'image' ) |
從右邊那張圖可以看出,有兩個(gè)交點(diǎn),說明原圖像中有兩條直線。
如果我們要把圖中的兩條直線繪制出來,則需要用到另外一個(gè)函數(shù):
skimage.transform.hough_line_peaks(hspace, angles, dists)
用這個(gè)函數(shù)可以取出峰值點(diǎn),即交點(diǎn),也即原圖中的直線。
返回的參數(shù)與輸入的參數(shù)一樣。我們修改一下上邊的程序,在原圖中將兩直線繪制出來。
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
38
39
|
import skimage.transform as st import numpy as np import matplotlib.pyplot as plt # 構(gòu)建測試圖片 image = np.zeros(( 100 , 100 )) #背景圖 idx = np.arange( 25 , 75 ) #25-74序列 image[idx[:: - 1 ], idx] = 255 # 線條\ image[idx, idx] = 255 # 線條/ # hough線變換 h, theta, d = st.hough_line(image) #生成一個(gè)一行三列的窗口(可顯示三張圖片). fig, (ax0, ax1,ax2) = plt.subplots( 1 , 3 , figsize = ( 8 , 6 )) plt.tight_layout() #顯示原始圖片 ax0.imshow(image, plt.cm.gray) ax0.set_title( 'Input image' ) ax0.set_axis_off() #顯示hough變換所得數(shù)據(jù) ax1.imshow(np.log( 1 + h)) ax1.set_title( 'Hough transform' ) ax1.set_xlabel( 'Angles (degrees)' ) ax1.set_ylabel( 'Distance (pixels)' ) ax1.axis( 'image' ) #顯示檢測出的線條 ax2.imshow(image, plt.cm.gray) row1, col1 = image.shape for _, angle, dist in zip ( * st.hough_line_peaks(h, theta, d)): y0 = (dist - 0 * np.cos(angle)) / np.sin(angle) y1 = (dist - col1 * np.cos(angle)) / np.sin(angle) ax2.plot(( 0 , col1), (y0, y1), '-r' ) ax2.axis(( 0 , col1, row1, 0 )) ax2.set_title( 'Detected lines' ) ax2.set_axis_off() |
注意,繪制線條的時(shí)候,要從極坐標(biāo)轉(zhuǎn)換為笛卡爾坐標(biāo),公式為:
skimage還提供了另外一個(gè)檢測直線的霍夫變換函數(shù),概率霍夫線變換:
skimage.transform.probabilistic_hough_line(img, threshold=10, line_length=5,line_gap=3)
參數(shù):
img: 待檢測的圖像。
threshold: 閾值,可先項(xiàng),默認(rèn)為10
line_length: 檢測的最短線條長度,默認(rèn)為50
line_gap: 線條間的最大間隙。增大這個(gè)值可以合并破碎的線條。默認(rèn)為10
返回:
lines: 線條列表, 格式如((x0, y0), (x1, y0)),標(biāo)明開始點(diǎn)和結(jié)束點(diǎn)。
下面,我們用canny算子提取邊緣,然后檢測哪些邊緣是直線?
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
|
import skimage.transform as st import matplotlib.pyplot as plt from skimage import data,feature #使用Probabilistic Hough Transform. image = data.camera() edges = feature.canny(image, sigma = 2 , low_threshold = 1 , high_threshold = 25 ) lines = st.probabilistic_hough_line(edges, threshold = 10 , line_length = 5 ,line_gap = 3 ) # 創(chuàng)建顯示窗口. fig, (ax0, ax1, ax2) = plt.subplots( 1 , 3 , figsize = ( 16 , 6 )) plt.tight_layout() #顯示原圖像 ax0.imshow(image, plt.cm.gray) ax0.set_title( 'Input image' ) ax0.set_axis_off() #顯示canny邊緣 ax1.imshow(edges, plt.cm.gray) ax1.set_title( 'Canny edges' ) ax1.set_axis_off() #用plot繪制出所有的直線 ax2.imshow(edges * 0 ) for line in lines: p0, p1 = line ax2.plot((p0[ 0 ], p1[ 0 ]), (p0[ 1 ], p1[ 1 ])) row2, col2 = image.shape ax2.axis(( 0 , col2, row2, 0 )) ax2.set_title( 'Probabilistic Hough' ) ax2.set_axis_off() plt.show() |
總結(jié)
以上就是本文關(guān)于Python數(shù)字圖像處理之霍夫線變換實(shí)現(xiàn)詳解的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!