
Python實(shí)現(xiàn)霍夫圓和橢圓變換代碼詳解
這篇文章主要介紹了Python實(shí)現(xiàn)霍夫圓和橢圓變換代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下
在極坐標(biāo)中,圓的表示方式為:
x=x0+rcosθ
y=y0+rsinθ
圓心為(x0,y0),r為半徑,θ為旋轉(zhuǎn)度數(shù),值范圍為0-359
如果給定圓心點(diǎn)和半徑,則其它點(diǎn)是否在圓上,我們就能檢測(cè)出來(lái)了。在圖像中,我們將每個(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è)出來(lái)。
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)容
數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼
若不方便掃碼,搜微信號(hào):CDAshujufenxi
LSTM 模型輸入長(zhǎng)度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長(zhǎng)序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠(chéng)摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡(jiǎn)稱 BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測(cè)分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢(shì)預(yù)判? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代,預(yù)測(cè)分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭(zhēng)搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢(shì)性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢(shì)性檢驗(yàn)如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢(shì)與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢(shì)變化以及識(shí)別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國(guó)內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對(duì)策略? 長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場(chǎng)調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場(chǎng)調(diào)研是企業(yè)洞察市場(chǎng)動(dòng)態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場(chǎng)調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動(dòng)力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動(dòng)力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價(jià)值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03