给一幅分割 label,求某个物体的凸包(convex hull)[1]和包络(polygon)[2],所得是一幅 0/1 的 mask。凸包、包络都是包含物体的,分别在于包络不要求凸,可以更细致地勾勒物体形状。例:
从左到右:此物体的 segmentation mask、包络 mask、凸包 mask。包络、凸包两 mask 或可用于 masked dice loss[3]。
Code
- 包络:skimage.draw.polygon
- 凸包:skimage.morphology.convex_hull_image
import numpy as np
import medpy.io as medio
from PIL import Image
import skimage# 读一幅 seg label
label, _ = medio.load("test/ctpelvic1k/dataset5_1411226_Image_mask_4label.nii.gz")
print(label.shape, label.min(), label.max(), label.dtype) # (512, 512, 175) 0 4 uint16
lab = label[:, :, 100]# object seg mask
lab_bin = (lab > 0).astype(np.uint8) * 255# polygon
px, py = np.where(lab > 0)
rr, cc = skimage.draw.polygon(px, py)
polygon = np.zeros_like(lab, dtype=np.uint8)
polygon[rr, cc] = 255# convex hull
chull = skimage.morphology.convex_hull_image(lab_bin)
print(chull.shape, chull.min(), chull.max(), chull.dtype) # (512, 512) False True bool
chull = chull.astype(np.uint8) * 255# 一并展示
comb = np.concatenate([lab_bin, polygon, chull], axis=1)
Image.fromarray(comb).save("test.png")
References
- Convex Hull
- skimage.draw.polygon
- MaskedDiceLoss