最基礎的形態學操作有四個,分別是腐蝕、膨脹、開計算和閉計算,`scipy.ndimage分別實現了二值數組和灰度數組的這四種運算
二值 | 灰階 | |
---|---|---|
#binary_erosion | grey_erosion | 腐蝕 |
binary_dilation | ||
##binary_closing | ||
閉(先膨脹後腐蝕) | binary_opening |
開(先腐蝕後膨脹)
二值形態學
所謂腐蝕,用數學符號表示為
##其中B
ij表示當B BB的原點在(i,j)處時,B中所有為1的值的集合。
這個式子的意思是,用結構B腐蝕A,當B的原點平移到圖像A的像元(i,j)時,若B完全被二者的重疊區域所包圍,則賦值為1,否則賦值為0。當B中某個元素為1時,若A中對應位置也是1,則(i,j)處的值為1,這是較直觀的例子。
換言之,只要B和A的重疊區域不是空集,那麼(i,j)點就置為1。
舉例如下
import numpy as np import matplotlib.pyplot as plt import scipy.ndimage as sn x = np.zeros([20,20]) x[5:15, 5:15] = 1 x_ero = sn.binary_erosion(x) x_dil = sn.binary_dilation(x) fig = plt.figure() ax = fig.add_subplot(1,3,1) ax.imshow(x) plt.title("original") ax = fig.add_subplot(1,3,2) ax.imshow(x_ero) plt.title("erosion") ax = fig.add_subplot(1,3,3) ax.imshow(x_dil) plt.title("dilation") plt.show()效果如下
#開運算是先腐蝕後膨脹;閉運算是先膨脹後腐蝕,範例如下
x = np.zeros([20,20]) x[5:15, 5:15] = 1 x[10:12,10:12] = 0 x[2:4, 2:4] = 1 x_open = sn.binary_opening(x) x_close = sn.binary_closing(x) fig = plt.figure() ax = fig.add_subplot(1,3,1) ax.imshow(x) plt.title("original") ax = fig.add_subplot(1,3,2) ax.imshow(x_open) plt.title("opening") ax = fig.add_subplot(1,3,3) ax.imshow(x_close) plt.title("closing") plt.show()效果如下,可見開運算會移除孤立的1,閉運算會移除孤立的0。
灰階形態學
灰階影像的腐蝕、膨脹以及開閉運算,是其二值形勢下的一個擴展,採用了類似卷積的邏輯,下面直接從scipy中調取樓梯圖片,並依序做腐蝕、膨脹以及開閉操作。
from scipy.misc import ascent img = ascent() funcs = { "original": lambda x, tmp:x, "erosion" : sn.grey_erosion, "dilation" : sn.grey_dilation, "opening" : sn.grey_opening, "closing" : sn.grey_closing } fig = plt.figure() for i, key in enumerate(funcs): ax = fig.add_subplot(2,3,i+1) plt.imshow(funcs[key](img, (10,10)), cmap=plt.cm.gray) plt.title(key) plt.show()參數列表
二值函數和灰階函數的參數並不相同,下面以closing運算為例,二值和灰階函數的所有參數,除了輸入input之外,二者共有的參數有
structure 為數組類型,表示建構元素,可以理解為是卷積模板
-
output 與輸入相同維度的數組,可以存下結果
orgin 過濾器設置,預設為0
##二值形態學濾波的其他參數如下binary_closing(input, iterations=1, mask=None, border_value=0, brute_force=False)
grey_closing(input, size=None, footprint=None, mode='reflect', cval=0.0)
- size 為濾波模板 ##mode 可選reflect,constant, nearest,mirror, wrap,邊緣填滿方式
- cval 邊緣填滿值
以上是Python基本形態學濾波怎麼實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了版本3.10中介紹的Python的新“匹配”語句,該語句與其他語言相同。它增強了代碼的可讀性,並為傳統的if-elif-el提供了性能優勢

Python中的功能註釋將元數據添加到函數中,以進行類型檢查,文檔和IDE支持。它們增強了代碼的可讀性,維護,並且在API開發,數據科學和圖書館創建中至關重要。

本文討論了Python中的單位測試,其好處以及如何有效編寫它們。它突出顯示了諸如UNITSEST和PYTEST之類的工具進行測試。

文章討論了Python的\ _ \ _ Init \ _ \ _()方法和Self在初始化對象屬性中的作用。還涵蓋了其他類方法和繼承對\ _ \ _ Init \ _ \ _()的影響。

本文討論了python中@classmethod,@staticmethod和實例方法之間的差異,詳細介紹了它們的屬性,用例和好處。它說明瞭如何根據所需功能選擇正確的方法類型和DA

Inpython,YouAppendElementStoAlistusingTheAppend()方法。 1)useappend()forsingleelements:my_list.append(4).2)useextend()orextend()或= formultiplelements:my_list.extend.extend(emote_list)ormy_list = [4,5,6] .3)useInsert()forspefificpositions:my_list.insert(1,5).beaware


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具