def to_itk_image(image_like): if isinstance(image_like, itk.Image): return image_like if is_arraylike(image_like): array = np.asarray(image_like) can_use_view = array.flags['OWNDATA'] if have_dask and isinstance(image_like, dask.array.core.Array): can_use_view = False array = np.ascontiguousarray(array) # JavaScript does not support 64-bit integers if array.dtype == np.int64: array = array.astype(np.float32) elif array.dtype == np.uint64: array = array.astype(np.float32) if can_use_view: image_from_array = itk.image_view_from_array(array) else: image_from_array = itk.image_from_array(array) return image_from_array elif have_vtk and isinstance(image_like, vtk.vtkImageData): from vtk.util import numpy_support as vtk_numpy_support array = vtk_numpy_support.vtk_to_numpy( image_like.GetPointData().GetScalars()) array.shape = tuple(image_like.GetDimensions())[::-1] image_from_array = itk.image_view_from_array(array) image_from_array.SetSpacing(image_like.GetSpacing()) image_from_array.SetOrigin(image_like.GetOrigin()) return image_from_array elif have_simpleitk and isinstance(image_like, sitk.Image): array = sitk.GetArrayViewFromImage(image_like) image_from_array = itk.image_view_from_array(array) image_from_array.SetSpacing(image_like.GetSpacing()) image_from_array.SetOrigin(image_like.GetOrigin()) direction = image_like.GetDirection() npdirection = np.asarray(direction) npdirection = np.reshape(npdirection, (-1, 3)) itkdirection = itk.matrix_from_array(npdirection) image_from_array.SetDirection(itkdirection) return image_from_array elif have_imagej: import imglyb if isinstance(image_like, imglyb.util.ReferenceGuardingRandomAccessibleInterval): array = imglyb.to_numpy(image_like) image_from_array = itk.image_view_from_array(array) return image_from_array elif isinstance(image_like, itk.ProcessObject): return itk.output(image_like) return None
def validate(self, obj, value): if is_arraylike(value): array = np.asarray(value) self._source_object = array if array.flags['OWNDATA']: image_from_array = itk.GetImageViewFromArray(array) else: image_from_array = itk.GetImageFromArray(array) return image_from_array elif have_vtk and isinstance(value, vtk.vtkImageData): from vtk.util import numpy_support as vtk_numpy_support array = vtk_numpy_support.vtk_to_numpy( value.GetPointData().GetScalars()) array.shape = tuple(value.GetDimensions())[::-1] self._source_object = array image_from_array = itk.GetImageViewFromArray(array) image_from_array.SetSpacing(value.GetSpacing()) image_from_array.SetOrigin(value.GetOrigin()) return image_from_array elif have_imglyb and isinstance( value, imglyb.util.ReferenceGuardingRandomAccessibleInterval): array = imglyb.to_numpy(value) self._source_object = array image_from_array = itk.GetImageViewFromArray(array) return image_from_array try: # an itk.Image or a filter that produces an Image # return itk.output(value) # Working around traitlets / ipywidgets update mechanism to # force an update. While the result of __eq__ can indicate it is # the same object, the actual contents may have changed, as # indicated by image.GetMTime() value = itk.output(value) self._source_object = value grafted = value.__New_orig__() grafted.Graft(value) return grafted except: self.error(obj, value)
def view(image): have_imglyb = False try: import imglyb have_imglyb = True except ImportError: pass have_vtk = False try: import vtk have_vtk = True except ImportError: pass viewer = Viewer() if isinstance(image, np.ndarray): image_from_array = itk.GetImageViewFromArray(image) viewer.image = image_from_array elif have_vtk and isinstance(image, vtk.vtkImageData): from vtk.util import numpy_support as vtk_numpy_support array = vtk_numpy_support.vtk_to_numpy( image.GetPointData().GetScalars()) array.shape = tuple(image.GetDimensions())[::-1] image_from_array = itk.GetImageViewFromArray(array) image_from_array.SetSpacing(image.GetSpacing()) image_from_array.SetOrigin(image.GetOrigin()) viewer.image = image_from_array elif have_imglyb and isinstance( image, imglyb.util.ReferenceGuardingRandomAccessibleInterval): image_array = imglyb.to_numpy(image) print(image_array) image_from_array = itk.GetImageViewFromArray(image_array) else: # an itk.Image viewer.image = image return viewer
def to_itk_image(image_like): if isinstance(image_like, (itk.Image, itk.VectorImage)): return image_like if is_arraylike(image_like): array = np.asarray(image_like) can_use_view = array.flags['OWNDATA'] if have_dask and isinstance(image_like, dask.array.core.Array): can_use_view = False array = np.ascontiguousarray(array) # JavaScript does not support 64-bit integers if array.dtype == np.int64: array = array.astype(np.float32) elif array.dtype == np.uint64: array = array.astype(np.float32) if can_use_view: image_from_array = itk.image_view_from_array(array) else: image_from_array = itk.image_from_array(array) return image_from_array elif have_vtk and isinstance(image_like, vtk.vtkImageData): from vtk.util import numpy_support as vtk_numpy_support array = vtk_numpy_support.vtk_to_numpy( image_like.GetPointData().GetScalars()) dims = list(image_like.GetDimensions()) spacing = list(image_like.GetSpacing()) origin = list(image_like.GetOrigin()) # Check for zdim==1 zdim = dims.pop() if zdim > 1: # zdim>1, put it back in the dims array dims.append(zdim) else: #zdim==1, remove z-spacing and z-origin spacing.pop() origin.pop() array.shape = dims[::-1] image_from_array = itk.image_view_from_array(array) image_from_array.SetSpacing(spacing) image_from_array.SetOrigin(origin) return image_from_array elif have_simpleitk and isinstance(image_like, sitk.Image): array = sitk.GetArrayViewFromImage(image_like) image_from_array = itk.image_view_from_array(array) image_from_array.SetSpacing(image_like.GetSpacing()) image_from_array.SetOrigin(image_like.GetOrigin()) direction = image_like.GetDirection() npdirection = np.asarray(direction) npdirection = np.reshape(npdirection, (-1, image_like.GetDimension())) itkdirection = itk.matrix_from_array(npdirection) image_from_array.SetDirection(itkdirection) return image_from_array elif have_imagej: import imglyb if isinstance(image_like, imglyb.util.ReferenceGuardingRandomAccessibleInterval): array = imglyb.to_numpy(image_like) image_from_array = itk.image_view_from_array(array) return image_from_array elif isinstance(image_like, itk.ProcessObject): return itk.output(image_like) return None