scipy.ndimage.

maximum#

scipy.ndimage.maximum(input, labels=None, index=None)[source]#

計算標記區域上陣列值的最大值。

參數:
inputarray_like

類陣列的值。對於由 labels 指定的每個區域,計算該區域上 input 的最大值。

labelsarray_like, optional

一個整數陣列,標記不同的區域,在這些區域上計算 input 的最大值。labels 必須與 input 具有相同的形狀。如果未指定 labels,則返回整個陣列的最大值。

indexarray_like, optional

用於計算最大值時考慮的區域標籤列表。如果 index 為 None,則返回 labels 為非零的所有元素的最大值。

返回:
outputfloat 或 float 列表

labels 確定的區域上以及索引在 index 中的 input 的最大值列表。如果未指定 indexlabels,則返回一個 float:如果 labels 為 None,則為 input 的最大值;如果 index 為 None,則為 labels 大於零的元素的最大值。

說明

此函數返回一個 Python 列表,而不是 NumPy 陣列,請使用 np.array 將列表轉換為陣列。

範例

>>> import numpy as np
>>> a = np.arange(16).reshape((4,4))
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
>>> labels = np.zeros_like(a)
>>> labels[:2,:2] = 1
>>> labels[2:, 1:3] = 2
>>> labels
array([[1, 1, 0, 0],
       [1, 1, 0, 0],
       [0, 2, 2, 0],
       [0, 2, 2, 0]])
>>> from scipy import ndimage
>>> ndimage.maximum(a)
15
>>> ndimage.maximum(a, labels=labels, index=[1,2])
[5, 14]
>>> ndimage.maximum(a, labels=labels)
14
>>> b = np.array([[1, 2, 0, 0],
...               [5, 3, 0, 4],
...               [0, 0, 0, 7],
...               [9, 3, 0, 0]])
>>> labels, labels_nb = ndimage.label(b)
>>> labels
array([[1, 1, 0, 0],
       [1, 1, 0, 2],
       [0, 0, 0, 2],
       [3, 3, 0, 0]], dtype=int32)
>>> ndimage.maximum(b, labels=labels, index=np.arange(1, labels_nb + 1))
[5, 7, 9]