scipy.ndimage.
binary_hit_or_miss#
- scipy.ndimage.binary_hit_or_miss(input, structure1=None, structure2=None, output=None, origin1=0, origin2=None, *, axes=None)[source]#
多維二元擊中或錯失變換。
擊中或錯失變換會在輸入影像中找到給定樣式的位置。
- 參數:
- inputarray_like (轉換為布林值)
要偵測樣式的二元影像。
- structure1array_like (轉換為布林值),選用
結構元素的其中一部分,要擬合到 input 的前景(非零元素)。如果未提供值,則會選擇連通性為 1 的方形結構。
- structure2array_like (轉換為布林值),選用
結構元素的第二部分,必須完全錯失前景。如果未提供值,則會採用 structure1 的互補結構。
- outputndarray,選用
與輸入形狀相同的陣列,輸出會放置到其中。預設情況下,會建立一個新陣列。
- origin1int 或 int 元組,選用
結構元素 structure1 第一部分的放置位置,預設情況下,中心結構為 0。
- origin2int 或 int 元組,選用
結構元素 structure2 第二部分的放置位置,預設情況下,中心結構為 0。如果為 origin1 提供值,而未為 origin2 提供值,則 origin2 會設定為 origin1。
- axesint 元組或 None
要套用篩選器的軸。如果為 None,則會沿所有軸篩選 input。如果提供 origin1 或 origin2 元組,則其長度必須與軸數相符。
- 返回:
- binary_hit_or_missndarray
使用給定結構元素 (structure1、structure2) 的 input 的擊中或錯失變換。
另請參閱
參考文獻
範例
>>> from scipy import ndimage >>> import numpy as np >>> a = np.zeros((7,7), dtype=int) >>> a[1, 1] = 1; a[2:4, 2:4] = 1; a[4:6, 4:6] = 1 >>> a array([[0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> structure1 = np.array([[1, 0, 0], [0, 1, 1], [0, 1, 1]]) >>> structure1 array([[1, 0, 0], [0, 1, 1], [0, 1, 1]]) >>> # Find the matches of structure1 in the array a >>> ndimage.binary_hit_or_miss(a, structure1=structure1).astype(int) array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]) >>> # Change the origin of the filter >>> # origin1=1 is equivalent to origin1=(1,1) here >>> ndimage.binary_hit_or_miss(a, structure1=structure1,\ ... origin1=1).astype(int) array([[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0]])