コード例 #1
0
                                         == 0] = (0, 0, 0)
                    #                     ori_imgs[index,...]*=np.tile(np.expand_dims(pred_vessels[index,...], axis=3), (1,1,3))
                    Image.fromarray(ori_imgs[index, ...].astype(
                        np.uint8)).save(
                            os.path.join(vessels_dir,
                                         os.path.basename(filenames[index])))

                # compare with the ground truth
                comp_dir = comparison_out.format(os.path.basename(dataset),
                                                 os.path.basename(result))
                if not os.path.isdir(comp_dir):
                    os.makedirs(comp_dir)
                dice_list = []
                for index in range(gt_vessels.shape[0]):
                    diff_map, dice_coeff = utils.difference_map(
                        gt_vessels[index, ...], pred_vessels[index, ...],
                        masks[index, ...])
                    dice_list.append(dice_coeff)
                    Image.fromarray(diff_map.astype(np.uint8)).save(
                        os.path.join(comp_dir,
                                     os.path.basename(filenames[index])))
#                 print "indices of best dice coeff : {}".format(sorted(range(len(dice_list)),key=lambda k: dice_list[k]))

# skip the ground truth
            if "1st_manual" not in result:
                # print metrics
                print "-- {} --".format(os.path.basename(result))
                print "dice coefficient : {}".format(
                    utils.dice_coefficient(gt_vessels, pred_vessels, masks))
                print "f1 score : {}, accuracy : {}, sensitivity : {}, specificity : {}".format(
                    *utils.misc_measures_evaluation(gt_vessels, pred_vessels,
コード例 #2
0
def save_imgs(slice_list,
              save_folder,
              methods,
              img_size,
              crop_size=(100, 80),
              factor=1.5):
    # initialize CT and MR folders
    ct_save_folder = os.path.join(save_folder, 'raw_ct')
    if not os.path.exists(ct_save_folder):
        os.makedirs(ct_save_folder)

    mr_save_folder = os.path.join(save_folder, 'raw_mr')
    if not os.path.exists(mr_save_folder):
        os.makedirs(mr_save_folder)

    # Save image
    for idx in range(args.number):
        canvas, info_height, margin = None, None, None

        if args.add_value:
            font_type = cv2.FONT_HERSHEY_TRIPLEX
            font_scale = 1.
            font_thickness = 2
            font_color = (0, 0, 0)

            # Calculate height and width of the text information
            info_str = '{} {:.3f}'.format(args.measure.upper(),
                                          slice_list[idx].measurement)
            info_size = cv2.getTextSize(info_str, font_type, font_scale,
                                        font_thickness)
            info_height, info_width = info_size[0][1], info_size[0][0]
            margin = int(0.2 * info_height)
            bottom_left = (
                int((len(methods) + 2) * img_size[1] * 0.5 - info_width * 0.5),
                int(img_size[0] + int(factor * crop_size[0]) + info_height +
                    0.5 * margin))  # row and col

            canvas = 255 * np.ones(
                (img_size[0] + int(factor * crop_size[0]) + info_height +
                 margin, (len(methods) + 2) * img_size[1], 3),
                dtype=np.uint8)

            # Add text information
            cv2.putText(canvas,
                        info_str,
                        bottom_left,
                        font_type,
                        font_scale,
                        font_color,
                        thickness=font_thickness)
        else:
            canvas = 255 * np.ones((img_size[0] + int(factor * crop_size[0]),
                                    (len(methods) + 2) * img_size[1], 3),
                                   dtype=np.uint8)

        img_name = slice_list[idx].name
        ct_img = cv2.imread(os.path.join('../ct',
                                         img_name))  # read 3 channel data
        mri_img = cv2.imread(os.path.join('../gt',
                                          img_name))  # read 3 channel data

        canvas[:img_size[0], :img_size[1], :] = ct_img  # save ct img
        # save mri img
        mri_img_rec, mri_crop = utils.central_crop(mri_img.copy(),
                                                   factor=factor)
        canvas[:img_size[0],
               (len(methods) + 1) * img_size[1]:, :] = mri_img_rec
        canvas[img_size[0]:img_size[0] + int(factor * crop_size[0]),
               -int(factor * crop_size[1]):, :] = mri_crop

        for idx_method, method_name in enumerate(methods):
            # Read 3 channel data
            img = cv2.imread(
                os.path.join('../{}'.format(method_name), img_name))
            # calculate difference map
            utils.difference_map(img, mri_img, save_folder, img_name,
                                 method_name)

            img, crop_img = utils.central_crop(img, factor=factor)
            if args.add_value:
                canvas[:-int(factor * crop_size[0]) - info_height - margin,
                       (idx_method + 1) * img_size[1]:(idx_method + 2) *
                       img_size[1], :] = img
                canvas[img_size[0]:-info_height - margin,
                       (idx_method + 2) * img_size[1] -
                       int(factor * crop_size[1]):(idx_method + 2) *
                       img_size[1], :] = crop_img
            else:
                canvas[:img_size[0], (idx_method + 1) *
                       img_size[1]:(idx_method + 2) * img_size[1], :] = img
                canvas[img_size[0]:, (idx_method + 2) * img_size[1] -
                       int(factor * crop_size[1]):(idx_method + 2) *
                       img_size[1], :] = crop_img

        cv2.imwrite(os.path.join(save_folder, img_name), canvas)
        cv2.imwrite(os.path.join(ct_save_folder, img_name), ct_img)
        cv2.imwrite(os.path.join(mr_save_folder, img_name), mri_img)