def process(sample_file,
            sample_path,
            sample_save_path,
            pic_save_path,
            threhold=-150.0):
    print("Trying to process %s..." % sample_file)
    array = np.load(os.path.join(sample_path, sample_file))
    array[array >= threhold] = -1000.0

    file_name = sample_file.split(".")[0]
    #print(file_name)
    np.save(os.path.join(sample_save_path, file_name + ".npy"), array)
    plot_arr(array, os.path.join(pic_save_path, file_name + ".png"))
Esempio n. 2
0
def sample_sph(cfg, threhold=-150.0):
    label_df = pd.read_csv(cfg.label_file)
    #count = 0
    non_case = []
    for i in label_df.index:
        scan_path = label_df.loc[i, "patient_ID"]
        print("Start sample nodule_%d of %s" % (int(i), scan_path))
        if os.path.exists(
                os.path.join(cfg.extract_path,
                             scan_path)) and int(i) not in cfg.deviant_index:
            scan_array = read_scan(os.path.join(cfg.extract_path, scan_path))

            nodule_coords = label_df.loc[i, "coord_XYZ"].split(" ")
            nodule_x = int(nodule_coords[0])
            nodule_y = int(nodule_coords[1])
            nodule_z = int(nodule_coords[2])
            edge_z_min = nodule_z - cfg.cube_size // 4
            edge_z_max = nodule_z + cfg.cube_size // 4
            edge_y_min = nodule_y - cfg.cube_size // 2
            edge_y_max = nodule_y + cfg.cube_size // 2
            edge_x_min = nodule_x - cfg.cube_size // 2
            edge_x_max = nodule_x + cfg.cube_size // 2
            print(edge_z_min, edge_z_max, edge_y_min, edge_y_max, edge_x_min,
                  edge_x_max)
            nodule_array = scan_array[edge_z_min:edge_z_max,
                                      edge_y_min:edge_y_max,
                                      edge_x_min:edge_x_max]

            nodule_64_array = np.ones((32, 64, 64), dtype=np.float32)
            nodule_64_array *= -1000.0
            padding_z_min = (nodule_64_array.shape[0] -
                             nodule_array.shape[0]) // 2
            padding_z_max = padding_z_min + nodule_array.shape[0]
            padding_y_min = (nodule_64_array.shape[1] -
                             nodule_array.shape[1]) // 2
            padding_y_max = padding_y_min + nodule_array.shape[1]
            padding_x_min = (nodule_64_array.shape[2] -
                             nodule_array.shape[2]) // 2
            padding_x_max = padding_x_min + nodule_array.shape[2]

            nodule_64_array[padding_z_min:padding_z_max,
                            padding_y_min:padding_y_max,
                            padding_x_min:padding_x_max] = nodule_array
            nodule_64_array[nodule_64_array > threhold] = -1000.0

            save_name = "nodule_" + str(int(i)).zfill(3) + "_" + scan_path
            picture_path = os.path.join(cfg.p_v3_path, "sample_pictures/")
            output_path = os.path.join(cfg.p_v3_path, "samples/")
            if not os.path.exists(picture_path):
                os.makedirs(picture_path)
            if not os.path.exists(output_path):
                os.makedirs(output_path)
            print("Save nodule %d of %s" % (int(i), scan_path))
            pn.plot_arr(nodule_64_array, picture_path + save_name + ".png")
            np.save(output_path + save_name + ".npy", nodule_64_array)
            #count += 1
            del scan_array, nodule_array, nodule_64_array
        else:
            non_case.append(scan_path)

    return non_case
    result_arr = np.load(os.path.join(result_path, result_file))
    #print(result_arr.shape)
    label_arr = np.zeros(
        (result_arr.shape[1], result_arr.shape[2], result_arr.shape[3]),
        dtype=np.float32)
    result_arr = result_arr[0, ...]
    for z in range(label_arr.shape[0]):
        for y in range(label_arr.shape[1]):
            for x in range(label_arr.shape[2]):
                if result_arr[z, y, x, 0] >= result_arr[z, y, x, 1]:
                    label_arr[z, y, x] = 0
                else:
                    label_arr[z, y, x] = 1

    return label_arr


if __name__ == '__main__':
    result_path = "E:/lidc_seg/lidc_padding_samples/test/label/"
    #label_path = "E:/lidc_seg/lidc_padding_samples/test/label/LIDC-IDRI-0004_label_0.npy"
    save_path = "E:/lidc_seg/lidc_padding_samples/test/label_picture/"
    result_list = os.listdir(result_path)
    for result_file in result_list:
        print(result_file)
        #result_arr = merge_result(result_file, result_path)
        result_arr = np.load(result_path + result_file)
        result_name = result_file.split(".")[0]
        save_name = save_path + result_name + ".png"
        print(save_name)
        plot_arr(result_arr, save_name)
Esempio n. 4
0
#-*-coding:utf-8-*-

import numpy as np
import os
from data_padding import padding_patch, normalization
from plot_nodule import plot_arr

lidc_path = "D:/DOI/"
origin_path = os.path.join(lidc_path, "four_radiologist_samples/")
padding_path = os.path.join(lidc_path, "lidc_padding_samples/")
origin_sample_path = os.path.join(origin_path, "sample/")
origin_label_path = os.path.join(origin_path, "label/")
padding_sample_path = os.path.join(padding_path, "sample/")
padding_label_path = os.path.join(padding_path, "label/")

nodule_file = os.path.join(origin_sample_path, "LIDC-IDRI-0004_sample_0.npy")
label_file = os.path.join(origin_label_path, "LIDC-IDRI-0004_label_0.npy")

nodule_arr = np.load(nodule_file)
label_arr = np.load(label_file)

nodule_64_arr, label_64_arr = padding_patch(nodule_arr, label_arr)
print(np.max(label_64_arr), np.min(label_64_arr))
print(np.max(label_arr), np.min(label_arr))

plot_arr(label_64_arr)
        nodule_array = image_array[edge_z_min:edge_z_max,
                                   edge_y_min:edge_y_max,
                                   edge_x_min:edge_x_max]
        nodule_64_array = np.ones((32, 64, 64), dtype=np.float32)
        nodule_64_array *= -1000.0
        padding_z_min = (nodule_64_array.shape[0] - nodule_array.shape[0]) // 2
        padding_z_max = padding_z_min + nodule_array.shape[0]
        padding_y_min = (nodule_64_array.shape[1] - nodule_array.shape[1]) // 2
        padding_y_max = padding_y_min + nodule_array.shape[1]
        padding_x_min = (nodule_64_array.shape[2] - nodule_array.shape[2]) // 2
        padding_x_max = padding_x_min + nodule_array.shape[2]

        nodule_64_array[padding_z_min:padding_z_max,
                        padding_y_min:padding_y_max,
                        padding_x_min:padding_x_max] = nodule_array
        save_name = os.path.join(cfg.p_v2_path, "deviant/",
                                 case + "_" + str(count).zfill(2))
        print(save_name)
        np.save(save_name + ".npy", nodule_64_array)
        plot_arr(nodule_64_array, save_name + ".png")
        count += 1


if __name__ == '__main__':
    seriesUID = "1.3.12.2.1107.5.1.4.65018.30000016050712025118100482153"
    cfg = config.Config()
    data_path = os.path.join(cfg.origin_path, "0800441455/")
    extract_path = os.path.join(cfg.extract_path, "0800441455/")
    case = "0800441455"
    sample_deviant(case, cfg)