Python實(shí)現(xiàn)霍夫圓和橢圓變換代碼詳解
在極坐標(biāo)中,圓的表示方式為:
x=x0+rcosθ
y=y0+rsinθ
圓心為(x0,y0),r為半徑,θ為旋轉(zhuǎn)度數(shù),值范圍為0-359
如果給定圓心點(diǎn)和半徑,則其它點(diǎn)是否在圓上,我們就能檢測(cè)出來了。在圖像中,我們將每個(gè)非0像素點(diǎn)作為圓心點(diǎn),以一定的半徑進(jìn)行檢測(cè),如果有一個(gè)點(diǎn)在圓上,我們就對(duì)這個(gè)圓心累加一次。如果檢測(cè)到一個(gè)圓,那么這個(gè)圓心點(diǎn)就累加到最大,成為峰值。因此,在檢測(cè)結(jié)果中,一個(gè)峰值點(diǎn),就對(duì)應(yīng)一個(gè)圓心點(diǎn)。
霍夫圓檢測(cè)的函數(shù):
skimage.transform.hough_circle(image, radius)
radius是一個(gè)數(shù)組,表示半徑的集合,如[3,4,5,6]
返回一個(gè)3維的數(shù)組(radius index, M, N), 第一維表示半徑的索引,后面兩維表示圖像的尺寸。
例1:繪制兩個(gè)圓形,用霍夫圓變換將它們檢測(cè)出來。
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é)果圖如下:原圖中的圓用白色繪制,檢測(cè)出的圓用紅色繪制。

例2,檢測(cè)出下圖中存在的硬幣。

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) #檢測(cè)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: 待檢測(cè)圖像。
accuracy: 使用在累加器上的短軸二進(jìn)制尺寸,是一個(gè)double型的值,默認(rèn)為1
thresh: 累加器閾值,默認(rèn)為4
min_size: 長(zhǎng)軸最小長(zhǎng)度,默認(rèn)為4
max_size: 短軸最大長(zhǎng)度,默認(rèn)為None,表示圖片最短邊的一半。
返回一個(gè) [(accumulator, y0, x0, a, b, orientation)] 數(shù)組,accumulator表示累加器,(y0,x0)表示橢圓中心點(diǎn),(a,b)分別表示長(zhǎng)短軸,orientation表示橢圓方向
例:檢測(cè)出咖啡圖片中的橢圓杯口
import matplotlib.pyplot as plt
from skimage import data,draw,color,transform,feature
#加載圖片,轉(zhuǎn)換成灰度圖并檢測(cè)邊緣
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)色表示檢測(cè)出的橢圓
#分別用白色表示canny邊緣,用紅色表示檢測(cè)出的橢圓,進(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)霍夫圓和橢圓變換代碼詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Python字符編碼轉(zhuǎn)碼之GBK,UTF8互轉(zhuǎn)
說到python的編碼,一句話總結(jié),說多了都是淚啊,這個(gè)在以后的python的開發(fā)中絕對(duì)是一件令人頭疼的事情。所以有必要輸入理解2020-02-02
Django使用uwsgi部署時(shí)的配置以及django日志文件的處理方法
今天小編就為大家分享一篇Django使用uwsgi部署時(shí)的配置以及django日志文件的處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
python執(zhí)行js腳本報(bào)錯(cuò)CryptoJS is not defined問題
這篇文章主要介紹了python執(zhí)行js腳本報(bào)錯(cuò)CryptoJS is not defined問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Python使用VIF實(shí)現(xiàn)檢測(cè)多重共線性
多重共線性是指多元回歸模型中有兩個(gè)或兩個(gè)以上的自變量,它們之間具有高度的相關(guān)性,本文主要介紹了如何使用VIF實(shí)現(xiàn)檢測(cè)多重共線性,需要的可以參考下2023-12-12
Python3標(biāo)準(zhǔn)庫glob文件名模式匹配的問題
glob的模式規(guī)則與re模塊使用的正則表達(dá)式并不相同。實(shí)際上,glob的模式遵循標(biāo)準(zhǔn)UNIX路徑擴(kuò)展規(guī)則。只使用幾個(gè)特殊字符來實(shí)現(xiàn)兩個(gè)不同的通配符和字符區(qū)間。這篇文章主要介紹了Python3標(biāo)準(zhǔn)庫glob文件名模式匹配的知識(shí),需要的朋友可以參考下2020-03-03

