コード例 #1
0
ファイル: luna16.py プロジェクト: a53769/Vnet
def normalize_lung_CT(**kwargs):
    mean_values = []
    var_values = []
    MIN_BOUND = -1000
    MAX_BOUND = 400
    Z_MAX, Y_MAX, X_MAX = kwargs['Z_MAX'], kwargs['Y_MAX'], kwargs['X_MAX']
    vox_spacing = kwargs['vox_spacing']
    utils.init_dims3D(Z_MAX, Y_MAX, X_MAX, vox_spacing)
    luna_subset_path = kwargs['src']
    luna_save_path = kwargs['dst']
    file_list=glob(luna_subset_path + "/" + "*.mhd")
    img_spacing = (vox_spacing, vox_spacing, vox_spacing)

    for img_file in file_list:
        itk_img = sitk.ReadImage(img_file)
        (x_space, y_space, z_space) = itk_img.GetSpacing()
        spacing_old = (z_space, y_space, x_space)
        img_array = sitk.GetArrayFromImage(itk_img) # indexes are z,y,x (notice the ordering)
        img, mu, var = utils.resample_volume(img_array, spacing_old, img_spacing, bounds=(MIN_BOUND, MAX_BOUND))
        utils.save_updated_image(img, luna_save_path+os.path.basename(img_file), itk_img.GetOrigin(), img_spacing)
        mean_values.append(mu)
        var_values.append(var)
    dataset_mean = np.mean(mean_values)
    dataset_stddev = np.sqrt(np.mean(var_values))
    return (dataset_mean, dataset_stddev)
コード例 #2
0
ファイル: luna16.py プロジェクト: a53769/Vnet
def normalize_lung_mask(**kwargs):
    Z_MAX, Y_MAX, X_MAX = kwargs['Z_MAX'], kwargs['Y_MAX'], kwargs['X_MAX']
    vox_spacing = kwargs['vox_spacing']
    utils.init_dims3D(Z_MAX, Y_MAX, X_MAX, vox_spacing)
    luna_seg_lungs_path = kwargs['src']
    luna_seg_lungs_save_path = kwargs['dst']
    file_list=glob(os.path.join(luna_seg_lungs_path, "*.mhd"))
    img_spacing = (vox_spacing, vox_spacing, vox_spacing)
    for img_file in file_list:
        itk_img = sitk.ReadImage(img_file)
        (x_space, y_space, z_space) = itk_img.GetSpacing()
        spacing_old = (z_space, y_space, x_space)
        img_array = sitk.GetArrayFromImage(itk_img) # indexes are z,y,x (notice the ordering)
        img, _, _ = utils.resample_volume(img_array, spacing_old, img_spacing)
        img[img < 1] = 0
        utils.save_updated_image(img, os.path.join(luna_seg_lungs_save_path, os.path.basename(img_file)),
                                 itk_img.GetOrigin(), img_spacing)
コード例 #3
0
ファイル: luna16.py プロジェクト: a53769/Vnet
def normalize_nodule_mask(**kwargs):
    luna_path = kwargs['orig']
    pixel_count = 0
    mask_count = 0
    annotations = kwargs['annotations']
    Z_MAX, Y_MAX, X_MAX = kwargs['Z_MAX'], kwargs['Y_MAX'], kwargs['X_MAX']
    shape_max = (Z_MAX, Y_MAX, X_MAX)
    vox_spacing = kwargs['vox_spacing']
    tables_path = kwargs['tables']
    utils.init_dims3D(Z_MAX, Y_MAX, X_MAX, vox_spacing)
    x_list, y_list, z_list = [], [], []
    luna_normal_path = kwargs['src']
    luna_mask_path = kwargs['dst']

    def get_boundaries(origin, offsets, params):
        diam, center = params
        diam3 = np.array((diam, diam, diam))
        diamu = diam + vox_spacing
        diam3u = np.array((diamu, diamu, diamu))
        v_center = np.rint((center - origin)/vox_spacing)
        v_lower = np.rint((center - diam3 - origin)/vox_spacing)
        v_upper = np.rint((center + diam3u - origin)/vox_spacing)
        v_center -= offsets
        v_lower -= offsets
        v_upper -= offsets
        x_list.append(v_upper[2])
        y_list.append(v_upper[1])
        z_list.append(v_upper[0])
        x_list.append(v_lower[2])
        y_list.append(v_lower[1])
        z_list.append(v_lower[0])
        return (v_lower, v_center, v_upper)

    def l2_norm(pointA, pointB):
        point = pointA - pointB
        return np.sqrt(np.dot(point, point))

    def get_filename(case):
        for f in file_list:
            if case in f:
                return(f)

    def update_mask(mask, CT, bounds):
        v_lower, v_center, v_upper = bounds
        z_min, y_min, x_min = v_lower
        z_max, y_max, x_max = v_upper
        pixel_count = 0
        radius = np.rint((z_max - z_min + vox_spacing)/2)
        ct_thresh = -1000
        bit_count = 0
        #print(v_center)
        for z in range(z_min, z_max):
            for y in range(y_min, y_max):
                for x in range(x_min, x_max):
                    if l2_norm(np.array((z, y, x)), v_center) > radius:
                        break
                    if CT[z][y][x] > ct_thresh:
                        mask[z][y][x] = 4
                        pixel_count += 1
                        bit_count += 1
        #assert bit_count != 0
        if bit_count == 0:
            print(v_center)

    origin_dict = utils.npz_load(os.path.join(tables_path, "origin_table"))
    offset_dict = utils.npz_load(os.path.join(tables_path, "offset_table"))

    file_list=glob(luna_normal_path+"*.mhd")
    df_node = pd.read_csv(annotations)
    df_node["file"] = df_node["seriesuid"].apply(get_filename)
    df_node = df_node.dropna()
    img_spacing = (vox_spacing, vox_spacing, vox_spacing)
    count = 0
    for img_file in file_list:
        mask_count = 0
        mini_df = df_node[df_node["file"]==img_file] #get all nodules associate with file
        if len(mini_df) == 0:
            continue
        mask = np.zeros(shape_max, dtype=np.int16)
        series = os.path.basename(img_file)[0:-4]
        origin = origin_dict[series]
        if origin[1] > 0 and origin[2] > 0:
            origin[1] = -origin[1]
            origin[2] = -origin[2]
        offsets = offset_dict[series]
        itk_img = sitk.ReadImage(img_file)
        img_array = sitk.GetArrayFromImage(itk_img)
        for i in range(len(mini_df)):
            node_x = mini_df["coordX"].values[i]
            node_y = mini_df["coordY"].values[i]
            node_z = mini_df["coordZ"].values[i]
            diam = mini_df["diameter_mm"].values[i]
            params = (diam, np.array((node_z, node_y, node_x)))
            bounds = get_boundaries(origin, offsets, params)
            _, v_center, _ = bounds
            if np.min(v_center) < 0:
                print("origin: {} offsets: {}\n params: {} v_center: {}".format(
                    origin, offsets, params, v_center))
                continue
            bounds = np.array(bounds).astype(np.int16)
            update_mask(mask, img_array, bounds)
            mask_count += 1
        assert mask_count != 0
        itk_mask_img = sitk.GetImageFromArray(mask, isVector=False)
        itk_mask_img.SetSpacing(img_spacing)
        itk_mask_img.SetOrigin(origin)
        sitk.WriteImage(itk_mask_img, luna_mask_path+'/'+os.path.basename(img_file))