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

发布时间:2024年01月12日
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)

文章来源:https://blog.csdn.net/sdhdsf132452/article/details/135550153
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。