def numpy_to_itk_displacement(arr:np.ndarray) -> itk.Image: # Verify 32-bit floating point storage assert arr.dtype == np.float32 # Get itk.VectorImage[itk.F,2] vector_image = itk.image_from_array(arr, is_vector=True) assert vector_image.GetVectorLength() == 2 # Cast to itk.Image image_type = itk.Image[itk.Vector[itk.F,2],2] image = itk.cast_image_filter(vector_image, ttype=[type(vector_image), image_type]) # Apply relevant spatial properties. Here we arbitrarily set spacing and origin. image.SetSpacing([0.1,0.2]) image.SetOrigin([1,1]) return image
[itk.UI, np.uint32, "IUC3IUI3"], [np.float32, np.float32, "IUC3IF3"], ]: if hasattr(itk.CastImageFilter, t[2]): cast = image.astype(t[0]) (cast_image_template, (cast_pixel_type, cast_image_dimension)) = itk.template(cast) assert (cast_image_template == input_image_template and cast_image_dimension == input_image_dimension and cast.dtype == t[1]) # test .astype for itk.VectorImage numpyImage = np.random.randint(0, 256, (8, 5, 3)).astype(np.float32) image = itk.image_from_array(numpyImage, is_vector=True) vectorimage = itk.cast_image_filter(Input=image, ttype=(type(image), itk.VectorImage[itk.F, 2])) cast = vectorimage.astype(np.float32) assert cast == vectorimage (vector_image_template, (vector_pixel_type, vector_image_dimension)) = itk.template(vectorimage) for t in [ [itk.D, np.float64, "VIF2VID2"], [itk.SS, np.int16, "VIF2VISS2"], [itk.UI, np.uint32, "VIF2VIUI2"], [np.float64, np.float64, "VIF2VID2"], ]: if hasattr(itk.CastImageFilter, t[2]): cast = vectorimage.astype(t[0]) (cast_image_template, (cast_pixel_type, cast_image_dimension)) = itk.template(cast)
Dimension = 2 if len(sys.argv) > 3: Dimension = int(sys.argv[3]) # Testing GPU Neighborhood Operator Image Filter InputPixelType = itk.F OutputPixelType = itk.F InputImageType = itk.Image[InputPixelType, Dimension] OutputImageType = itk.Image[OutputPixelType, Dimension] InputGPUImageType = itk.GPUImage[InputPixelType, Dimension] OutputGPUImageType = itk.GPUImage[OutputPixelType, Dimension] input_image = itk.imread(input_file, InputPixelType) input_gpu_image = itk.cast_image_filter(input_image, in_place=False, ttype=(InputImageType, InputGPUImageType)) input_gpu_image.UpdateBuffers() RealOutputPixelType = OutputPixelType NeighborhoodFilterType = itk.NeighborhoodOperatorImageFilter[ InputImageType, OutputImageType, RealOutputPixelType] GPUNeighborhoodFilterType = itk.GPUNeighborhoodOperatorImageFilter[ InputGPUImageType, OutputGPUImageType, RealOutputPixelType] # Create 1D Gaussian operator OperatorType = itk.GaussianOperator[RealOutputPixelType, Dimension] oper = OperatorType() oper.SetDirection(0)
cast ) assert ( cast_image_template == input_image_template and cast_image_dimension == input_image_dimension and cast.dtype == t[1] ) # test .astype for itk.VectorImage numpyImage = np.random.randint(0, 256, (8, 5, 3)).astype(np.float32) image = itk.image_from_array(numpyImage, is_vector=True) assert type(image) == type(itk.image_from_array(numpyImage, ttype=(type(image),))) assert type(image) == type(itk.image_from_array(numpyImage, ttype=[type(image)])) assert type(image) == type(itk.image_from_array(numpyImage, ttype=type(image))) ImageVectorsType = itk.Image[itk.Vector[itk.F, 3], 2] imagevectors = itk.cast_image_filter(Input=image, ttype=(type(image), ImageVectorsType)) assert type(imagevectors) == ImageVectorsType cast = image.astype(np.float32) assert cast == image (vector_image_template, (vector_pixel_type, vector_image_dimension)) = itk.template( image ) for t in [ [itk.D, np.float64, "VIF2VID2"], [itk.SS, np.int16, "VIF2VISS2"], [itk.UI, np.uint32, "VIF2VIUI2"], [np.float64, np.float64, "VIF2VID2"], ]: if hasattr(itk.CastImageFilter, t[2]): cast = image.astype(t[0]) (cast_image_template, (cast_pixel_type, cast_image_dimension)) = itk.template(
#!/usr/bin/env python import itk import sys input_filename = sys.argv[1] output_filename = sys.argv[2] image = itk.imread(input_filename) InputType = type(image) # Find input image dimension input_dimension = image.GetImageDimension() # Select float as output pixel type OutputType = itk.Image[itk.UC, input_dimension] # Functional interface casted = itk.cast_image_filter(image, ttype=(InputType, OutputType)) # Object-oriented interface cast_filter = itk.CastImageFilter[InputType, OutputType].New() cast_filter.SetInput(image) cast_filter.Update() casted = cast_filter.GetOutput() # imwrite calls .Update() itk.imwrite(cast_filter, output_filename)
corner.Fill(0) size = itk.Size[dimension]() size.Fill(200) region = itk.ImageRegion[dimension]() region.SetIndex(corner) region.SetSize(size) image = float_image_type.New(Regions=region) image.Allocate() # Make a square image[40:100, 40:100] = 100 else: image = itk.imread(args.input_image, pixel_type=itk.F) # Define some types unsigned_char_image_type = itk.Image[itk.UC, dimension] # Compute the FFT image = itk.forward_fft_image_filter(image) # Compute the IFFT image = itk.inverse_fft_image_filter(image) image = itk.cast_image_filter(image, ttype=(float_image_type, unsigned_char_image_type)) itk.imwrite(image, "ComputeInverseFFTOfImagePython.png")