def load_and_extract_center(datum, data_prefix): """ Load image and computer optimal center """ full_image_path = os.path.join(data_prefix, datum["short_file_path"]) image = reading_images.read_image_png(full_image_path) return datum["short_file_path"], extract_center(datum, image)
def get_optimal_center_single(cropped_mammogram_path, metadata_path): """ Get optimal center for single example """ metadata = pickling.unpickle_from_file(metadata_path) image = reading_images.read_image_png(cropped_mammogram_path) optimal_center = get_optimal_centers.extract_center(metadata, image) metadata["best_center"] = optimal_center pickling.pickle_to_file(metadata_path, metadata)
def load_image(image_path, view, horizontal_flip): """ Loads a png or hdf5 image as floats and flips according to its view. """ if image_path.endswith("png"): image = read_image_png(image_path) elif image_path.endswith("hdf5"): image = read_image_mat(image_path) else: raise RuntimeError() image = image.astype(np.float32) image = flip_image(image, view, horizontal_flip) return image
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