def query_extract_region(fixed_image: sitk.Image, moving_image: sitk.Image, transform: sitk.Transform, registration_method: sitk.ImageRegistrationMethod): do_extract = util.query_yes_no( 'Do you wish to extract a sub-region to register based on? [y/n] >> ') if do_extract: itkplt.plot_overlay(fixed_image, moving_image, transform=transform) size = util.query_int('Enter the region size >> ') origin = [] for dim in range(len(fixed_image.GetSpacing())): origin_in_dim = util.query_float( 'Enter the origin in dimension {0} >> '.format(dim)) origin.append(origin_in_dim) origin = np.array(origin) fixed_region = extract_region(fixed_image, size, origin) moving_region = extract_region(moving_image, size * 1.1, origin - 0.05 * size, transform) query_registration_sampling_change(registration_method) return fixed_region, moving_region, do_extract else: return fixed_image, moving_image, do_extract
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 plot_overlay_from_czi_timepoints(path_file, timepoint_one, timepoint_two): """ Plot two timepoints from a czi image in a red/green overlay. Warning: requires a running javabridge :param path_file: Path to the czi file :param timepoint_one: First timepoint :param timepoint_two: Second timepoint :return: """ array_one = bf.load_image(str(path_file), t=timepoint_one) array_two = bf.load_image(str(path_file), t=timepoint_two) image_one = sitk.GetImageFromArray(array_one) image_two = sitk.GetImageFromArray(array_two) itkplot.plot_overlay(image_one, image_two, sitk.Transform(2, sitk.sitkIdentity), continuous_update=True, downsample=False)
def query_rotation_change(fixed_image: sitk.Image, moving_image: sitk.Image, initial_transform: sitk.Transform): """Ask if the user wants a new 2D ITK origin based on image overlay""" itkplt.plot_overlay(fixed_image, moving_image, initial_transform) change_rotation = util.query_yes_no( 'Do you want to change the rotation? [y/n] >> ') if change_rotation: while True: rotation = util.query_float('Enter new rotation (degrees): ') tran.set_transform_rotation(initial_transform, rotation) itkplt.plot_overlay(fixed_image, moving_image, initial_transform, rotation) # bug: The image does not show up till after the question if util.query_yes_no('Is this rotation good? [y/n] >> '): break
def query_translation_change(fixed_image: sitk.Image, moving_image: sitk.Image, transform: sitk.Transform): """Ask if the user wants a new 2D ITK translation based on image overlay""" change_origin = util.query_yes_no( 'Do you want to change the initial translation? [y/n] >> ') translation = tran.get_translation(transform) if change_origin: while True: print('Current physical shift: ' + str(-1 * np.array(translation))) new_translation = [] for dim in range(len(translation)): new_dim_translation = -1 * util.query_float( 'Enter the new shift in dimension {0} >> '.format( str(dim))) new_translation.append(new_dim_translation) tran.set_translation(transform, new_translation) itkplt.plot_overlay(fixed_image, moving_image, transform) # bug: The image does not show up till after the question if util.query_yes_no('Is this translation good? [y/n] >>> '): break