Exemple #1
0
def create_pickle_for_patient(in_dir, out_dir):
    scan_id = os.path.basename(in_dir)
    image_folder = os.path.join(in_dir, "sa", "images")
    con_file = os.path.join(in_dir, "sa", "contours.con")
    meta_txt = os.path.join(in_dir, "meta.txt")

    if not os.path.isdir(image_folder):
        logger.error("Could not find image folder for: {}".format(scan_id))
        return
    if not os.path.isfile(con_file):
        logger.error("Could not find .con file for: {}".format(scan_id))
        return
    if not os.path.isfile(meta_txt):
        logger.error("Could not find meta.txt file for: {}".format(scan_id))
        return

    dr = DCMreaderVM(image_folder)
    if dr.num_frames == 0 and dr.num_frames == 0 or dr.broken:
        logger.error("Could not create pickle file for {}".format(scan_id))
        return

    cr = CONreaderVM(con_file)
    contours = left_ventricle_contours(cr.get_hierarchical_contours())

    frame_slice_dict = collect_contour_slices_by_frames(contours)
    if not (len(frame_slice_dict) == 2):
        logger.error("Too many contour frames for {}".format(scan_id))
        return

    pickle_file_path = os.path.join(out_dir, scan_id + ".p")
    create_path_for_file(pickle_file_path)

    diastole_frame = frame_of_diastole(frame_slice_dict, contours)
    sampling_slices = calculate_sampling_slices(frame_slice_dict,
                                                diastole_frame)
    sampling_contours = []
    for slice_index in sampling_slices:
        shape = dr.get_image(slice_index, diastole_frame).shape
        sampling_contours.append(contours[slice_index][diastole_frame])
    pathology = read_pathology(meta_txt)
    shape = dr.get_image(sampling_slices[0], diastole_frame).shape
    contour_diff_matricies = create_contour_diff_matricies(
        sampling_contours, shape)
    print(type(contour_diff_matricies))
    patient_data = PatientData(scan_id, pathology, cr.get_volume_data(),
                               contour_diff_matricies)

    with (open(pickle_file_path, "wb")) as pickleFile:
        pickle.dump(patient_data, pickleFile)
Exemple #2
0
def contours2images(patient_folder):
    image_folder = pjoin(patient_folder, 'sa', 'images')
    con_file = pjoin(patient_folder, 'sa', 'contours.con')
    # reading the dicom files
    dr = DCMreaderVM(image_folder)

    # reading the contours
    cr = CONreaderVM(con_file)
    contours = cr.get_hierarchical_contours()

    # drawing the contours for the images
    for slc in contours:
        for frm in contours[slc]:
            image = dr.get_image(slc, frm)  # numpy array
            cntrs = []
            rgbs = []
            for mode in contours[slc][frm]:
                # choose color
                if mode == 'ln':  # left endocardium -> red
                    rgb = [1, 0, 0]
                elif mode == 'lp':  # left epicardium -> green
                    rgb = [0, 1, 0]
                elif mode == 'rn':  # right endocardium -> yellow
                    rgb = [1, 1, 0]
                else:
                    rgb = None
                if rgb is not None:
                    cntrs.append(contours[slc][frm][mode])
                    rgbs.append(rgb)
            if len(cntrs) > 0:
                draw(image, cntrs, rgbs)
Exemple #3
0

image_folder = 'C:\\dev\\LHYP\\sample\\13457546AMR801\\sa\\images'
con_file = 'C:\\dev\\LHYP\\sample\\13457546AMR801\\sa\\contours.con'

# reading the dicom files
dr = DCMreaderVM(image_folder)

# reading the contours
cr = CONreaderVM(con_file)
contours = cr.get_hierarchical_contours()

# drawing the contours for the images
for slc in contours:
    for frm in contours[slc]:
        image = dr.get_image(slc, frm)  # numpy array
        cntrs = []
        rgbs = []
        for mode in contours[slc][frm]:
            # choose color
            if mode == 'ln':
                rgb = [1, 0, 0]
            elif mode == 'lp':
                rgb = [0, 1, 0]
            elif mode == 'rn':
                rgb = [1, 1, 0]
            else:
                rgb = None
            if rgb is not None:
                cntrs.append(contours[slc][frm][mode])
                rgbs.append(rgb)