scipy.ndimage.

maximum_position#

scipy.ndimage.maximum_position(input, labels=None, index=None)[原始碼]#

找出標籤陣列中,於各標籤區域內陣列數值的最大值位置。

對於 labels 指定的每個區域,會返回 input 在該區域內最大值的位置。

參數:
inputarray_like

數值之類陣列。

labelsarray_like,選用

整數陣列,標記要計算 input 最大值位置的不同區域。labels 必須與 input 具有相同的形狀。如果未指定 labels,則會返回整個陣列中第一個最大值的位置。

labels 參數僅在指定 index 時有效。

indexarray_like,選用

用於尋找最大值位置的區域標籤列表。如果 index 為 None,則會返回 labels 為非零的所有元素中第一個最大值的位置。

index 參數僅在指定 labels 時有效。

返回:
outputtuple of ints 列表

tuple of ints 列表,指定 input 在由 labels 確定且其索引在 index 中的區域上的最大值位置。

如果未指定 indexlabels,則會返回一個 tuple of ints,指定 inputfirst 最大值的位置。

範例

>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.array([[1, 2, 0, 0],
...               [5, 3, 0, 4],
...               [0, 0, 0, 7],
...               [9, 3, 0, 0]])
>>> ndimage.maximum_position(a)
(3, 0)

可以使用 labelsindex 指定要處理的特徵

>>> lbl = np.array([[0, 1, 2, 3],
...                 [0, 1, 2, 3],
...                 [0, 1, 2, 3],
...                 [0, 1, 2, 3]])
>>> ndimage.maximum_position(a, lbl, 1)
(1, 1)

如果沒有給定索引,則會處理非零的 labels

>>> ndimage.maximum_position(a, lbl)
(2, 3)

如果沒有最大值,則返回第一個元素的位置

>>> ndimage.maximum_position(a, lbl, 2)
(0, 2)