Python實(shí)現(xiàn)霍夫圓和橢圓變換代碼詳解
在極坐標(biāo)中,圓的表示方式為:
x=x0+rcosθ
y=y0+rsinθ
圓心為(x0,y0),r為半徑,θ為旋轉(zhuǎn)度數(shù),值范圍為0-359
如果給定圓心點(diǎn)和半徑,則其它點(diǎn)是否在圓上,我們就能檢測出來了。在圖像中,我們將每個(gè)非0像素點(diǎn)作為圓心點(diǎn),以一定的半徑進(jìn)行檢測,如果有一個(gè)點(diǎn)在圓上,我們就對(duì)這個(gè)圓心累加一次。如果檢測到一個(gè)圓,那么這個(gè)圓心點(diǎn)就累加到最大,成為峰值。因此,在檢測結(jié)果中,一個(gè)峰值點(diǎn),就對(duì)應(yīng)一個(gè)圓心點(diǎn)。
霍夫圓檢測的函數(shù):
skimage.transform.hough_circle(image, radius)
radius是一個(gè)數(shù)組,表示半徑的集合,如[3,4,5,6]
返回一個(gè)3維的數(shù)組(radius index, M, N), 第一維表示半徑的索引,后面兩維表示圖像的尺寸。
例1:繪制兩個(gè)圓形,用霍夫圓變換將它們檢測出來。
import numpy as np import matplotlib.pyplot as plt from skimage import draw,transform,feature img = np.zeros((250, 250,3), dtype=np.uint8) rr, cc = draw.circle_perimeter(60, 60, 50) #以半徑50畫一個(gè)圓 rr1, cc1 = draw.circle_perimeter(150, 150, 60) #以半徑60畫一個(gè)圓 img[cc, rr,:] =255 img[cc1, rr1,:] =255 fig, (ax0,ax1) = plt.subplots(1,2, figsize=(8, 5)) ax0.imshow(img) #顯示原圖 ax0.set_title('origin image') hough_radii = np.arange(50, 80, 5) #半徑范圍 hough_res =transform.hough_circle(img[:,:,0], hough_radii) #圓變換 centers = [] #保存所有圓心點(diǎn)坐標(biāo) accums = [] #累積值 radii = [] #半徑 for radius, h in zip(hough_radii, hough_res): #每一個(gè)半徑值,取出其中兩個(gè)圓 num_peaks = 2 peaks =feature.peak_local_max(h, num_peaks=num_peaks) #取出峰值 centers.extend(peaks) accums.extend(h[peaks[:, 0], peaks[:, 1]]) radii.extend([radius] * num_peaks) #畫出最接近的圓 image =np.copy(img) for idx in np.argsort(accums)[::-1][:2]: center_x, center_y = centers[idx] radius = radii[idx] cx, cy =draw.circle_perimeter(center_y, center_x, radius) image[cy, cx] =(255,0,0) ax1.imshow(image) ax1.set_title('detected image')
結(jié)果圖如下:原圖中的圓用白色繪制,檢測出的圓用紅色繪制。
例2,檢測出下圖中存在的硬幣。
import numpy as np import matplotlib.pyplot as plt from skimage import data, color,draw,transform,feature,util image = util.img_as_ubyte(data.coins()[0:95, 70:370]) #裁剪原圖片 edges =feature.canny(image, sigma=3, low_threshold=10, high_threshold=50) #檢測canny邊緣 fig, (ax0,ax1) = plt.subplots(1,2, figsize=(8, 5)) ax0.imshow(edges, cmap=plt.cm.gray) #顯示canny邊緣 ax0.set_title('original iamge') hough_radii = np.arange(15, 30, 2) #半徑范圍 hough_res =transform.hough_circle(edges, hough_radii) #圓變換 centers = [] #保存中心點(diǎn)坐標(biāo) accums = [] #累積值 radii = [] #半徑 for radius, h in zip(hough_radii, hough_res): #每一個(gè)半徑值,取出其中兩個(gè)圓 num_peaks = 2 peaks =feature.peak_local_max(h, num_peaks=num_peaks) #取出峰值 centers.extend(peaks) accums.extend(h[peaks[:, 0], peaks[:, 1]]) radii.extend([radius] * num_peaks) #畫出最接近的5個(gè)圓 image = color.gray2rgb(image) for idx in np.argsort(accums)[::-1][:5]: center_x, center_y = centers[idx] radius = radii[idx] cx, cy =draw.circle_perimeter(center_y, center_x, radius) image[cy, cx] = (255,0,0) ax1.imshow(image) ax1.set_title('detected image')
橢圓變換是類似的,使用函數(shù)為:
skimage.transform.hough_ellipse(img,accuracy, threshold, min_size, max_size)
輸入?yún)?shù):
img: 待檢測圖像。
accuracy: 使用在累加器上的短軸二進(jìn)制尺寸,是一個(gè)double型的值,默認(rèn)為1
thresh: 累加器閾值,默認(rèn)為4
min_size: 長軸最小長度,默認(rèn)為4
max_size: 短軸最大長度,默認(rèn)為None,表示圖片最短邊的一半。
返回一個(gè) [(accumulator, y0, x0, a, b, orientation)] 數(shù)組,accumulator表示累加器,(y0,x0)表示橢圓中心點(diǎn),(a,b)分別表示長短軸,orientation表示橢圓方向
例:檢測出咖啡圖片中的橢圓杯口
import matplotlib.pyplot as plt from skimage import data,draw,color,transform,feature #加載圖片,轉(zhuǎn)換成灰度圖并檢測邊緣 image_rgb = data.coffee()[0:220, 160:420] #裁剪原圖像,不然速度非常慢 image_gray = color.rgb2gray(image_rgb) edges = feature.canny(image_gray, sigma=2.0, low_threshold=0.55, high_threshold=0.8) #執(zhí)行橢圓變換 result =transform.hough_ellipse(edges, accuracy=20, threshold=250,min_size=100, max_size=120) result.sort(order='accumulator') #根據(jù)累加器排序 #估計(jì)橢圓參數(shù) best = list(result[-1]) #排完序后取最后一個(gè) yc, xc, a, b = [int(round(x)) for x in best[1:5]] orientation = best[5] #在原圖上畫出橢圓 cy, cx =draw.ellipse_perimeter(yc, xc, a, b, orientation) image_rgb[cy, cx] = (0, 0, 255) #在原圖中用藍(lán)色表示檢測出的橢圓 #分別用白色表示canny邊緣,用紅色表示檢測出的橢圓,進(jìn)行對(duì)比 edges = color.gray2rgb(edges) edges[cy, cx] = (250, 0, 0) fig2, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, figsize=(8, 4)) ax1.set_title('Original picture') ax1.imshow(image_rgb) ax2.set_title('Edge (white) and result (red)') ax2.imshow(edges) plt.show()
霍夫橢圓變換速度非常慢,應(yīng)避免圖像太大。
總結(jié)
以上就是本文關(guān)于Python實(shí)現(xiàn)霍夫圓和橢圓變換代碼詳解的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
對(duì)Python 網(wǎng)絡(luò)設(shè)備巡檢腳本的實(shí)例講解
下面小編就為大家分享一篇對(duì)Python 網(wǎng)絡(luò)設(shè)備巡檢腳本的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python學(xué)習(xí)筆記之For循環(huán)用法詳解
這篇文章主要介紹了Python學(xué)習(xí)筆記之For循環(huán)用法,結(jié)合實(shí)例形式詳細(xì)分析了Python for循環(huán)的功能、原理、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-08-08Python高級(jí)技巧之利用psutil和subprocess實(shí)現(xiàn)程序監(jiān)控與管理
本文介紹了如何使用Python的psutil和subprocess模塊監(jiān)控程序運(yùn)行狀態(tài),并提供了一個(gè)案例腳本,用于監(jiān)控目標(biāo)程序并在停止時(shí)自動(dòng)重啟,詳細(xì)介紹了subprocess模塊的基本用法和psutil模塊的系統(tǒng)信息獲取、進(jìn)程管理及資源監(jiān)控功能,需要的朋友可以參考下2024-09-09Python Opencv 通過軌跡(跟蹤)欄實(shí)現(xiàn)更改整張圖像的背景顏色
這篇文章主要介紹了Python Opencv 通過軌跡(跟蹤)欄實(shí)現(xiàn)更改整張圖像的背景顏色,在文章末尾有一個(gè)小訓(xùn)練——是將所學(xué)得的圖像顏色修改應(yīng)用為畫板一般的刷新,需要的朋友可以參考下2020-03-03Django實(shí)現(xiàn)隨機(jī)圖形驗(yàn)證碼的示例
這篇文章主要介紹了Django實(shí)現(xiàn)隨機(jī)圖形驗(yàn)證碼的示例,幫助大家更好的學(xué)習(xí)和使用django框架,感興趣的朋友可以了解下2020-10-10