def output_preprocessed_image_patch_to_output_directory( full_image_path, output_directory_path, first_centermost_crop_size, downscale_to_size, second_centermost_crop_size): input_image = Image.open(full_image_path) first_centermost_crop_image = image_preprocessing_utils.crop_to_the_centermost( input_image, first_centermost_crop_size) scaled_image = image_preprocessing_utils.scale_image( first_centermost_crop_image, downscale_to_size) second_centermost_crop_image = image_preprocessing_utils.crop_to_the_centermost( scaled_image, second_centermost_crop_size) is_mostly_white_pixels = is_image_mostly_white_pixels( 0.95, second_centermost_crop_image) case_id = full_image_path.split('/')[-2] image_patch_id = full_image_path.split('/')[-1][:-4] output_case_subfolder = join(output_directory_path, case_id) path_utils.create_directory_if_directory_does_not_exist_at_path( output_case_subfolder) if is_mostly_white_pixels: path_utils.create_directory_if_directory_does_not_exist_at_path( output_case_subfolder + "/white_background_image_patches") output_image_name = join( output_case_subfolder + "/white_background_image_patches/", image_patch_id + '.jpg') second_centermost_crop_image.save(output_image_name) else: output_image_name = join(output_case_subfolder, image_patch_id + '.jpg') second_centermost_crop_image.save(output_image_name)
def output_preprocessed_image_patch_to_output_directory( full_image_path, output_directory_path, downscale_to_size): input_image = Image.open(full_image_path).convert('LA') scaled_image = image_preprocessing_utils.scale_image( input_image, downscale_to_size) case_id = full_image_path.split('/')[-2] image_patch_id = full_image_path.split('/')[-1][:-4] output_case_subfolder = join(output_directory_path, case_id) path_utils.create_directory_if_directory_does_not_exist_at_path( output_case_subfolder) output_image_name = join(output_case_subfolder, image_patch_id + '.png') scaled_image.save(output_image_name)
def create_saliency_prediction_overview_visualization_for_case( output_folder_path_for_saliancy_prediction_visualization_directory, svs_image, case_ID, image_patch_metadata_objects_corresponding_to_CID): output_path = output_folder_path_for_saliancy_prediction_visualization_directory + '/' + case_ID + '/' path_utils.create_directory_if_directory_does_not_exist_at_path( output_path) thumbnail = create_jpeg_thumbnail_of_wsi(image_paths_of_sample[0]) thumbnail = thumbnail.convert("RGB") thumbnail_path = output_path + case_ID + "_original.jpeg" thumbnail.save(thumbnail_path, 'JPEG') thumbnail = thumbnail.convert("RGBA") thumbnail_with_predictions_above_threshold = draw_saliency_prediction_annotation_boxes_onto_thumbnail( svs_image, thumbnail, image_patch_metadata_objects_corresponding_to_CID, accuracy_percentage_threshold, draw_border=False) thumbnail_with_predictions_above_threshold_path = output_path + case_ID + "_multiple_annotations.jpeg" thumbnail_with_predictions_above_threshold.save( thumbnail_with_predictions_above_threshold_path, 'JPEG') thumbnail = thumbnail.convert("RGBA") image_patch_metadata_object_with_highest_saliency = image_patch_metadata_object_utils.get_image_patch_metadata_object_with_the_highest_saliency( image_patch_metadata_objects_corresponding_to_CID) thumbnail_with_single_predication = draw_saliency_prediction_annotation_boxes_onto_thumbnail( svs_image, thumbnail, [image_patch_metadata_object_with_highest_saliency], accuracy_percentage_threshold, draw_border=True) thumbnail_with_single_predication_path = output_path + case_ID + "_single_annotations.jpeg" thumbnail_with_single_predication.save( thumbnail_with_single_predication_path, 'JPEG') merged_thumbnail_image = image_utils.merge_images_horizontally([ thumbnail_path, thumbnail_with_predictions_above_threshold_path, thumbnail_with_single_predication_path ]) merged_thumbnail_image_path = output_path + case_ID + "_all_thumbnail_images.jpeg" merged_thumbnail_image.save(merged_thumbnail_image_path, 'JPEG')
def create_most_salient_image_patch_high_res_visualization( output_folder_path_for_most_salient_image_patch_visualization_directory, svs_image_path, image_patch_metadata_objects_corresponding_to_CID): output_path = output_folder_path_for_most_salient_image_patch_visualization_directory path_utils.create_directory_if_directory_does_not_exist_at_path( output_path) image_patch_metadata_object_with_highest_saliency = image_patch_metadata_object_utils.get_image_patch_metadata_object_with_the_highest_saliency( image_patch_metadata_objects_corresponding_to_CID) svs_image_patch_extractor.extract_image_patch_jpeg( svs_image_path, output_path, from_resolution_level=image_patch_metadata_object_with_highest_saliency .resolution_level, to_resolution_level=enums.ResolutionLevel.LEVEL_2, patch_area_x=image_patch_metadata_object_with_highest_saliency. x_coordinate, patch_area_y=image_patch_metadata_object_with_highest_saliency. y_coordinate, patch_area_width=image_patch_metadata_object_with_highest_saliency. width, patch_area_height=image_patch_metadata_object_with_highest_saliency. height)
def annotate_the_region_on_image_patch_with_the_highest_nuclei_count( highest_saliency_image_patch_metadata_object, high_nuclei_count_metadata_object, svs_loader, output_path): case_ID = highest_saliency_image_patch_metadata_object.case_id output_path = output_path + '/' + case_ID + '/' path_utils.create_directory_if_directory_does_not_exist_at_path( output_path) image_patch_with_highest_saliency = Image.open( highest_saliency_image_patch_metadata_object.image_patch_path) image_patch_with_highest_saliency = image_patch_with_highest_saliency.convert( "RGBA") svs_path = svs_loader.get_0_indexed_svs_path_for_CID(case_ID) svs_image = svs_utils.get_svs_image_of_wsi_from_path(svs_path) scaled_high_nuclei_count_metadata_image_patch_data = svs_utils.scale_image_patch_metadata_object_to_new_resolution_level( high_nuclei_count_metadata_object, highest_saliency_image_patch_metadata_object.resolution_level, svs_image) scaled_high_nuclei_count_metadata_image_patch_data.x_coordinate = abs( int(scaled_high_nuclei_count_metadata_image_patch_data.x_coordinate) - int(highest_saliency_image_patch_metadata_object.x_coordinate)) scaled_high_nuclei_count_metadata_image_patch_data.y_coordinate = abs( int(scaled_high_nuclei_count_metadata_image_patch_data.y_coordinate) - int(highest_saliency_image_patch_metadata_object.y_coordinate)) image_patch_with_annotation_box_of_highest_nuclei_count = image_utils.draw_annotation_box_onto_image( image_patch_with_highest_saliency, scaled_high_nuclei_count_metadata_image_patch_data, draw_border=True) thumbnail_with_single_predication_path = output_path + case_ID + "_highest_nuclei_count_annotation.jpeg" image_patch_with_annotation_box_of_highest_nuclei_count.save( thumbnail_with_single_predication_path, 'JPEG')
args = parser.parse_args() input_folder_path = args.input_folder_path input_folder_path_for_high_res_image_patches = args.input_folder_path + "/image_patches_with_highest_nuclei_count/images_original" svs_input_folder_path = args.svs_input_folder_path output_folder_path = args.output_folder_path visualizations_output_path = output_folder_path + "/visualizations" copy_tree(input_folder_path + "/visualizations", visualizations_output_path) highest_nuclei_count_visualization_output_path = visualizations_output_path + "/highest_nuclei_count_annotations_on_most_salient_high_res_image_patch" svs_loader = svs_l.SVSLoader(svs_input_folder_path) path_utils.halt_script_if_path_does_not_exist(input_folder_path) path_utils.create_directory_if_directory_does_not_exist_at_path( output_folder_path) most_salient_image_patch_case_directory_paths = path_utils.create_full_paths_to_directories_in_directory_path( input_folder_path + "/visualizations/most_salient_image_patch_high_res") CID_indexed_high_saliency_image_patch_metadata_objects_dict = image_patch_metadata_object_utils.case_directory_paths_containing_image_patches_to_dict_indexed_by_CID( most_salient_image_patch_case_directory_paths) high_nuclei_count_case_directory_paths = path_utils.create_full_paths_to_directories_in_directory_path( input_folder_path_for_high_res_image_patches) for high_nuclei_count_case_directory_path in high_nuclei_count_case_directory_paths: high_nuclei_count_case_image_patches_paths = path_utils.create_full_paths_to_files_in_directory_path( high_nuclei_count_case_directory_path) first_image_patch_path = high_nuclei_count_case_image_patches_paths[0] image_name = first_image_patch_path.split('/')[-1] image_patch_metadata_object_with_high_nucleus = image_patch_file_name_parser.parse_image_patch_file_name_into_image_patch_metadata_object(
required=True) args = parser.parse_args() input_folder_path = args.input_folder_path image_patches_with_nuclei_counts_path = args.input_folder_path + '/' + "image_patches_with_nuclei_counts" + "/" preprocessed_high_res_image_patches_path = args.input_folder_path + '/' + "preprocessed_high_res_image_patches" + "/" output_folder_path = args.output_folder_path image_mode = args.image_mode path_utils.halt_script_if_path_does_not_exist( image_patches_with_nuclei_counts_path) path_utils.create_directory_if_directory_does_not_exist_at_path( output_folder_path) output_folder_path_for_images_with_highest_nuclei_count = output_folder_path + '/' + "image_patches_with_highest_nuclei_count" path_utils.create_directory_if_directory_does_not_exist_at_path( output_folder_path_for_images_with_highest_nuclei_count) case_directory_paths = path_utils.create_full_paths_to_directories_in_directory_path( image_patches_with_nuclei_counts_path) for case_directory_path in case_directory_paths: path_to_image_patches = case_directory_path + "/" + "images_original" case_id = case_directory_path.split('/')[-1] case_id_output_path = output_folder_path_for_images_with_highest_nuclei_count + "/images_original/" + case_id path_utils.create_directory_if_directory_does_not_exist_at_path( case_id_output_path) all_image_patches_for_case_id_directory_path = preprocessed_high_res_image_patches_path + "/" + case_id output_image_patch_with_highest_predicted_nuclei_count(
def split_to_jpeg_image_patches( full_image_path, full_output_path, to_resolution_level, overlapping_percentage, window_size, from_resolution_level=enums.ResolutionLevel.LEVEL_0_BASE, patching_area_x=None, patching_area_y=None, patching_area_width=None, patching_area_height=None): img = openslide.OpenSlide(full_image_path) has_specific_patching_area = patching_area_x is not None and patching_area_y is not None and patching_area_width is not None and patching_area_height is not None if has_specific_patching_area: original_start_position_x = int(patching_area_x) original_start_position_y = int(patching_area_y) width, height = int(patching_area_width), int(patching_area_height) else: original_start_position_x = 0 original_start_position_y = 0 width, height = img.level_dimensions[from_resolution_level] original_start_position_x_label = int( svs_utils.scale(int(original_start_position_x), from_resolution_level, to_resolution_level, img)) original_start_position_y_label = int( svs_utils.scale(int(original_start_position_y), from_resolution_level, to_resolution_level, img)) width = int( svs_utils.scale(int(width), from_resolution_level, to_resolution_level, img)) height = int( svs_utils.scale(int(height), from_resolution_level, to_resolution_level, img)) x_start_positions_labels = get_start_positions( original_start_position_x_label, width, height, window_size, enums.Axis.X, overlapping_percentage) y_start_positions_label = get_start_positions( original_start_position_y_label, width, height, window_size, enums.Axis.Y, overlapping_percentage) total_number_of_patches = len(x_start_positions_labels) * len( y_start_positions_label) tile_number = 1 for x_index, x_start_position in enumerate(x_start_positions_labels): for y_index, y_start_position in enumerate(y_start_positions_label): x_end_position = min(original_start_position_x_label + width, x_start_position + window_size) y_end_position = min(original_start_position_y_label + height, y_start_position + window_size) patch_width = x_end_position - x_start_position patch_height = y_end_position - y_start_position is_image_patch_size_equal_to_window_size = ( (patch_height == window_size) and (patch_width == window_size)) if not is_image_patch_size_equal_to_window_size: continue #must always convert to resolution 0 when doing patch extraction reader_x_start_position = x_start_position reader_y_start_position = y_start_position if to_resolution_level != enums.ResolutionLevel.LEVEL_0_BASE: reader_x_start_position = int( svs_utils.scale(x_start_position, to_resolution_level, from_resolution_level, img)) reader_y_start_position = int( svs_utils.scale(y_start_position, to_resolution_level, from_resolution_level, img)) patch = img.read_region( (reader_x_start_position, reader_y_start_position), to_resolution_level, (patch_width, patch_height)) patch.load() patch_rgb = Image.new("RGB", patch.size, (255, 255, 255)) patch_rgb.paste(patch, mask=patch.split()[3]) print("\n") print("Patch data", x_start_position, y_start_position, to_resolution_level, patch_width, patch_height) print("Tile size for tile number " + str(tile_number) + ":" + str(patch.size)) # compress the image # patch_rgb = patch_rgb.resize( # (int(patch_rgb.size[0] / compression_factor), int(patch_rgb.size[1] / compression_factor)), # Image.ANTIALIAS) # save the image case_id = full_image_path.split('/')[-1][:-4] output_subfolder = join(full_output_path, case_id) path_utils.create_directory_if_directory_does_not_exist_at_path( output_subfolder) output_image_name = join( output_subfolder, image_patch_file_name_builder.build_image_patch_file_name( case_id, to_resolution_level, x_start_position, y_start_position, patch_width, patch_height)) patch_rgb.save(output_image_name) print("Tile", tile_number, "/", total_number_of_patches, "created") tile_number = tile_number + 1
def extract_image_patch_jpeg( full_image_path, full_output_path, from_resolution_level=enums.ResolutionLevel.LEVEL_0_BASE, to_resolution_level=None, patch_area_x=None, patch_area_y=None, patch_area_width=None, patch_area_height=None): img = openslide.OpenSlide(full_image_path) original_start_position_x = int(patch_area_x) original_start_position_y = int(patch_area_y) width, height = int(patch_area_width), int(patch_area_height) original_start_position_x_label = int( svs_utils.scale(int(original_start_position_x), from_resolution_level, to_resolution_level, img)) original_start_position_y_label = int( svs_utils.scale(int(original_start_position_y), from_resolution_level, to_resolution_level, img)) width = int( svs_utils.scale(int(width), from_resolution_level, to_resolution_level, img)) height = int( svs_utils.scale(int(height), from_resolution_level, to_resolution_level, img)) x_end_position = original_start_position_x_label + width y_end_position = original_start_position_y_label + height patch_width = width patch_height = height # must always convert to resolution 0 when doing patch extraction reader_x_start_position = original_start_position_x_label reader_y_start_position = original_start_position_y_label if to_resolution_level != enums.ResolutionLevel.LEVEL_0_BASE: reader_x_start_position = int( svs_utils.scale(original_start_position_x_label, to_resolution_level, enums.ResolutionLevel.LEVEL_0_BASE, img)) reader_y_start_position = int( svs_utils.scale(original_start_position_y_label, to_resolution_level, enums.ResolutionLevel.LEVEL_0_BASE, img)) patch = img.read_region((reader_x_start_position, reader_y_start_position), to_resolution_level, (patch_width, patch_height)) patch.load() patch_rgb = Image.new("RGB", patch.size, (255, 255, 255)) patch_rgb.paste(patch, mask=patch.split()[3]) print("\n") print("Image patch data", original_start_position_x_label, original_start_position_y_label, to_resolution_level, patch_width, patch_height) print("\n") print("\n") print("\n") # compress the image # patch_rgb = patch_rgb.resize( # (int(patch_rgb.size[0] / compression_factor), int(patch_rgb.size[1] / compression_factor)), # Image.ANTIALIAS) # save the image case_id = full_image_path.split('/')[-1][:-4] output_subfolder = join(full_output_path, case_id) path_utils.create_directory_if_directory_does_not_exist_at_path( output_subfolder) output_image_name = join( output_subfolder, image_patch_file_name_builder.build_image_patch_file_name( case_id, to_resolution_level, original_start_position_x_label, original_start_position_y_label, patch_width, patch_height)) patch_rgb.save(output_image_name)
help="The path to the input folder.", required=True) parser.add_argument("-o", "--output_folder_path", type=str, help="The path to the output folder." " If output folder doesn't exists at runtime " "the script will create it.", required=True) args = parser.parse_args() input_folder_path = args.input_folder_path output_folder_path = args.output_folder_path path_utils.halt_script_if_path_does_not_exist(input_folder_path) path_utils.create_directory_if_directory_does_not_exist_at_path( output_folder_path) output_folder_path_for_prediction_csv_files = output_folder_path + '/' + "saliency_predictions_csvs" path_utils.create_directory_if_directory_does_not_exist_at_path( output_folder_path_for_prediction_csv_files) case_paths = path_utils.create_full_paths_to_directories_in_directory_path( input_folder_path) for case_path in case_paths: output_case_prediction_csv_path = output_folder_path_for_prediction_csv_files + '/' + case_path.split( '/')[-1] + '.csv' is_case_already_classified = auto_resume_utils.is_case_already_classified_into_output_csv( case_path, output_case_prediction_csv_path) if is_case_already_classified: continue
"--accuracy_percentage_threshold", type=float, help="Accuracy percentage threshold for showing annotations", required=True) args = parser.parse_args() svs_input_folder_path = args.svs_input_folder_path csv_input_folder_path = args.csv_input_folder_path + "/saliency_predictions_csvs" output_folder_path = args.output_folder_path accuracy_percentage_threshold = args.accuracy_percentage_threshold path_utils.halt_script_if_path_does_not_exist(svs_input_folder_path) path_utils.halt_script_if_path_does_not_exist(csv_input_folder_path) path_utils.create_directory_if_directory_does_not_exist_at_path( output_folder_path) output_folder_path_for_visualization_directory = output_folder_path + '/' + "visualizations" path_utils.create_directory_if_directory_does_not_exist_at_path( output_folder_path_for_visualization_directory) output_folder_path_for_saliancy_prediction_visualization_directory = output_folder_path_for_visualization_directory + '/' + "saliency_prediction_overview_visualization" path_utils.create_directory_if_directory_does_not_exist_at_path( output_folder_path_for_saliancy_prediction_visualization_directory) output_folder_path_for_most_salient_image_patch_high_res_visualization_directory = output_folder_path_for_visualization_directory + '/' + "most_salient_image_patch_high_res" path_utils.create_directory_if_directory_does_not_exist_at_path( output_folder_path_for_saliancy_prediction_visualization_directory) tcga_download_directory_paths = path_utils.create_full_paths_to_directories_in_directory_path( svs_input_folder_path)