def copy_meta_data_itk(source: sitk.Image, target: sitk.Image) -> sitk.Image: """ Copy meta data between files Args: source: source file target: target file Returns: sitk.Image: target file with copied meta data """ # for i in source.GetMetaDataKeys(): # target.SetMetaData(i, source.GetMetaData(i)) raise NotImplementedError("Does not work!") target.SetOrigin(source.GetOrigin()) target.SetDirection(source.GetDirection()) target.SetSpacing(source.GetSpacing()) return target
def match_world_info(source: sitk.Image, target: sitk.Image, spacing: Union[bool, Tuple[int], List[int]] = True, origin: Union[bool, Tuple[int], List[int]] = True, direction: Union[bool, Tuple[int], List[int]] = True): """Copy world information (eg spacing, origin, direction) from one image object to another. This matching is sometimes necessary for slight differences in metadata perhaps from founding that may prevent ITK filters from executing. Args: source (:obj:`sitk.Image`): Source object whose relevant metadata will be copied into ``target``. target (:obj:`sitk.Image`): Target object whose corresponding metadata will be overwritten by that of ``source``. spacing: True to copy the spacing from ``source`` to ``target``, or the spacing to set in ``target``; defaults to True. origin: True to copy the origin from ``source`` to ``target``, or the origin to set in ``target``; defaults to True. direction: True to copy the direction from ``source`` to ``target``, or the direction to set in ``target``; defaults to True. """ # get the world info from the source if not already set if spacing is True: spacing = source.GetSpacing() if origin is True: origin = source.GetOrigin() if direction is True: direction = source.GetDirection() # set the values in the target _logger.debug( "Adjusting spacing from %s to %s, origin from %s to %s, " "direction from %s to %s", target.GetSpacing(), spacing, target.GetOrigin(), origin, target.GetDirection(), direction) if spacing: target.SetSpacing(spacing) if origin: target.SetOrigin(origin) if direction: target.SetDirection(direction)
def sitk_copy_metadata(img_source: sitk.Image, img_target: sitk.Image) -> sitk.Image: """ Copy metadata (spacing, origin, direction) from source to target image Args img_source: source image img_target: target image Returns: SimpleITK.Image: target image with copied metadata """ raise RuntimeError("Deprecated") spacing = img_source.GetSpacing() img_target.SetSpacing(spacing) origin = img_source.GetOrigin() img_target.SetOrigin(origin) direction = img_source.GetDirection() img_target.SetDirection(direction) return img_target
def copy_geometry(image: sitk.Image, ref: sitk.Image): image.SetOrigin(ref.GetOrigin()) image.SetDirection(ref.GetDirection()) image.SetSpacing(ref.GetSpacing()) return image