def save_heatmaps(heatmap_malignant, heatmap_benign, short_file_path, view, horizontal_flip, parameters): """ Saves the heatmaps after flipping back to the original direction """ image_extension = '.hdf5' if parameters['use_hdf5'] else '.png' heatmap_malignant = loading.flip_image(heatmap_malignant, view, horizontal_flip) heatmap_benign = loading.flip_image(heatmap_benign, view, horizontal_flip) heatmap_save_path_malignant = os.path.join( parameters['save_heatmap_path'][0], short_file_path + image_extension) if heatmap_save_path_malignant.endswith("png"): saving_images.save_image_as_png(heatmap_malignant, heatmap_save_path_malignant) elif heatmap_save_path_malignant.endswith("hdf5"): saving_images.save_image_as_hdf5(heatmap_malignant, heatmap_save_path_malignant) else: raise RuntimeError() heatmap_save_path_benign = os.path.join(parameters['save_heatmap_path'][1], short_file_path + image_extension) if heatmap_save_path_benign.endswith("png"): saving_images.save_image_as_png(heatmap_benign, heatmap_save_path_benign) elif heatmap_save_path_benign.endswith("hdf5"): saving_images.save_image_as_hdf5(heatmap_benign, heatmap_save_path_benign) else: raise RuntimeError()
def crop_mammogram_one_image(scan, input_file_path, output_file_path, num_iterations, buffer_size): """ Crops a mammogram, saves as png file, includes the following additional information: - window_location: location of cropping window w.r.t. original dicom image so that segmentation map can be cropped in the same way for training. - rightmost_points: rightmost nonzero pixels after correctly being flipped - bottommost_points: bottommost nonzero pixels after correctly being flipped - distance_from_starting_side: number of zero columns between the start of the image and start of the largest connected component w.r.t. original dicom image. """ image = reading_images.read_image_png(input_file_path) try: # error detection using erosion. Also get cropping information for this image. cropping_info = crop_img_from_largest_connected( image, image_orientation(scan['horizontal_flip'], scan['side']), True, num_iterations, buffer_size, 1 / 3) except Exception as error: print( input_file_path, "\n\tFailed to crop image because image is invalid.", str(error)) else: top, bottom, left, right = cropping_info[0] target_parent_dir = os.path.split(output_file_path)[0] if not os.path.exists(target_parent_dir): os.makedirs(target_parent_dir) try: saving_images.save_image_as_png( image[top:bottom, left:right], output_file_path) except Exception as error: print(input_file_path, "\n\tError while saving image.", str(error)) return cropping_info