画像のサイズによっては、画像にラベルを付けてすべての接続コンポーネントにするのが最も簡単です。既知のピクセルのラベルを使用して、接続されたピクセルも取得します。 skimage
は、skimage.measure.label
とskimage.measure.regionprops
を使ってこれを実際に簡単にします。 connectivity
またはneighbors
のパラメータがlabel
になっていることを理解しておいてください。これは、対角隣接が接触するかどうかに影響します。
from skimage import measure
import numpy as np
# load array; arr = np.ndarray(...)
# arr = np.zeros((10,10), dtype=bool)
# arr[:2,:2] = True
# arr[-4:,-4:] = True
labeled = measure.label(arr, background=False, connectivity=2)
label = labeled[8,8] # known pixel location
rp = measure.regionprops(labeled)
props = rp[label - 1] # background is labeled 0, not in rp
props.bbox # (min_row, min_col, max_row, max_col)
props.image # array matching the bbox sub-image
props.coordinates # list of (row,col) pixel indices