【医疗影像处理】对分割的mask做处理,保留最大联通区域

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.

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值