熱線電話:13121318867

登錄
首頁精彩閱讀Python實現霍夫圓和橢圓變換代碼詳解
Python實現霍夫圓和橢圓變換代碼詳解
2018-01-24
收藏

Python實現霍夫圓和橢圓變換代碼詳解

這篇文章主要介紹了Python實現霍夫圓和橢圓變換代碼詳解,具有一定借鑒價值,需要的朋友可以參考下

在極坐標中,圓的表示方式為:

x=x0+rcosθ

y=y0+rsinθ

圓心為(x0,y0),r為半徑,θ為旋轉度數,值范圍為0-359

如果給定圓心點和半徑,則其它點是否在圓上,我們就能檢測出來了。在圖像中,我們將每個非0像素點作為圓心點,以一定的半徑進行檢測,如果有一個點在圓上,我們就對這個圓心累加一次。如果檢測到一個圓,那么這個圓心點就累加到最大,成為峰值。因此,在檢測結果中,一個峰值點,就對應一個圓心點。

霍夫圓檢測的函數:

skimage.transform.hough_circle(image, radius)

radius是一個數組,表示半徑的集合,如[3,4,5,6]

返回一個3維的數組(radius index, M, N), 第一維表示半徑的索引,后面兩維表示圖像的尺寸。

例1:繪制兩個圓形,用霍夫圓變換將它們檢測出來。

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畫一個圓
rr1, cc1 = draw.circle_perimeter(150, 150, 60) #以半徑60畫一個圓
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 = [] #保存所有圓心點坐標
accums = [] #累積值
radii = [] #半徑
 
for radius, h in zip(hough_radii, hough_res):
 #每一個半徑值,取出其中兩個圓
 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')

結果圖如下:原圖中的圓用白色繪制,檢測出的圓用紅色繪制。

例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 = [] #保存中心點坐標
accums = [] #累積值
radii = [] #半徑
 
for radius, h in zip(hough_radii, hough_res):
 #每一個半徑值,取出其中兩個圓
 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個圓
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')

橢圓變換是類似的,使用函數為:

skimage.transform.hough_ellipse(img,accuracy, threshold, min_size, max_size)

輸入參數:

img: 待檢測圖像。

accuracy: 使用在累加器上的短軸二進制尺寸,是一個double型的值,默認為1

thresh: 累加器閾值,默認為4

min_size: 長軸最小長度,默認為4

max_size: 短軸最大長度,默認為None,表示圖片最短邊的一半。

返回一個 [(accumulator, y0, x0, a, b, orientation)] 數組,accumulator表示累加器,(y0,x0)表示橢圓中心點,(a,b)分別表示長短軸,orientation表示橢圓方向

例:檢測出咖啡圖片中的橢圓杯口

import matplotlib.pyplot as plt
from skimage import data,draw,color,transform,feature
 
#加載圖片,轉換成灰度圖并檢測邊緣
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)
 
#執行橢圓變換
result =transform.hough_ellipse(edges, accuracy=20, threshold=250,min_size=100, max_size=120)
result.sort(order='accumulator') #根據累加器排序
 
#估計橢圓參數
best = list(result[-1]) #排完序后取最后一個
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) #在原圖中用藍色表示檢測出的橢圓
 
#分別用白色表示canny邊緣,用紅色表示檢測出的橢圓,進行對比
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()

霍夫橢圓變換速度非常慢,應避免圖像太大。

總結

以上就是本文關于Python實現霍夫圓和橢圓變換代碼詳解的全部內容

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢