ITKSphinxExamples icon indicating copy to clipboard operation
ITKSphinxExamples copied to clipboard

VectorImage Python example

Open thewtex opened this issue 6 years ago • 4 comments

Transferred from https://github.com/InsightSoftwareConsortium/ITK/issues/548 :

Description

An example of how to read and write an Image with an unknown number of Components will be great. The VectorImage is wrapped in itk python and it works great but there is no usage example.

Expected behavior

An example at the ITKExamples would be great.

Actual behavior

There is no example of this feature.

Additional Information

Dimension = 3
ComponentType = itk.ctype('float')
OutputImageType = itk.VectorImage[ComponentType, Dimension]

out_img = OutputImageType.New()
out_img.SetNumberOfComponentsPerPixel(40)

size = itk.Size[Dimension]()
size.Fill(64)

index = itk.Index[Dimension]()
index.Fill(0)

RegionType = itk.ImageRegion[Dimension]
region = RegionType()
region.SetIndex(index)
region.SetSize(size)

out_img.SetRegions(region)
out_img.SetDirection(img.GetDirection())
out_img.SetOrigin(img.GetOrigin())
out_img.SetSpacing(img.GetSpacing())
out_img.Allocate()

thewtex avatar Jun 29 '19 02:06 thewtex

The following returns a vector image

image = itk.imread(file_path, pixel_type=itk.VariableLengthVector[itk.F])

EDIT: Removed dimension

tbirdso avatar May 03 '22 16:05 tbirdso

If the number of components is known before the file is read and unchanging then it might be better to use Vector[itk.F,2].

Leengit avatar May 03 '22 19:05 Leengit

I had the template wrong, itk.VectorImage is useful for vector sizes that are unknown or not explicitly wrapped, you are right. For example we often use 31-component vectors in ITKUltrasound, but itk.Vector is only wrapped for small dimensions (2,3,4). I have corrected the snippet above.

tbirdso avatar May 03 '22 19:05 tbirdso

Take a look at https://github.com/InsightSoftwareConsortium/ITK/blob/231ecb7d3dbbb11bc0c60f54a782a4d30f8ec4e2/Wrapping/Generators/Python/Tests/readWriteVLV.py, or even at https://github.com/InsightSoftwareConsortium/ITK/blob/0e84a057ec742bef99b3c42c0802fb0974fe39dc/Modules/Core/Common/wrapping/test/itkVariableLengthVectorTest.py.

dzenanz avatar May 17 '22 09:05 dzenanz