Fel*_*lix 5 python numpy image-processing
我有一个二进制图像:numpy.ndarray(dtype=bool)
. 它有数百个充满True
价值的连接区域。
但我只对一个地区感兴趣。我知道其中一个元素的位置,并想找出这个感兴趣区域的边界框(也可能是这个区域其他点的位置)。
最好的方法是什么?
根据图像的大小,标记图像以获取所有连接的组件可能是最简单的方法。使用已知像素的标签来获取连接的像素。使用和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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3240 次 |
最近记录: |