def itk_image_to_medipy_image(itk_image, medipy_image, transferOwnership): """ Modify a ``medipy.base.Image`` to match the contents and type of given ITK image. If ``transferOwnership`` is ``True``, then the image will own the data, and the ``itk.Image`` will not. Otherwise, the image does not own the data, and the ``itk.Image`` is unchanged. If ``medipy_image`` is ``None``, then a new image is created. In any case, the medipy Image is returned """ if medipy_image is None : itk_type = itk.template(itk_image)[1][0] dimension = itk.template(itk_image)[1][1] medipy_image = medipy.base.Image(dimension*(0,), types.itk_to_dtype[itk_type]) if itk_image.GetNameOfClass() == "Image" : if not itk.NumpyBridge[itk_image].IsBufferShared(medipy_image.data, itk_image) : medipy_image.data = itk_image_to_array(itk_image, transferOwnership) medipy_image.data_type = "scalar" elif itk_image.GetNameOfClass() == "VectorImage" : if not itk.NumpyBridge[itk_image].IsBufferShared(medipy_image.data, itk_image) : medipy_image.data = itk_vector_image_to_array(itk_image, transferOwnership) medipy_image.data_type = "vector" matrix_type = itk.Matrix[itk.D, medipy_image.ndim, medipy_image.ndim] matrix_bridge = itk.MatrixBridge[matrix_type] itk_direction = matrix_bridge.GetArrayFromMatrix(itk_image.GetDirection()) medipy_image.direction = numpy.fliplr(numpy.flipud(itk_direction)) medipy_image.origin = [x for x in reversed(itk_image.GetOrigin())] medipy_image.spacing = [x for x in reversed(itk_image.GetSpacing())] return medipy_image
def Show(self, itkIm, title="noname") : """ Display an image, with optional title If different titles are used then multiple images will be available in imview. Flip between them with the menu or space bar """ import itk #itkIm = itk.image(itkIm) itkIm = itk.output(itkIm) if not self.connected : # do the login now that we have an image type self.imviewObj = itk.Imview[itkIm] self.Connection = self.imviewObj.ImviewLogin(self.portNum) self.connected = True print "Connected" print self.Connection self.ImageTemp = itk.template(itkIm)[1] else: if itk.template(itkIm)[1] != self.ImageTemp: self.ImageTemp = itk.template(itkIm)[1] self.imviewObj = itk.Imview[itkIm] # transmit the image status = self.imviewObj.ImviewPutImage(itkIm, self.Connection, title) if (status != 0): # Failed to send image. Assume that imview has died self.__StartImview__() status = self.imviewObj.ImviewPutImage(itkIm, self.Connection, title) if (status != 0): print "Something seriously wrong - give up on this Imview instance"
def getInformation(image): # tested """ Returns an information string about a ITK image in a compressed way. Parameters ---------- image : itk.Image An ITK image as used by WrapITK. Returns ------- information : string Pretty-formatted string with image metadata. Notes ----- Performs UpdateOutputInformation() on the image, therefore triggering pipeline processing if necessary Only works on 3D images. """ # refresh information image.UpdateOutputInformation() # request information and format string s = 'itkImageData info:\n' s += '\tscalar-type: {}\n'.format(str(itk.template(image)[1][0])) rs = image.GetLargestPossibleRegion().GetSize() s += '\tdimensions: {}\n'.format([rs.GetElement(x) for x in range(rs.GetSizeDimension())]) sp = image.GetSpacing() s += '\tspacing: {}\n'.format([sp.GetElement(x) for x in range(rs.GetSizeDimension())]) o = image.GetOrigin() s += '\torigin: {}\n'.format([o.GetElement(x) for x in range(rs.GetSizeDimension())]) s += '\tdata dim.: {}'.format(str(itk.template(image)[1][1])) # alternative impl. for when GetImageDimension() fails return s
def itk_displacement_to_strain(image:itk.Image) -> itk.Image: vector_type = itk.template(image)[1][0] real_type = itk.template(vector_type)[1][0] # Functional interface is invalid for StrainImageFilter so we define a filter object. strain_filter = itk.StrainImageFilter[type(image),real_type,real_type].New() strain_filter.SetInput(image) strain_filter.Update() return strain_filter.GetOutput()
def add(ImgA, ImgB): s,d = itk.template(ImgA)[1] assert s,d == itk.template(ImgB)[1] input_type = itk.Image[s,d] Result = itk.AddImageFilter[input_type, input_type, input_type].New() Result.SetInput1(ImgA) Result.SetInput2(ImgB) Result.Update() return Result.GetOutput()
def Overlay(self, itkIm, title="noname") : """Send an overlay to image with specified title""" import itk #itkIm = itk.image(itkIm) itkIm = itk.output(itkIm) if not self.connected : print "No image being viewed - send one first" else: if itk.template(itkIm)[1] != self.ImageTemp: self.ImageTemp = itk.template(itkIm)[1] self.imviewObj = itk.Imview[itkIm] status = self.imviewObj.ImviewPutOverlay(itkIm, self.Connection, title)
def getImageType(image): # tested """ Returns the image type of the supplied image as itk.Image template. @param image: an instance of itk.Image @return a template of itk.Image @rtype itk.Image """ try: return itk.Image[itk.template(image)[1][0], itk.template(image)[1][1]] except IndexError as _: raise NotImplementedError, 'The python wrappers of ITK define no template class for this data type.'
def wasm_type_from_pointset_type(itkpointset): import itk component = itk.template(itkpointset)[1][0] mangle = None pixelType = "Scalar" pixel_type_components = 1 if component in (itk.F, itk.D): mangle = component elif component in [i[1] for i in itk.Array.items()]: mangle = itk.template(component)[1][0] pixelType = "Array" return pixelType, python_to_js(mangle), pixel_type_components
def test_mesh_to_geometry(): # 3D Dimension = 3 PixelType = itk.ctype('double') MeshType = itk.Mesh[PixelType, Dimension] mesh = MeshType.New() PointType = itk.Point[itk.F, Dimension] point0 = PointType() point0[0] = -1 point0[1] = -1 point0[2] = 0 mesh.SetPoint(0, point0) mesh.SetPointData(0, 8.0) point1 = PointType() point1[0] = 1 point1[1] = -1 point1[2] = 0 mesh.SetPointData(1, 9.0) mesh.SetPoint(1, point1) point2 = PointType() point2[0] = 1 point2[1] = 1 point2[2] = 0 mesh.SetPoint(2, point2) mesh.SetPointData(2, 19.0) point3 = PointType() point3[0] = 1 point3[1] = 1 point3[2] = 0 mesh.SetPoint(3, point3) mesh.SetPointData(3, 24.0) geometry = to_geometry(mesh) points = mesh.GetPoints() point_template = itk.template(points) identifier_type = point_template[1][0] element_type = point_template[1][1] point_values = itk.array_from_vector_container(points) assert (geometry['vtkClass'] == 'vtkPolyData') assert (geometry['points']['vtkClass'] == 'vtkPoints') assert (geometry['points']['numberOfComponents'] == 3) assert (geometry['points']['dataType'] == 'Float32Array') assert (geometry['points']['size'] == 4 * 3) assert (np.array_equal(geometry['points']['values'], point_values.astype(np.float32))) assert (geometry['pointData']['vtkClass'] == 'vtkDataSetAttributes') assert (geometry['pointData']['arrays'][0]['data']['vtkClass'] == 'vtkDataArray') assert (geometry['pointData']['arrays'][0]['data']['name'] == 'Point Data') assert ( geometry['pointData']['arrays'][0]['data']['numberOfComponents'] == 1) assert (geometry['pointData']['arrays'][0]['data']['size'] == 4) assert (geometry['pointData']['arrays'][0]['data']['dataType'] == 'Float64Array') assert (np.array_equal( geometry['pointData']['arrays'][0]['data']['values'], np.array([8.0, 9.0, 19.0, 24.0], dtype=np.float64)))
def image_layer_from_image_sitk(image): """Convert an itk.Image to a napari.layers.Image.""" rgb = False try: if isinstance(image, itk.Image): PixelType = itk.template(image)[1][0] if PixelType is itk.RGBPixel[itk.UC] or PixelType is itk.RGBAPixel[ itk.UC]: rgb = True except: pass #print(image["origin"]) try: metadata = dict(image) except: metadata = None scale = image["spacing"] translate = image["origin"] # Todo: convert the rotation matrix to angles, in degrees # rotate = image['direction'] # https://github.com/InsightSoftwareConsortium/itk-napari-conversion/issues/7 data = sitk.GetArrayFromImage(image) if metadata is None: image_layer = napari.layers.Image(data, rgb=rgb, scale=scale, translate=translate) else: image_layer = napari.layers.Image(data, rgb=rgb, metadata=metadata, scale=scale, translate=translate) return image_layer
def __init__(self, *args, **kargs): # call the constructor of the superclass but without args and kargs, because the attributes # are not all already there! # Set/GetRadius() is created in the constructor for example, with the expose() method itk.pipeline.__init__(self) # get the template parameters template_parameters = kargs["template_parameters"] # and store them in an easier way ImageType, DistanceMapType = template_parameters # the maximum value of the image type PixelType, dim = itk.template(ImageType)[1] maxValue = itk.NumericTraits[PixelType].max() # build the minipipeline # use a cast filter to dispatch the input image self.connect(itk.CastImageFilter[ImageType, ImageType].New(InPlace=False)) # dilate the objects in the input image self.connect(itk.BinaryThresholdImageFilter[ImageType, ImageType].New(LowerThreshold=0, UpperThreshold=0, InsideValue=0, OutsideValue=maxValue)) self.connect(itk.BinaryDilateImageFilter[ImageType, ImageType, itk.FlatStructuringElement[dim]].New()) self.expose("Kernel") self.expose("Radius") # compute the voronoi map and cast it to a usable image type self.append(itk.DanielssonDistanceMapImageFilter[ImageType, DistanceMapType].New(self.filters[0], UseImageSpacing=True, SquaredDistance=False)) self.append(itk.CastImageFilter[DistanceMapType, ImageType].New(self.filters[-1].GetVoronoiMap())) # and mask the voronoi map with the dilated objects self.connect(itk.MaskImageFilter[ImageType, ImageType, ImageType].New(Input2=self.filters[2])) # now we can parse the inputs itk.set_inputs(self, args, kargs)
def RelabelComponents(inputImage, outputImageType = None): # relabel = itk.RelabelComponentImageFilter[input_type, output_type].New() # relabel.SetInput(inputImage) # relabel.Update() # return relabel.GetOutput() label_field = itk.GetArrayFromImage(inputImage) offset = 1 max_label = int(label_field.max()) # Ensure max_label is an integer labels, labels_counts= np.unique(label_field,return_counts=True) labels=labels[np.argsort(labels_counts)[::-1]] labels0 = labels[labels != 0] new_max_label = offset - 1 + len(labels0) new_labels0 = np.arange(offset, new_max_label + 1) output_type = label_field.dtype required_type = np.min_scalar_type(new_max_label) if np.dtype(required_type).itemsize > np.dtype(label_field.dtype).itemsize: output_type = required_type forward_map = np.zeros(max_label + 1, dtype=output_type) forward_map[labels0] = new_labels0 inverse_map = np.zeros(new_max_label + 1, dtype=output_type) inverse_map[offset:] = labels0 relabeled = forward_map[label_field] result = itk.GetImageFromArray(relabeled) result.SetOrigin(inputImage.GetOrigin()) result.SetSpacing(inputImage.GetSpacing()) result.SetDirection(inputImage.GetDirection()) if not outputImageType is None: s,d = itk.template(inputImage)[1] output_type = itk.Image[outputImageType,d] result = castImage(result, OutputType=output_type) return result
def binaryThresholding(inputImage, lowerThreshold, upperThreshold, outputImageType = None, insideValue = 1, outsideValue = 0): # Old version: # s,d = itk.template(inputImage)[1] # input_type = itk.Image[s,d] # output_type = input_type if outputImageType is None else itk.Image[outputImageType,d] # thresholder = itk.BinaryThresholdImageFilter[input_type, output_type].New() # thresholder.SetInput(inputImage) # thresholder.SetLowerThreshold( lowerThreshold ) # thresholder.SetUpperThreshold( upperThreshold ) # thresholder.SetInsideValue(insideValue) # thresholder.SetOutsideValue(outsideValue) # thresholder.Update() # return thresholder.GetOutput() values = itk.GetArrayFromImage(inputImage) cond = (values>=lowerThreshold) & (values<=upperThreshold) values[ cond ] = insideValue values[ np.logical_not(cond) ] = outsideValue result = itk.GetImageFromArray(values) result.SetOrigin(inputImage.GetOrigin()) result.SetSpacing(inputImage.GetSpacing()) result.SetDirection(inputImage.GetDirection()) if not outputImageType is None: s,d = itk.template(inputImage)[1] output_type = itk.Image[outputImageType,d] result = castImage(result, OutputType=output_type) return result
def Volume3DToDicom(imgObj, MetadataObj = None, outdir = "", format_templ = "%03d.dcm"): format_templ = os.path.join(outdir, format_templ) entire_region = imgObj.GetLargestPossibleRegion() fngen = itk.NumericSeriesFileNames.New() fngen.SetSeriesFormat(format_templ) fngen.SetStartIndex( entire_region.GetIndex()[2] ) fngen.SetEndIndex( entire_region.GetIndex()[2] + entire_region.GetSize()[2] - 1 ) fngen.SetIncrementIndex(1) if not MetadataObj is None: metadata_array_copy = itk.vector.itkMetaDataDictionary() # I had to create a set of metadata to avoid pointers issues metadata_list_objs = [ itk.MetaDataDictionary() for metadata_list in MetadataObj ] for metadata_list, temp_metadata in zip(MetadataObj, metadata_list_objs): for k,v in metadata_list.items(): temp_metadata[k] = v metadata_array_copy.append(temp_metadata) s,d = itk.template(imgObj)[1] dicom_io = itk.GDCMImageIO.New() writer_type = itk.Image[s,d] writer_otype = itk.Image[s,d-1] writer = itk.ImageSeriesWriter[writer_type, writer_otype].New() writer.SetInput(imgObj) writer.SetImageIO(dicom_io) writer.SetFileNames(fngen.GetFileNames()) if not MetadataObj is None: writer.SetMetaDataDictionaryArray(metadata_array_copy) writer.Update()
def paste_to_common_space(images:list) -> list: image_type = type(images[0]) pixel_type, dimension = itk.template(images[0])[1] resized_images = list() # Verify spacing is equivalent SPACING_TOLERANCE = 1e-7 assert(all([itk.spacing(images[idx])[dim] - itk.spacing(images[0])[dim] < SPACING_TOLERANCE for dim in range(dimension) for idx in range(1,len(images))])) # Get largest common region max_size = itk.size(images[0]) for image in images: max_size = [max(max_size[i], itk.size(image)[i]) for i in range(len(max_size))] # Define paste region for image in images: region = itk.ImageRegion[dimension]() region.SetSize(max_size) region.SetIndex([0] * dimension) new_image = type(images[0]).New(regions=region, spacing=image.GetSpacing()) new_image.Allocate() resized_image = itk.paste_image_filter(source_image=image, source_region=image.GetLargestPossibleRegion(), destination_image=new_image, destination_index=[0] * dimension, ttype=type(image)) resized_images.append(resized_image) return resized_images
def __init__(self, fileName=None, channel=0, ImageType=None): from vtk import vtkLSMReader, vtkImageCast import itk itk.pipeline.__init__(self) # if ImageType is None, give it a default value # this is useful to avoid loading Base while loading this module if ImageType == None: ImageType = itk.Image.UC3 # remove useless SetInput() method created by the constructor of the pipeline class # del self.SetInput # set up the pipeline self.connect(vtkLSMReader()) self.connect(vtkImageCast()) PType = itk.template(ImageType)[1][0] if PType == itk.UC: self[-1].SetOutputScalarTypeToUnsignedChar() elif PType == itk.US: self[-1].SetOutputScalarTypeToUnsignedShort() self.connect(itk.VTKImageToImageFilter[ImageType].New()) self.connect(itk.ChangeInformationImageFilter[ImageType].New( ChangeSpacing=True)) # and configure the pipeline if fileName: self.SetFileName(fileName) self.SetChannel(channel)
def castImage(imgObj, OutputType): s,d = itk.template(imgObj)[1] input_type = itk.Image[s,d] output_type = OutputType castObj = itk.CastImageFilter[input_type, output_type].New() castObj.SetInput(imgObj) castObj.Update() return castObj.GetOutput()
def _get_image_type(image): """Returns the image type of the supplied image as itk.Image template. Args: image: an instance of itk.Image Returns: a template of itk.Image, type itk.Image """ try: return itk.Image[itk.template(image)[1][0], itk.template(image)[1][1]] except IndexError: raise ( NotImplementedError, 'The python wrappers of ITK define no template class for this data type.' )
def _set_t_98(self, value): # Cast value to input pixel type of BETImageFilter itk_type = itk.template(itk.template(self._bet_filter)[1][1])[1][0] value = medipy.itk.itk_to_dtype[itk_type](value) self._bet_filter.SetT98(value) colormap = self._image.get_layer_colormap(self._intensity_range_layer) colormap.display_range = (colormap.display_range[0], value) self.ui.intensity_range.remove_observer("value", self.on_intensity_range_value) self.ui.intensity_range.value = (self.ui.intensity_range.value[0], value) self.ui.intensity_range.add_observer("value", self.on_intensity_range_value) self.notify_observers("t_98")
def ConnectedComponents(inputImage, outputImageType = None): s,d = itk.template(inputImage)[1] input_type = itk.Image[s,d] output_type = input_type if outputImageType is None else itk.Image[outputImageType,d] CC = itk.ConnectedComponentImageFilter[input_type, output_type].New() CC.SetInput(inputImage) CC.Update() return CC.GetOutput()
def getImageType(image): # tested """ Returns the image type of the supplied image as itk.Image template. Parameters ---------- image : itk.Image (instance) An instance of itk.Image. Returns ------- image_type : itk.Image (template) An itk image type. """ try: return itk.Image[itk.template(image)[1][0], itk.template(image)[1][1]] except IndexError as _: raise NotImplementedError, 'The python wrappers of ITK define no template class for this data type.'
def binary_image_list_to_meshes(images:list, mesh_pixel_type:type=itk.F, object_pixel_value=1) -> list: _, dimension = itk.template(images[0])[1] mesh_type = itk.Mesh[mesh_pixel_type, dimension] mesh_list = list() for image in images: mesh = itk.binary_mask3_d_mesh_source(input=image, object_value=object_pixel_value, ttype=[type(image), mesh_type]) mesh_list.append(mesh) return mesh_list
def Gaussian(GaussInput, sigma, outputImageType = None): s,d = itk.template(GaussInput)[1] input_type = itk.Image[s,d] output_type = input_type if outputImageType is None else itk.Image[outputImageType,d] OperationObj = itk.DiscreteGaussianImageFilter[input_type, output_type].New() OperationObj.SetInput(GaussInput) OperationObj.SetVariance(sigma) OperationObj.Update() return OperationObj.GetOutput()
def mesh_to_image(meshes: list, reference_image: itk.Image = None) -> itk.Image: # Allow single mesh as input if type(meshes) != list: meshes = [meshes] mesh_type = type(meshes[0]) pixel_type, dimension = itk.template(mesh_type)[1] image_type = itk.Image[pixel_type, dimension] mesh_to_image_filter_type = itk.TriangleMeshToBinaryImageFilter[mesh_type, image_type] images = list() if not reference_image: # Set bounds to largest region encompassing all meshes # Bounds format: [x_min x_max y_min y_max z_min z_max] bounds = meshes[0].GetBoundingBox().GetBounds() for mesh in meshes[1:]: mesh_bounds = mesh.GetBoundingBox().GetBounds() for dim in range(0, dimension): bounds[dim * 2] = min(bounds[dim * 2], mesh_bounds[dim * 2]) bounds[(dim * 2) + 1] = max(bounds[(dim * 2) + 1], mesh_bounds[(dim * 2) + 1]) # Calculate spacing, origin, and size with 5 pixel buffer around mesh spacing = ((bounds[1] - bounds[0]) / 90, (bounds[3] - bounds[2]) / 90, (bounds[5] - bounds[4]) / 90) origin = (bounds[0] - 5 * spacing[0], bounds[2] - 5 * spacing[1], bounds[4] - 5 * spacing[2]) size = (100, 100, 100) direction = itk.Matrix[itk.D, dimension, dimension].GetIdentity() else: # Use given parameters origin = reference_image.GetOrigin() spacing = reference_image.GetSpacing() size = reference_image.GetLargestPossibleRegion().GetSize() direction = reference_image.GetDirection() # Generate image for each mesh for mesh in meshes: mesh_to_image_filter = mesh_to_image_filter_type.New( Input=mesh, Origin=origin, Spacing=spacing, Size=size, Direction=direction) mesh_to_image_filter.Update() distance = itk.signed_maurer_distance_map_image_filter( mesh_to_image_filter.GetOutput()) images.append(distance) return images[0] if len(images) == 1 else images
def streamline(model, step=0.5, minimum_fa=0.2, maximum_angle=numpy.pi/3, minimum_length=50, propagation_type="Euler", seed_spacing=None, mask=None) : """ Deterministic streamline propagation algorithm, return a list of tracks, where a track is a list of points in physical coordinates. * ``model`` : tensor field * ``step`` : propagation step * ``minimum_fa`` : minimum fractional anisotropy value allowed for propagation * ``maximum_angle`` : minimum angle value (in radians) allowed for propagation * ``minimum_length`` : minimum fiber length, in physical units * ``propagation_type`` : propagation criterion, may be either ``"Euler"`` or ``"RungeKuttaOrder4"`` * ``seed_spacing`` : spacing between seeds in physical units, defaults to ``2*model.spacing`` * ``mask`` : optional mask to restrict seeding """ if seed_spacing is None : seed_spacing = model.spacing*(2.0,) seeds = _generate_image_sampling(model, seed_spacing/model.spacing, mask) itk_model = medipy.itk.medipy_image_to_itk_image(model, False) ScalarImage = itk.Image[itk.template(itk_model)[1]] tractography_filter = itk.StreamlineTractographyAlgorithm[ itk_model, ScalarImage].New() tractography_filter.SetInputModel(itk_model) for seed in seeds: tractography_filter.AppendSeed(seed[::-1]) tractography_filter.SetStepSize(step) tractography_filter.SetUseRungeKuttaOrder4(propagation_type=="RungeKuttaOrder4") tractography_filter.SetMaximumAngle(maximum_angle) tractography_filter.SetMinimumFA(minimum_fa) tractography_filter.SetMinimumLength(minimum_length) mask_itk = None if mask : mask_itk = medipy.itk.medipy_image_to_itk_image(mask, False) tractography_filter.SetMask(mask_itk) tractography_filter.Update() fibers = [] for i in range(tractography_filter.GetNumberOfFibers()) : fiber = tractography_filter.GetOutputFiberAsPyArray(i) if _length(fiber,step)>=minimum_length : fibers.append(fiber) return fibers
def spatial_parameter_estimation(tensor, size_plane=3, size_depth=3, mask=None): """ Neighborhood-based estimation of the mean and standard deviation of second-order tensors. <gui> <item name="tensor" type="Image" label="DTI data"/> <item name="size_plane" type="Int" initializer="3" label="Neighborhood plane size"/> <item name="size_depth" type="Int" initializer="3" label="Neighborhood depth size"/> <item name="mask" type="Image" initializer="may_be_empty=True, may_be_empty_checked=True" label="Mask"/> <item name="mean" type="Image" initializer="output=True" role="return" label="Mean image"/> <item name="stdev" type="Image" initializer="output=True" role="return" label="Standard deviation image"/> </gui> """ log_tensor = log_transformation(tensor) log_tensor_itk = medipy.itk.medipy_image_to_itk_image(log_tensor, False) ScalarImage = itk.Image[itk.template(log_tensor_itk)[1]] mask_itk = None MaskImage = ScalarImage if mask: mask_itk = medipy.itk.medipy_image_to_itk_image(mask, False) MaskImage = mask_itk.__class__ estimation_filter = itk.SpatialDWIStatisticsImageFilter[ log_tensor_itk, log_tensor_itk, ScalarImage, MaskImage].New(Input=log_tensor_itk, SizePlane=size_plane, SizeDepth=size_depth) if mask: estimation_filter.SetMaskImage(mask_itk) estimation_filter() mean_itk = estimation_filter.GetMeanImage() mean = medipy.itk.itk_image_to_medipy_image(mean_itk, None, True) mean.image_type = "tensor_2" mean = exp_transformation(mean) standard_deviation_itk = estimation_filter.GetStandardDeviationImage() standard_deviation = medipy.itk.itk_image_to_medipy_image( standard_deviation_itk, None, True) return mean, standard_deviation
def _GetArrayFromVnlObject(vnl_object, function): """Get an array with the content of vnl_object """ # Finds the vnl object type import itk PixelType = itk.template(vnl_object)[1][0] keys = [k for k in itk.PyVnl.keys() if k[0] == PixelType] if len(keys) == 0: raise RuntimeError("No suitable template parameter can be found.") # Create a numpy array of the type of the vnl object templatedFunction = getattr(itk.PyVnl[keys[0]], function) return templatedFunction(vnl_object)
def linearTransform(Img, scale, shift, outputImageType = None): s,d = itk.template(Img)[1] input_type = itk.Image[s,d] output_type = input_type if outputImageType is None else itk.Image[outputImageType,d] Result = itk.ShiftScaleImageFilter[input_type, output_type].New() Result.SetInput(Img) Result.SetScale(scale) Result.SetShift(shift) Result.Update() return Result.GetOutput()
def resampling_transform(image, shape): imageType = itk.template(image)[0][itk.template(image)[1]] dummy_image = itk.image_from_array(np.zeros(tuple(reversed(shape)), dtype=itk.array_from_image(image).dtype)) if len(shape) == 2: transformType = itk.MatrixOffsetTransformBase[itk.D, 2, 2] else: transformType = itk.VersorRigid3DTransform[itk.D] initType = itk.CenteredTransformInitializer[transformType, imageType, imageType] initializer = initType.New() initializer.SetFixedImage(dummy_image) initializer.SetMovingImage(image) transform = transformType.New() initializer.SetTransform(transform) initializer.InitializeTransform() if len(shape) == 3: transformType = itk.MatrixOffsetTransformBase[itk.D, 3, 3] t2 = transformType.New() t2.SetCenter(transform.GetCenter()) t2.SetOffset(transform.GetOffset()) transform = t2 m = transform.GetMatrix() m_a = itk.array_from_matrix(m) input_shape = image.GetLargestPossibleRegion().GetSize() for i in range(len(shape)): m_a[i, i] = image.GetSpacing()[i] * (input_shape[i] / shape[i]) m_a = itk.array_from_matrix(image.GetDirection()) @ m_a transform.SetMatrix(itk.matrix_from_array(m_a)) return transform
def can_save(self, image): if image.image_type != "tensor_2": return False BridgedTypes = set([itk.template(x[0])[1][0] for x in itk.NumpyBridge]) PixelType = medipy.itk.dtype_to_itk[image.dtype.type] while PixelType not in BridgedTypes: PixelType = medipy.itk.types.larger_type[PixelType] Dimension = image.ndim VectorImageType = itk.VectorImage[PixelType, Dimension] return (VectorImageType, ) in itk.Tensor2ImageFileWriter.__template__
def getInformation(image): # tested """ Returns an information string about a ITK image in a compressed way. Parameters ---------- image : itk.Image An ITK image as used by WrapITK. Returns ------- information : string Pretty-formatted string with image metadata. Notes ----- Performs UpdateOutputInformation() on the image, therefore triggering pipeline processing if necessary Only works on 3D images. """ # refresh information image.UpdateOutputInformation() # request information and format string s = 'itkImageData info:\n' s += '\tscalar-type: {}\n'.format(str(itk.template(image)[1][0])) rs = image.GetLargestPossibleRegion().GetSize() s += '\tdimensions: {}\n'.format( [rs.GetElement(x) for x in range(rs.GetSizeDimension())]) sp = image.GetSpacing() s += '\tspacing: {}\n'.format( [sp.GetElement(x) for x in range(rs.GetSizeDimension())]) o = image.GetOrigin() s += '\torigin: {}\n'.format( [o.GetElement(x) for x in range(rs.GetSizeDimension())]) s += '\tdata dim.: {}'.format(str(itk.template( image)[1][1])) # alternative impl. for when GetImageDimension() fails return s
def GetArrayFromVnlMatrix(vnlMatrix): """Get an Array with the content of the vnl matrix """ # Check for numpy if not HAVE_NUMPY: raise ImportError('Numpy not available.') # Finds the vnl matrix type import itk PixelType = itk.template(vnlMatrix)[1][0] keys = [k for k in itk.PyVnl.keys() if k[0] == PixelType] if len(keys) == 0: raise RuntimeError("No suitable template parameter can be found.") # Create a numpy array of the type of the vnl matrix return itk.PyVnl[keys[0]].GetArrayFromVnlMatrix(vnlMatrix)
def GetArrayFromVnlMatrix(vnlMatrix): """Get an Array with the content of the vnl matrix """ # Check for numpy if not HAVE_NUMPY: raise ImportError('Numpy not available.') # Finds the vnl matrix type import itk PixelType = itk.template(vnlMatrix)[1][0] keys = [k for k in itk.PyVnl.keys() if k[0] == PixelType] if len(keys ) == 0: raise RuntimeError("No suitable template parameter can be found.") # Create a numpy array of the type of the vnl matrix return itk.PyVnl[keys[0]].GetArrayFromVnlMatrix(vnlMatrix)
def main(args): img = itk.imread(args.img) img_np = itk.GetArrayViewFromImage(img).astype(float) num_splits = img_np.shape[args.axis] img_np_split = np.split(img_np, num_splits, axis=args.axis) out_shape = np.delete(np.shape(img_np), args.axis) PixelType = itk.template(img)[1][0] Dimension = 2 PixelDimension = img.GetNumberOfComponentsPerPixel() Origin = np.delete(np.array(img.GetOrigin())[::-1], args.axis) Spacing = np.delete(np.array(img.GetSpacing())[::-1], args.axis) index = itk.Index[Dimension]() index.Fill(0) size = itk.Size[Dimension]() size.Fill(1) for i, s in enumerate(np.copy(out_shape)[::-1]): size[i] = int(s) RegionType = itk.ImageRegion[Dimension] Region = RegionType() Region.SetIndex(index) Region.SetSize(size) OutputImageType = itk.VectorImage[PixelType, 2] if PixelDimension > 1: np.append(out_shape, PixelDimension) for i, slice_np in enumerate(img_np_split): # print(np.reshape(slice_np, out_shape).shape) out_img = OutputImageType.New() out_img.SetNumberOfComponentsPerPixel(PixelDimension) out_img.SetOrigin(Origin) out_img.SetSpacing(Spacing) out_img.SetRegions(Region) out_img.Allocate() out_img_np = itk.GetArrayViewFromImage(out_img) out_img_np.setfield(np.reshape(slice_np, out_shape), out_img_np.dtype) out_name = os.path.join(args.out, args.prefix + str(i) + args.ext) print("Writing:", out_name) writer = itk.ImageFileWriter.New(FileName=out_name, Input=out_img) writer.UseCompressionOn() writer.Update()
def _GetArrayFromVnlObject(vnl_object, function): """Get an array with the content of vnl_object """ # Check for numpy if not HAVE_NUMPY: raise ImportError('Numpy not available.') # Finds the vnl object type import itk PixelType = itk.template(vnl_object)[1][0] keys = [k for k in itk.PyVnl.keys() if k[0] == PixelType] if len(keys ) == 0: raise RuntimeError("No suitable template parameter can be found.") # Create a numpy array of the type of the vnl object templatedFunction = getattr(itk.PyVnl[keys[0]], function) return templatedFunction(vnl_object)
def save(self, image): BridgedTypes = set([itk.template(x[0])[1][0] for x in itk.NumpyBridge]) PixelType = medipy.itk.dtype_to_itk[image.dtype.type] while PixelType not in BridgedTypes: PixelType = medipy.itk.types.larger_type[PixelType] Dimension = image.ndim VectorImageType = itk.VectorImage[PixelType, Dimension] itk_image = medipy.itk.medipy_image_to_itk_image(image, False) writer = itk.Tensor2ImageFileWriter[VectorImageType].New( ImageIO=self._saver, Input=itk_image) writer.SetFileName(self._filename) writer.Update()
def _compute_scalar(image, scalar_name): """ Compute a scalar from a tensor image. """ Filter = getattr(itk, "{0}ImageFilter".format(scalar_name)) itk_image = medipy.itk.medipy_image_to_itk_image(image, False) ScalarImage = itk.Image[itk.template(itk_image)[1]] filter_ = Filter[itk_image, ScalarImage].New(Input=itk_image) filter_() itk_output = filter_[0] output = medipy.itk.itk_image_to_medipy_image(itk_output, None, True) output.data[numpy.isnan(output.data)] = 0 output.data[numpy.isinf(output.data)] = 0 return output
def __init__(self, *args, **kargs): # call the constructor of the superclass but without args and kargs, because the attributes # are not all already there! # Set/GetRadius() is created in the constructor for example, with the expose() method itk.pipeline.__init__(self) # get the template parameters template_parameters = kargs["template_parameters"] # and store them in an easier way ImageType, DistanceMapType = template_parameters # the maximum value of the image type PixelType, dim = itk.template(ImageType)[1] LabelMapType = itk.LabelMap[itk.StatisticsLabelObject[itk.UL, dim]] # build the minipipeline self.connect(itk.LabelMapToLabelImageFilter[LabelMapType, ImageType].New()) self.connect(ClosestLabelDilateImageFilter[ImageType, DistanceMapType].New()) self.expose("Kernel") self.expose("Radius") self.connect(itk.LabelImageToLabelMapFilter[ImageType, LabelMapType].New()) # now we can parse the inputs itk.set_inputs(self, args, kargs)
def __init__(self, fileName=None, channel=0, ImageType=None ): from vtk import vtkLSMReader, vtkImageCast import itk itk.pipeline.__init__(self) # if ImageType is None, give it a default value # this is useful to avoid loading Base while loading this module if ImageType == None: ImageType = itk.Image.UC3 # remove useless SetInput() method created by the constructor of the pipeline class # del self.SetInput # set up the pipeline self.connect( vtkLSMReader() ) self.connect( vtkImageCast() ) PType = itk.template(ImageType)[1][0] if PType == itk.UC: self.filters[-1].SetOutputScalarTypeToUnsignedChar() elif PType == itk.US: self.filters[-1].SetOutputScalarTypeToUnsignedShort() self.connect( itk.VTKImageToImageFilter[ImageType].New() ) self.connect( itk.ChangeInformationImageFilter[ImageType].New( ChangeSpacing=True ) ) # and configure the pipeline if fileName: self.SetFileName( fileName ) self.SetChannel( channel )
def __init__(self, imageOrFilter, Label=False, Title=None): import tempfile import itk import os import platform # get some data from the environment command = os.environ.get("WRAPITK_SHOW2D_COMMAND") if command is None: if platform.system() == "Darwin": command = ( "open -a ImageJ -n --args -eval 'open(\"%(image)s\"); " "run (\"View 100%%\"); rename(\"%(title)s\");'") else: command = ( "imagej %(image)s -run 'View 100%%' -eval " "'rename(\"%(title)s\")' &") label_command = os.environ.get("WRAPITK_SHOW2D_LABEL_COMMAND") if label_command is None: if platform.system() == "Darwin": label_command = ( "open -a ImageJ -n --args -eval 'open(\"%(image)s\"); " "run (\"View 100%%\"); rename(\"%(title)s\"); " "run(\"3-3-2 RGB\");'") else: label_command = ( "imagej %(image)s -run 'View 100%%' -eval " "'rename(\"%(title)s\")' -run '3-3-2 RGB' &") compress = os.environ.get( "WRAPITK_SHOW2D_COMPRESS", "true").lower() in ["on", "true", "yes", "1"] extension = os.environ.get("WRAPITK_SHOW2D_EXTENSION", ".tif") # use the tempfile module to get a non used file name and to put # the file at the rignt place self.__tmpFile__ = tempfile.NamedTemporaryFile(suffix=extension) # get an updated image img = output(imageOrFilter) img.UpdateOutputInformation() img.Update() if Title is None: # try to generate a title s = img.GetSource() if s: s = itk.down_cast(s) if hasattr(img, "GetSourceOutputIndex"): o = '[%s]' % img.GetSourceOutputIndex() elif hasattr(img, "GetSourceOutputName"): o = '[%s]' % img.GetSourceOutputName() else: o = "" Title = "%s%s" % (s.__class__.__name__, o) else: Title = img.__class__.__name__ try: import IPython ip = IPython.get_ipython() if ip is not None: names = [] ref = imageOrFilter if s: ref = s for n, v in ip.user_ns.iteritems(): if isinstance(v, itk.LightObject) and v == ref: names.append(n) if names != []: Title = ", ".join(names) + " - " + Title except ImportError: # just do nothing pass # change the LabelMaps to an Image, so we can look at them easily if 'LabelMap' in dir(itk) and img.GetNameOfClass() == 'LabelMap': # retreive the biggest label in the label map maxLabel = img.GetNthLabelObject( img.GetNumberOfLabelObjects() - 1).GetLabel() # search for a filter to convert the label map lab = itk.LabelMapToLabelImageFilter.keys() maxVal = itk.NumericTraits[itk.template(params[1])[1][0]].max() cond = params[0] == class_(img) and maxVal >= maxLabel label_image_type = sorted([params[1] for params in lab if cond])[0] convert = itk.LabelMapToLabelImageFilter[ img, label_image_type].New(img) convert.Update() img = convert.GetOutput() # this is a label image - force the parameter Label = True write(img, self.__tmpFile__.name, compress) # now run imview import os if Label: os.system( label_command % {"image": self.__tmpFile__.name, "title": Title}) else: os.system( command % {"image": self.__tmpFile__.name, "title": Title})
# template should return the same class with a class as parameter # or with an object of this class, and should also be the same # with the attribute # create instances of image for the next tests im = ImageType.New() im2 = ImageType.New() readerType = itk.ImageFileReader[ImageType] readerType2 = itk.ImageFileReader[im] readerType3 = itk.ImageFileReader.IUC2 assert readerType == readerType2 == readerType3 # we should be able to get the template and its parameters from the class (tpl, parameters) = itk.template(ImageType) assert tpl == itk.Image assert parameters == (PixelType, dim) # test that `isinstance` works obj = itk.ImageFileReader[ImageType].New() assert isinstance(obj, itk.ImageFileReader.IUC2) assert isinstance(obj, itk.ImageFileReader) # the template must raise a KeyError exception if the template parameter # is unknown try: itk.ImageFileReader['unknown parameter'] raise Exception('no exception sent for unknown parameter') except TypeError as e: print("Exception caught!")
IType = itk.Image[PType, dim] ReaderType = itk.ImageFileReader[IType] reader = ReaderType.New(FileName=fileName) # test echo itk.echo(reader) itk.echo(reader, sys.stdout) # test class_ assert itk.class_(reader) == ReaderType assert itk.class_(reader.GetPointer()) == ReaderType assert itk.class_("dummy") == str # test template assert itk.template(ReaderType) == (itk.ImageFileReader, (IType,)) assert itk.template(reader) == (itk.ImageFileReader, (IType,)) assert itk.template(reader.GetPointer()) == (itk.ImageFileReader, (IType,)) try: itk.template(str) raise Exception("unknown class should send an exception") except KeyError: pass # test ctype assert itk.ctype("unsigned short") == itk.US assert itk.ctype(" unsigned \n short \t ") == itk.US try: itk.ctype("dummy") raise Exception("unknown C type should send an exception") except KeyError:
def __init__(self, imageOrFilter, Label=False) : import tempfile, itk, os # get some data from the environment command = os.environ.get("WRAPITK_SHOW2D_COMMAND", "imview %s -fork") label_command = os.environ.get("WRAPITK_SHOW2D_LABEL_COMMAND", "imview %s -c regions.lut -fork") compress = os.environ.get("WRAPITK_SHOW2D_COMPRESS", "true").lower() in ["on", "true", "yes", "1"] extension = os.environ.get("WRAPITK_SHOW2D_EXTENSION", ".tif") # use the tempfile module to get a non used file name and to put # the file at the rignt place self.__tmpFile__ = tempfile.NamedTemporaryFile(suffix=extension) # get an updated image img = output(imageOrFilter) img.UpdateOutputInformation() img.Update() # change the LabelMaps to an Image, so we can look at them easily if 'LabelMap' in dir(itk) and img.GetNameOfClass() == 'LabelMap': # retreive the biggest label in the label map maxLabel = img.GetNthLabelObject( img.GetNumberOfLabelObjects() - 1 ).GetLabel() # search for a filter to convert the label map label_image_type = sorted( [params[1] for params in itk.LabelMapToLabelImageFilter.keys() if params[0] == class_(img) and itk.NumericTraits[itk.template(params[1])[1][0]].max() >= maxLabel ] )[0] convert = itk.LabelMapToLabelImageFilter[ img, label_image_type ].New( img ) convert.Update() img = convert.GetOutput() # this is a label image - force the parameter Label = True write(img, self.__tmpFile__.name, compress) # now run imview import os if Label: os.system( label_command % self.__tmpFile__.name) else: os.system( command % self.__tmpFile__.name)
ReaderType = itk.ImageFileReader[ImageType] reader = ReaderType.New(FileName=fileName) # test snake_case keyword arguments reader = ReaderType.New(file_name=fileName) # test echo itk.echo(reader) itk.echo(reader, sys.stdout) # test class_ assert itk.class_(reader) == ReaderType assert itk.class_("dummy") == str # test template assert itk.template(ReaderType) == (itk.ImageFileReader, (ImageType,)) assert itk.template(reader) == (itk.ImageFileReader, (ImageType,)) try: itk.template(str) raise Exception("unknown class should send an exception") except KeyError: pass # test ctype assert itk.ctype("unsigned short") == itk.US assert itk.ctype(" unsigned \n short \t ") == itk.US assert itk.ctype("signed short") == itk.SS assert itk.ctype("short") == itk.SS try: itk.ctype("dummy") raise Exception("unknown C type should send an exception")
# template should return the same class with a class as parameter # or with an object of this class, and should also be the same # with the attribute # create instances of image for the next tests im = IType.New() im2 = IType.New() readerType = itk.ImageFileReader[IType] readerType2 = itk.ImageFileReader[im] readerType3 = itk.ImageFileReader.IUC2 assert readerType == readerType2 == readerType3 # we should be able to get the template and its parameters from the class (tpl, parameters) = itk.template( IType ) assert tpl == itk.Image assert parameters == (PType, dim) # the template must raise a KeyError exception if the template parameter # is unknown try : itk.ImageFileReader['unknown parameter'] raise Exception('no exception sent for unknown parameter') except KeyError: pass # TODO: test the rest of the dict interface # TODO: test __eq__, __ne__ and __hash__ # something else ?