Python:如果我知道原点像素的位置,如何找到所有连接的像素?

Fel*_*lix 5 python numpy image-processing

我有一个二进制图像:numpy.ndarray(dtype=bool). 它有数百个充满True价值的连接区域。

但我只对一个地区感兴趣。我知道其中一个元素的位置,并想找出这个感兴趣区域的边界框(也可能是这个区域其他点的位置)。

最好的方法是什么?

Log*_*ers 6

根据图像的大小,标记图像以获取所有连接的组件可能是最简单的方法。使用已知像素的标签来获取连接的像素。使用和skimage使这变得非常简单。请务必了解 的或参数,因为它会影响对角线邻居是否接触。skimage.measure.labelskimage.measure.regionpropsconnectivityneighborslabel

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
Run Code Online (Sandbox Code Playgroud)