99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀Python實(shí)現(xiàn)霍夫圓和橢圓變換代碼詳解
Python實(shí)現(xiàn)霍夫圓和橢圓變換代碼詳解
2018-01-24
收藏

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

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }