在深度学习模型预测过程中,对生成的mask(0,1二值图)有多个连通区域的情况,有时我们只需要对其中某一个连通区域操作,思路就是对不同连通区域赋给不同的值。
from skimage.measure import label as label_function
import numpy as np
copy_label_brain = copy.deepcopy(label_brain)
z,x,y = np.shape(label_brain)
temporal_r = np.zeros((z,x,y), np.uint8)
temporal_l = np.zeros((z,x,y), np.uint8)
for i in range(z):
copy_label_brain[i,:,:] = label_function(copy_label_brain[i,:,:])
if np.max(copy_label_brain[i])==3:
#保留2,3区域
temporal_r[i,:,:] = np.where(copy_label_brain[i,:,:]>1,1,0)
temporal_r[i,:,:] = label_brain[i,:,:] - temporal_r[i,:,:]
#保留3区域
temporal_l[i,:,:] = np.where(copy_label_brain[i,:,:]>2,1,0)
temporal_l[i,:,:] = label_brain[i,:,:] - temporal_r[i,:,:] - temporal_l[i,:,:]
三个连通区域赋不同值.png
网友评论