计算三维医学图像二值numpy数组的边界

import numpy as np
import SimpleITK as sitk
import os
from tqdm import tqdm

def cal_board(label_array,index):
    """calculate borader
    0: depth
    1: hight
    2: width
    """
    D,H,W = label_array.shape    
    dots = np.argwhere(label_array==1)
    mins = np.min(dots,axis=0) # z,y,x // 这个一定是0
    maxs = np.max(dots,axis=0) # z,y,x // 这个一定是0
    print(mins)
    print(maxs)
    low_boarder, high_boarder = mins[index],maxs[index] 

    return low_boarder, high_boarder
                                                                                                                                                                                                                 
def normalize_img(img:np.ndarray,min_val=-1000,max_val=600)->np.ndarray:
    """ 归一化 """
    # max_val = np.max(img)
    # min_val = np.min(img)
    value_range = max_val - min_val
    norm_0_1 = (img-min_val)/value_range
    img = np.clip(2*norm_0_1-1,-1,1)        
    return img


if __name__ == '__main__':
    x = np.array([[[0,1,1,0],[0,0,1,1]],[[0,0,0,0],[0,0,1,1]]])
    print(x)
    print(x.shape)

    # 求z的boarder
    low, high = cal_board(x, 0)
    print(low, high)

    # 求h的boarder
    low, high = cal_board(x, 1)
    print(low, high)

    # 求w的boarder
    low, high = cal_board(x, 2)
    print(low, high)

你可能感兴趣的:(医学图像处理1,numpy)