def bulk_apply_mask(image_dir, mask_dir, output_dir, output_suffix, skip_existing_images=True): """Find corresponding images between dirs and apply the second as a mask Inputs: image_dir -- Directory of images to-be-masked mask_dir -- Directory of images that will be used as the mask output_dir -- Directory where the masked images will be saved ouptut_suffix -- Filename text after the core/sample name of the image file """ (image_path_list, mask_path_list) = blk.find_shared_images(image_dir, mask_dir) for i in range(np.size(image_path_list)): masked_path = blk.create_new_image_path(image_path_list[i], output_dir, output_suffix) if masked_path.exists() and skip_existing_images: continue image = meta.setup_image(image_path_list[i]) mask = meta.setup_image(mask_path_list[i]) > 0 print('Masking ' + os.path.basename(image_path_list[i]) + ' with ' + os.path.basename(mask_path_list[i])) masked_image = sitk.Mask(image, mask) meta.copy_relevant_metadata(masked_image, image) meta.write_image(masked_image, masked_path)
def resize_image(itk_image, current_spacing, target_spacing): """Resize an image by an integer factor towards target spacing""" if current_spacing < target_spacing: scale = math.floor(target_spacing / current_spacing) end_res = current_spacing * scale resized_image = sitk.Shrink(itk_image, [scale, scale]) resized_image.SetSpacing([end_res, end_res]) resized_image.SetOrigin(itk_image.GetOrigin()) elif current_spacing > target_spacing: scale = math.floor(current_spacing / target_spacing) end_res = current_spacing / scale resized_image = sitk.Expand(itk_image, [scale, scale]) resized_image.SetSpacing([end_res, end_res]) resized_image.SetOrigin(itk_image.GetOrigin()) else: resized_image = itk_image meta.copy_relevant_metadata(resized_image, itk_image) return resized_image
def convert_intensity_to_retardance(itk_image, ret_ceiling=35, wavelength=549, nm_input=True, deg_output=True): """Convert retardance intensities that are scaled to the image input (e.g., 16 bit int) into to actual retardance values. :param itk_image: The image being converted, as an ITK _image object :param ret_ceiling: The retardance value corresponding to max intensity :param wavelength: The wavelength of light used to image, for converting between degrees and retardance. :param nm_input: The input ret_ceiling is in nm if true, degrees if false :param deg_output: The output is in degrees if true, nm if false :return A new ITK image with retardance values either in degrees or in nm """ input_array = sitk.GetArrayFromImage(itk_image) # todo: implement a check for pixel type pixel_type_factor = ret_ceiling / 65535 if nm_input and deg_output: wavelength_factor = 360 / wavelength elif nm_input is False and deg_output is False: wavelength_factor = wavelength / 360 else: wavelength_factor = 1 output_array = input_array * pixel_type_factor * wavelength_factor output_image = sitk.GetImageFromArray(output_array) output_image = sitk.Cast(output_image, sitk.sitkFloat32) meta.copy_relevant_metadata(output_image, itk_image) return output_image
def apply_transform_fromfile(fixed_image: sitk.Image, moving_image: sitk.Image, transform_path): transform = sitk.ReadTransform(str(transform_path)) registered_image = sitk.Resample(moving_image, fixed_image, transform, sitk.sitkLinear, 0.0, moving_image.GetPixelID()) meta.copy_relevant_metadata(registered_image, moving_image) return registered_image
def supervised_register_images(fixed_image: sitk.Image, moving_image: sitk.Image, initial_transform: sitk.Transform = None, moving_path=None, registration_parameters: dict = None): """Register two images :param fixed_image: image that is being registered to :param moving_image: image that is being transformed and registered :param initial_transform: the type of registration/transform, e.g. affine or euler :param registration_parameters: dictionary of registration key/value arguments :return: Registered image, corresponding transform, metric, and stop """ # todo: Re-enable registering for RGB images while True: registration_method = define_registration_method( registration_parameters) fixed_final, moving_final, region_extracted = query_for_changes( fixed_image, moving_image, initial_transform, registration_method, moving_path) reg_plot = RegistrationPlot(fixed_final, moving_final, transform=initial_transform) (transform, metric, stop) = register(fixed_final, moving_final, reg_plot, registration_method=registration_method, initial_transform=initial_transform) if region_extracted: itkplt.plot_overlay(fixed_image, moving_image, transform, downsample=False) if query_good_registration(transform, metric, stop): break # todo: change registration method query here registered_image = sitk.Resample(moving_image, fixed_image, transform, sitk.sitkLinear, 0.0, moving_image.GetPixelID()) meta.copy_relevant_metadata(registered_image, moving_image) plt.close('all') return registered_image, transform, metric, stop
def extract_region(image: sitk.Image, size, origin, transform=None): """ Extract a region from a SimpleITK image :param image: The SimpleITK image :param size: The size of the region in physical space :param origin: The origin of the region in physical space :param transform: Image transform to apply before extracting the region :return: A SimpleITK image that corresponds to the region """ if transform is not None: translation = tran.get_translation(transform) origin = origin + translation size_array, index = get_region_size_origin_indices(size, origin, image.GetSpacing()) region = sitk.Extract(image, size_array, index) meta.copy_relevant_metadata(region, image) return region
def bulk_threshold(input_dir, output_dir, output_suffix, threshold=1, skip_existing_images=False): """Apply intensity based thresholds to all images in folder""" path_list = util.list_filetype_in_dir(input_dir, '.tif') for i in range(len(path_list)): new_path = blk.create_new_image_path(path_list[i], output_dir, output_suffix) if new_path.exists() and skip_existing_images: continue original = meta.setup_image(path_list[i]) new_image = apply_threshold(original, os.path.basename(path_list[i]), threshold=threshold) meta.copy_relevant_metadata(new_image, original) meta.write_image(new_image, new_path)
def bulk_convert_to_eightbit(input_dir, output_dir, output_suffix): """Convert all tif images in a directory to 8bit and save in new directory Inputs: input_dir -- Directory of images to convert output_dir -- Directory to save converted images output_suffix -- Text in output image name after the core/sample name """ path_list = util.list_filetype_in_dir(input_dir, '.tif') for i in range(len(path_list)): original = meta.setup_image(path_list[i]) new_image = convert_to_eightbit(original, os.path.basename(path_list[i])) new_path = blk.create_new_image_path(path_list[i], output_dir, output_suffix) meta.copy_relevant_metadata(new_image, original) meta.write_image(new_image, new_path)