欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

保留三维体数据的mask的ROI区域---Python

程序员文章站 2022-05-04 14:14:59
...

针对最近的心脏细分割/肝脏分割项目,任务的划分分为两个阶段:

1、心脏/肝脏的粗定位;

2、心脏/肝脏的细分割;

第一步:粗定位(在原始标记上获取ROI)

def FindLabelBox3D(img, offset):
    '''
    img:ct-label-data
    offset:copy-level
    '''
    xdim = np.zeros(2)  # bouding box 和 x轴的交点
    ydim = np.zeros(2)  # bouding box 和 y轴的交点
    zdim = np.zeros(2)  # bouding box 和 z轴的交点
    tmp = np.squeeze(np.sum(np.sum(img, axis=2), axis=1))
    print("for x:", len(tmp))
    for i in range(len(tmp)):
        if tmp[i] == 0:
            xdim[0] = i
        else:
            break
    xdim[1] = len(tmp)
    for i in reversed(range(len(tmp))):
        if tmp[i] == 0:
            xdim[1] = i
        else:
            break
    # for y
    tmp = np.squeeze(np.sum(np.sum(img, axis=2), axis=0))
    print("for y:", len(tmp))
    for i in range(len(tmp)):
        if tmp[i] == 0:
            ydim[0] = i
        else:
            break

    ydim[1] = len(tmp)
    for i in reversed(range(len(tmp))):
        if tmp[i] == 0:
            ydim[1] = i
        else:
            break
    # for z
    tmp = np.squeeze(np.sum(np.sum(img, axis=1), axis=0))
    print("for z:", len(tmp))
    for i in range(len(tmp)):
        if tmp[i] == 0:
            zdim[0] = i
        else:
            break

    zdim[1] = len(tmp)
    for i in reversed(range(len(tmp))):
        if tmp[i] == 0:
            zdim[1] = i
        else:
            break

    # offset
    xdim[0] = max(0, xdim[0] - offset)
    xdim[1] = min(np.size(img, 0), xdim[1] + offset)

    ydim[0] = max(0, ydim[0] - offset)
    ydim[1] = min(np.size(img, 1), ydim[1] + offset)

    zdim[0] = max(0, zdim[0] - offset)
    zdim[1] = min(np.size(img, 2), zdim[1] + offset)

    return xdim, ydim, zdim

上述函数可以根据label-标记获取到ROI区域;

获取对应的label-区域与image-区域:

xdim, ydim, zdim = FindLabelBox3D(label, 0)
##image-roi
image_bouding_box = image_data[xdim[0]:xdim[1], ydim[0]:ydim[1], zdim[0]:zdim[1]]
##label-roi
label_bouding_box = label_data[xdim[0]:xdim[1], ydim[0]:ydim[1], zdim[0]:zdim[1]]

上述的image_bouding_box与label_bouding_box即为获取的ROI。

相关标签: 医学图像处理