API

  • skimage.measure

https://scikit-image.org/docs/dev/api/skimage.measure.html

out_arr 为得到的结果mask矩阵。

from skimage import measure
labels = measure.label(out_arr, neighbors=8)
print(np.unique(labels))

[ 0 , 1 , 2 , 3 , 4 ]

0 为背景

max_num = 0
for j in range(1, np.max(labels)+1):
    if np.sum(labels==j) > max_num:
        max_num = np.sum(labels==j)
        max_pixel = j
    if np.sum(labels==j)>0.1*np.sum(labels!=0):
        labels[labels==j] = max_pixel 
labels[labels != max_pixel]=0
labels[labels == max_pixel]=1
labels = np.array(labels,dtype=np.int8)

此时只保留了面积最大的联通区域。

measure.label 有参数为联通选择——neighbors
在这里插入图片描述
neighbors : {4, 8}, int, optional
Whether to use 4- or 8-“connectivity”. In 3D, 4-“connectivity” means connected pixels have to share face, whereas with 8-“connectivity”, they have to share only edge or vertex. Deprecated, use connectivity instead.

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐