當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python SciPy ndimage.binary_closing用法及代碼示例

本文簡要介紹 python 語言中 scipy.ndimage.binary_closing 的用法。

用法:

scipy.ndimage.binary_closing(input, structure=None, iterations=1, output=None, origin=0, mask=None, border_value=0, brute_force=False)#

與給定結構元素的多維二進製關閉。

結構元素對輸入圖像的閉合是結構元素對圖像膨脹的侵蝕。

參數

input array_like

二進製數組 被關閉。非零 (True) 元素構成要關閉的子集。

structure 數組,可選

用於關閉的結構元素。非零元素被認為是 True。如果沒有提供結構元素,則生成一個正方形連通性等於 1 的元素(即,隻有最近的鄰居連接到中心,diagonally-connected 元素不被視為鄰居)。

iterations 整數,可選

閉合的膨脹步驟,然後是腐蝕步驟,每次重複迭代次數(默認為一次)。如果迭代次數小於 1,則重複每個操作,直到結果不再變化。隻接受整數次迭代。

output ndarray,可選

與輸入形狀相同的數組,其中放置輸出。默認情況下,會創建一個新數組。

origin int 或整數元組,可選

過濾器的位置,默認為 0。

mask 數組,可選

如果給定掩碼,則每次迭代時僅修改在相應掩碼元素處具有 True 值的那些元素。

border_value int(強製轉換為 0 或 1),可選

輸出數組中邊界處的值。

brute_force 布爾值,可選

memory 條件:如果為False,則隻跟蹤上次迭代中值發生變化的像素作為本次迭代更新的候選;如果真正的 al 像素被認為是更新的候選者,則不管前一次迭代中發生了什麽。默認為假。

返回

binary_closing 布爾數組

通過結構元素關閉輸入。

注意

結束 [1]是數學形態學運算[2]這包括具有相同結構元素的輸入的連續膨脹和腐蝕。因此,閉合會填充小於結構元素的孔。

和...一起開幕(scipy.ndimage.binary_opening),關閉可用於消除噪音。

參考

例子

>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.zeros((5,5), dtype=int)
>>> a[1:-1, 1:-1] = 1; a[2,2] = 0
>>> a
array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 0, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]])
>>> # Closing removes small holes
>>> ndimage.binary_closing(a).astype(int)
array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]])
>>> # Closing is the erosion of the dilation of the input
>>> ndimage.binary_dilation(a).astype(int)
array([[0, 1, 1, 1, 0],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [0, 1, 1, 1, 0]])
>>> ndimage.binary_erosion(ndimage.binary_dilation(a)).astype(int)
array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]])
>>> a = np.zeros((7,7), dtype=int)
>>> a[1:6, 2:5] = 1; a[1:3,3] = 0
>>> a
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 1, 0, 0],
       [0, 0, 1, 0, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])
>>> # In addition to removing holes, closing can also
>>> # coarsen boundaries with fine hollows.
>>> ndimage.binary_closing(a).astype(int)
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])
>>> ndimage.binary_closing(a, structure=np.ones((2,2))).astype(int)
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 1, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])

相關用法


注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.ndimage.binary_closing。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。