Create an Image of Vectors#
Synopsis#
This example illustrates how to instantiate and use an itk::Image whose pixels are of itk::Vector type.
Results#
C++
[1.345, 6.841, 3.295]
Python
itkVectorF3 ([1.345, 6.841, 3.295])
Code#
Python#
#!/usr/bin/env python
import itk
Dimension = 3
ComponentType = itk.ctype("float")
PixelType = itk.Vector[ComponentType, Dimension]
ImageType = itk.Image[PixelType, Dimension]
image = ImageType.New()
start = itk.Index[Dimension]()
start[0] = 0
start[1] = 0
start[2] = 0
size = itk.Size[Dimension]()
size[0] = 200
size[1] = 200
size[2] = 200
region = itk.ImageRegion[Dimension]()
region.SetSize(size)
region.SetIndex(start)
image.SetRegions(region)
image.Allocate()
vectorValue = PixelType()
vectorValue[0] = 0
vectorValue[1] = 0
vectorValue[2] = 0
image.FillBuffer(vectorValue)
pixelIndex = itk.Index[Dimension]()
pixelIndex[0] = 27
pixelIndex[1] = 29
pixelIndex[2] = 37
pixelValue = PixelType()
pixelValue[0] = 1.345
pixelValue[1] = 6.841
pixelValue[2] = 3.295
image.SetPixel(pixelIndex, pixelValue)
value = image.GetPixel(pixelIndex)
print(value)
C++#
#include "itkVector.h"
#include "itkImage.h"
int
main()
{
using PixelType = itk::Vector<float, 3>;
using ImageType = itk::Image<PixelType, 3>;
// Then the image object can be created
auto image = ImageType::New();
// The image region should be initialized
const ImageType::IndexType start = { { 0, 0, 0 } }; // First index at {X,Y,Z}
const ImageType::SizeType size = { { 200, 200, 200 } }; // Size of {X,Y,Z}
ImageType::RegionType region;
region.SetSize(size);
region.SetIndex(start);
// Pixel data is allocated
image->SetRegions(region);
image->Allocate();
// The image buffer is initialized to a particular value
ImageType::PixelType initialValue;
// A vector can initialize all its components to the
// same value by using the Fill() method.
initialValue.Fill(0.0);
// Now the image buffer can be initialized with this
// vector value.
image->FillBuffer(initialValue);
const ImageType::IndexType pixelIndex = { { 27, 29, 37 } }; // Position {X,Y,Z}
ImageType::PixelType pixelValue;
pixelValue[0] = 1.345; // x component
pixelValue[1] = 6.841; // y component
pixelValue[2] = 3.295; // x component
image->SetPixel(pixelIndex, pixelValue);
ImageType::PixelType value = image->GetPixel(pixelIndex);
std::cout << value << std::endl;
return EXIT_SUCCESS;
}
Classes demonstrated#
-
template<typename T, unsigned int NVectorDimension = 3>
class Vector : public itk::FixedArray<T, NVectorDimension> A templated class holding a n-Dimensional vector.
Vector is a templated class that holds a single vector (i.e., an array of values). Vector can be used as the data type held at each pixel in an Image or at each vertex of an Mesh. The template parameter T can be any data type that behaves like a primitive (or atomic) data type (int, short, float, complex). The NVectorDimension defines the number of components in the vector array.
Vector is not a dynamically extendible array like std::vector. It is intended to be used like a mathematical vector.
If you wish a simpler pixel types, you can use Scalar, which represents a single data value at a pixel. There is also the more complex type ScalarVector, which supports (for a given pixel) a single scalar value plus an array of vector values. (The scalar and vectors can be of different data type.)
- See
Image
- See
Mesh
- See
Point
- See
CovariantVector
- See
Matrix
- ITK Sphinx Examples:
-
template<typename TPixel, unsigned int VImageDimension = 2>
class Image : public itk::ImageBase<VImageDimension> Templated n-dimensional image class.
Images are templated over a pixel type (modeling the dependent variables), and a dimension (number of independent variables). The container for the pixel data is the ImportImageContainer.
Within the pixel container, images are modelled as arrays, defined by a start index and a size.
The superclass of Image, ImageBase, defines the geometry of the image in terms of where the image sits in physical space, how the image is oriented in physical space, the size of a pixel, and the extent of the image itself. ImageBase provides the methods to convert between the index and physical space coordinate frames.
Pixels can be accessed directly using the SetPixel() and GetPixel() methods or can be accessed via iterators that define the region of the image they traverse.
The pixel type may be one of the native types; a Insight-defined class type such as Vector; or a user-defined type. Note that depending on the type of pixel that you use, the process objects (i.e., those filters processing data objects) may not operate on the image and/or pixel type. This becomes apparent at compile-time because operator overloading (for the pixel type) is not supported.
The data in an image is arranged in a 1D array as [][][][slice][row][col] with the column index varying most rapidly. The Index type reverses the order so that with Index[0] = col, Index[1] = row, Index[2] = slice, …
- See
ImageBase
- See
ImageContainerInterface
- ITK Sphinx Examples:
Subclassed by itk::GPUImage< TPixel, VImageDimension >