Python圖像處理之膨脹與腐蝕的操作
引言
膨脹與腐蝕是圖像處理中兩種最基本的形態(tài)學操作,膨脹將目標點融合到背景中,向外部擴展,腐蝕與膨脹意義相反,消除連通的邊界,使邊界向內(nèi)收縮。在本文中我們將了解使用內(nèi)核的圖像膨脹與腐蝕的基本原理。
讓我們開始吧,同樣我們需要導(dǎo)入必需的庫。
import numpy as np import matplotlib.pyplot as plt from skimage.io import imread, imshow from skimage.draw import circle from skimage.morphology import erosion, dilation
首先讓我們創(chuàng)建一個容易操作的形狀--一個簡單的圓。
circ_image = np.zeros((100, 100)) circ_image[circle(50, 50, 25)] = 1 imshow(circ_image);

現(xiàn)在讓我們定義一個內(nèi)核。
cross = np.array([[0,1,0], [1,1,1], [0,1,0]]) imshow(cross, cmap = 'gray');

將腐蝕函數(shù)應(yīng)用到創(chuàng)建的圓上。
eroded_circle = erosion(circ_image, cross) imshow(eroded_circle);

圖像看起來幾乎一模一樣。要看到那些微小的差異,我們必須仔細查看圖像。
linecolor = 'red'
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
ax[0].imshow(circ_image, cmap = 'gray');
ax[0].set_title('Original', fontsize = 19)
ax[0].axvline(x = 25, color = linecolor)
ax[0].axvline(x = 75, color = linecolor)
ax[0].axhline(y = 25, color = linecolor)
ax[0].axhline(y = 75, color = linecolor)
ax[1].imshow(eroded_circle, cmap = 'gray');
ax[1].set_title('Eroded', fontsize = 19)
ax[1].axvline(x = 25, color = linecolor)
ax[1].axvline(x = 75, color = linecolor)
ax[1].axhline(y = 25, color = linecolor)
ax[1].axhline(y = 75, color = linecolor)
fig.tight_layout()

我們可以看到,被腐蝕的圓已經(jīng)略微縮小了。這就是腐蝕一個對象的意義。如果我們對腐蝕函數(shù)進行迭代,它的效果會變得非常明顯。
def multi_erosion(image, kernel, iterations):
for i in range(iterations):
image = erosion(image, kernel)
return image
ites = [2,4,6,8,10,12,14,16,18,20]
fig, ax = plt.subplots(2, 5, figsize=(17, 5))
for n, ax in enumerate(ax.flatten()):
ax.set_title(f'Iterations : {ites[n]}', fontsize = 16)
new_circle = multi_erosion(circ_image, cross, ites[n])
ax.imshow(new_circle, cmap = 'gray');
ax.axis('off')
fig.tight_layout()

上圖清楚地顯示了圖像是如何被腐蝕的?,F(xiàn)在讓我們嘗試改變內(nèi)核,如果我們使用水平線和垂直線內(nèi)核代替交叉內(nèi)核會怎樣呢?
h_line = np.array([[0,0,0], [1,1,1], [0,0,0]]) v_line = np.array([[0,1,0], [0,1,0], [0,1,0]]) fig, ax = plt.subplots(1, 2, figsize=(15, 5)) ax[0].imshow(h_line, cmap='gray'); ax[1].imshow(v_line, cmap='gray'); fig.tight_layout()

ites = [2,4,6,8,10,12,14,16,18,20]
fig, ax = plt.subplots(2, 5, figsize=(17, 5))
for n, ax in enumerate(ax.flatten()):
ax.set_title(f'Horizontal Iterations : {ites[n]}', fontsize = 12)
new_circle = multi_erosion(circ_image, h_line, ites[n])
ax.imshow(new_circle, cmap = 'gray');
ax.axis('off')
fig.tight_layout()
fig, ax = plt.subplots(2, 5, figsize=(17, 5))
for n, ax in enumerate(ax.flatten()):
ax.set_title(f'Vertical Iterationss : {ites[n]}', fontsize = 12)
new_circle = multi_erosion(circ_image, v_line, ites[n])
ax.imshow(new_circle, cmap = 'gray');
ax.axis('off')
fig.tight_layout()

正如我們所看到的,水平和垂直的腐蝕以不同的方式影響著圖像。使用水平內(nèi)核我們得到一個垂直方向細長的圓;而使用垂直內(nèi)核我們得到一個水平方向細長的圓。
你可能會奇怪,為什么使用垂直內(nèi)核,會得到一個水平方向細長的圓呢?
因為腐蝕函數(shù)是分別尋找垂直和水平的線條,并慢慢把它們削掉。膨脹函數(shù)將會讓我們更清晰的理解這一點。
使用下面的函數(shù)設(shè)置處理的圖像、膨脹內(nèi)核以及迭代次數(shù)。
def multi_dilation(image, kernel, iterations): for i in range(iterations): image = dilation(image, kernel) return image
讓我們看一下處理后的圖像有什么不同。
dilated_circle = multi_dilation(circ_image, cross, 1)
linecolor = 'red'
fig, ax = plt.subplots(1, 2, figsize=(12, 5))
ax[0].imshow(circ_image, cmap = 'gray');
ax[0].set_title('Original', fontsize = 19)
ax[0].axvline(x = 25, color = linecolor)
ax[0].axvline(x = 75, color = linecolor)
ax[0].axhline(y = 25, color = linecolor)
ax[0].axhline(y = 75, color = linecolor)
ax[1].imshow(dilated_circle, cmap = 'gray');
ax[1].set_title('Dilated', fontsize = 19)
ax[1].axvline(x = 25, color = linecolor)
ax[1].axvline(x = 75, color = linecolor)
ax[1].axhline(y = 25, color = linecolor)
ax[1].axhline(y = 75, color = linecolor)
fig.tight_layout()

可以清楚地看到圓現(xiàn)在已經(jīng)越過了紅線,這清楚地表明它已經(jīng)擴大了?,F(xiàn)在讓我們對水平和垂直擴張進行迭代。
ites = [2,4,6,8,10,12,14,16,18,20]
fig, ax = plt.subplots(2, 5, figsize=(17, 5))
for n, ax in enumerate(ax.flatten()):
ax.set_title(f'Horizontal Iterations : {ites[n]}', fontsize =
12)
new_circle = multi_dilation(circ_image, h_line, ites[n])
ax.imshow(new_circle, cmap = 'gray');
ax.axis('off')
fig.tight_layout()
fig, ax = plt.subplots(2, 5, figsize=(17, 5))
for n, ax in enumerate(ax.flatten()):
ax.set_title(f'Vertical Iterationss : {ites[n]}', fontsize = 12)
new_circle = multi_dilation(circ_image, v_line, ites[n])
ax.imshow(new_circle, cmap = 'gray');
ax.axis('off')
fig.tight_layout()

現(xiàn)在可以非常清楚地看到,水平擴張增加了圖像寬度,而垂直擴張增加了圖像高度。
現(xiàn)在我們已經(jīng)了解了膨脹與腐蝕的基本原理,下面來看一個相對復(fù)雜的圖像。
complex_image = imread('complex_image.png')
imshow(complex_image);

在上面的圖像中,我們看到了水平線、垂直線和圓的混合物。我們可以使用膨脹和腐蝕函數(shù)孤立地觀察每一種形狀。
為了得到圓,我們可以先腐蝕垂直的線,再腐蝕水平的線。但要記住最后要對圖像進行膨脹,因為腐蝕函數(shù)同樣腐蝕了圓。
step_1 = multi_erosion(complex_image, h_line,3)
step_2 = multi_erosion(step_1, v_line,3)
step_3 = multi_dilation(step_2, h_line,3)
step_4 = multi_dilation(step_3, v_line,3)
steps = [step_1, step_2, step_3, step_4]
names = ['Step 1', 'Step 2', 'Step 3', 'Step 4']
fig, ax = plt.subplots(2, 2, figsize=(10, 10))
for n, ax in enumerate(ax.flatten()):
ax.set_title(f'{names[n]}', fontsize = 22)
ax.imshow(steps[n], cmap = 'gray');
ax.axis('off')
fig.tight_layout()

同樣,下面的代碼將得到水平的線。
step_1 = multi_erosion(complex_image, cross, 20)
step_2 = multi_dilation(step_1, h_line, 20)
step_3 = multi_dilation(step_2, v_line,2)
steps = [step_1, step_2, step_3]
names = ['Step 1', 'Step 2', 'Step 3']
fig, ax = plt.subplots(1, 3, figsize=(10, 10))
for n, ax in enumerate(ax.flatten()):
ax.set_title(f'{names[n]}', fontsize = 22)
ax.imshow(steps[n], cmap = 'gray');
ax.axis('off')
fig.tight_layout()

為了得到垂直的線,我們可以創(chuàng)建一個新的內(nèi)核。
long_v_line = np.array([[0,1,0],
[0,1,0],
[0,1,0],
[0,1,0],
[0,1,0]])
step_1 = multi_erosion(complex_image, long_v_line, 10)
step_2 = multi_dilation(step_1 ,long_v_line, 10)
steps = [step_1, step_2]
names = ['Step 1', 'Step 2']
fig, ax = plt.subplots(1, 2, figsize=(10, 10))
for n, ax in enumerate(ax.flatten()):
ax.set_title(f'{names[n]}', fontsize = 22)
ax.imshow(steps[n], cmap = 'gray');
ax.axis('off')
fig.tight_layout()

注意,內(nèi)核并不局限于本文中提到的這幾種,可以根據(jù)不同的需求自己定義合適的內(nèi)核。
總結(jié)
內(nèi)核腐蝕和膨脹是圖像處理領(lǐng)域需要理解的基本概念。它們甚至可能是任何圖像處理模塊的第一課。直觀地理解它們將是你以后在這個領(lǐng)域成功的關(guān)鍵。
到此這篇關(guān)于Python圖像處理之膨脹與腐蝕的操作的文章就介紹到這了,更多相關(guān)Python圖像膨脹與腐蝕內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
tensorflow estimator 使用hook實現(xiàn)finetune方式
今天小編就為大家分享一篇tensorflow estimator 使用hook實現(xiàn)finetune方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
如何用python 操作MongoDB數(shù)據(jù)庫
這篇文章主要介紹了如何用python 操作MongoDB數(shù)據(jù)庫,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下2021-04-04
python爬蟲使用requests發(fā)送post請求示例詳解
這篇文章主要介紹了python爬蟲使用requests發(fā)送post請求示例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
Python基礎(chǔ)教程,Python入門教程(超詳細)
Python由荷蘭數(shù)學和計算機科學研究學會 于1990 年代初設(shè)計,作為一門叫做ABC語言的替代品。Python語法和動態(tài)類型,以及解釋型語言的本質(zhì),使它成為多數(shù)平臺上寫腳本和快速開發(fā)應(yīng)用的編程語言2021-06-06

