国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Python邊緣檢測之prewitt, sobel, laplace算子

這篇具有很好參考價(jià)值的文章主要介紹了Python邊緣檢測之prewitt, sobel, laplace算子。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

濾波算子簡介

ndimage中提供了卷積算法,并且建立在卷積之上,提供了三種邊緣檢測的濾波方案:prewitt, sobel以及laplace。

在convolve中列舉了一個(gè)用于邊緣檢測的濾波算子,統(tǒng)一維度后,其 x x x y y y向的梯度算子分別寫為

[ ? 1 0 1 ? 1 0 1 ? 1 0 1 ] , [ ? 1 ? 1 ? 1 0 0 0 1 1 1 ] \begin{bmatrix} -1&0&1\\-1&0&1\\-1&0&1\\ \end{bmatrix}, \begin{bmatrix} -1&-1&-1\\0&0&0\\1&1&1\\ \end{bmatrix} ??1?1?1?000?111? ?, ??101??101??101? ?

此即prewitt算子。

Sobel算子為Prewitt增添了中心值的權(quán)重,記為

[ ? 1 0 1 ? 2 0 2 ? 1 0 1 ] , [ ? 1 ? 2 ? 1 0 0 0 1 2 1 ] \begin{bmatrix} -1&0&1\\-2&0&2\\-1&0&1\\ \end{bmatrix}, \begin{bmatrix} -1&-2&-1\\0&0&0\\1&2&1\\ \end{bmatrix} ??1?2?1?000?121? ?, ??101??202??101? ?

這兩種邊緣檢測算子,均適用于某一個(gè)方向,ndimage還提供了lapace算子,其本質(zhì)是二階微分算子,其 3 × 3 3\times3 3×3卷積模板可表示為

[ ? 1 1 ? 1 ? 1 ? 1 8 ? 1 ? 1 ? 1 ? 1 ] , \begin{bmatrix} -1&1-1&-1\\-1&8&-1\\-1&-1&-1\\ \end{bmatrix}, ??1?1?1?1?18?1??1?1?1? ?,

具體實(shí)現(xiàn)

ndimage封裝的這三種卷積濾波算法,定義如下

prewitt(input, axis=-1, output=None, mode='reflect', cval=0.0)
sobel(input, axis=-1, output=None, mode='reflect', cval=0.0)
laplace(input, output=None, mode='reflect', cval=0.0)

其中,mode表示卷積過程中對(duì)邊緣效應(yīng)的彌補(bǔ)方案,設(shè)待濾波數(shù)組為a b c d,則在不同的模式下,對(duì)邊緣進(jìn)行如下填充

左側(cè)填充 數(shù)據(jù) 右側(cè)填充
reflect d c b a a b c d d c b a
constant k k k k a b c d k k k k
nearest a a a a a b c d d d d d
mirror d c b a b c d c b a
wrap a b c d a b c d a b c d

測試

接下來測試一下

from scipy.ndimage import prewitt, sobel, laplace
from scipy.misc import ascent
import matplotlib.pyplot as plt
img = ascent()

dct = {
    "origin" : lambda img:img,
    "prewitt" : prewitt,
    "sobel" : sobel,
    "laplace" : lambda img : abs(laplace(img))
}

fig = plt.figure()
for i,key in enumerate(dct):
    ax = fig.add_subplot(2,2,i+1)
    ax.imshow(dct[key](img), cmap=plt.cm.gray)
    plt.ylabel(key)

plt.show()

為了看上去更加簡潔,代碼中將原圖、prewitt濾波、sobel濾波以及l(fā)aplace濾波封裝在了一個(gè)字典中。其中origin表示原始圖像,對(duì)應(yīng)的函數(shù)是一個(gè)lambda表達(dá)式。

在繪圖時(shí),通過將cmap映射到plt.cm.gray,使得繪圖之后表現(xiàn)為灰度圖像。

效果如下

Python邊緣檢測之prewitt, sobel, laplace算子文章來源地址http://www.zghlxwxcb.cn/news/detail-437768.html

到了這里,關(guān)于Python邊緣檢測之prewitt, sobel, laplace算子的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包