Create An Image

Synopsis

This example illustrates how to manually construct Image.

This example illustrates how to manually construct an itk::Image class. The following is the minimal code needed to instantiate, declare and create the Image class.

Results

Image (0x1eb5f10)
  RTTI typeinfo:   itk::Image<unsigned char, 3u>
  Reference Count: 1
  Modified Time: 4
  Debug: Off
  Object Name:
  Observers:
    none
  Source: (none)
  Source output name: (none)
  Release Data: Off
  Data Released: False
  Global Release Data: Off
  PipelineMTime: 0
  UpdateMTime: 0
  RealTimeStamp: 0 seconds
  LargestPossibleRegion:
    Dimension: 3
    Index: [0, 0, 0]
    Size: [200, 200, 200]
  BufferedRegion:
    Dimension: 3
    Index: [0, 0, 0]
    Size: [200, 200, 200]
  RequestedRegion:
    Dimension: 3
    Index: [0, 0, 0]
    Size: [200, 200, 200]
  Spacing: [1, 1, 1]
  Origin: [0, 0, 0]
  Direction:
1 0 0
0 1 0
0 0 1

  IndexToPointMatrix:
1 0 0
0 1 0
0 0 1

  PointToIndexMatrix:
1 0 0
0 1 0
0 0 1

  Inverse Direction:
1 0 0
0 1 0
0 0 1

  PixelContainer:
    ImportImageContainer (0x1a678a0)
      RTTI typeinfo:   itk::ImportImageContainer<unsigned long, unsigned char>
      Reference Count: 1
      Modified Time: 5
      Debug: Off
      Object Name:
      Observers:
        none
      Pointer: 0x7facf287d010
      Container manages memory: true
      Size: 8000000
      Capacity: 8000000

Code

C++

#include "itkImage.h"

int
main(int, char *[])
{
  using ImageType = itk::Image<unsigned char, 3>;
  ImageType::Pointer image = ImageType::New();

  ImageType::IndexType start;
  start[0] = 0; // first index on X
  start[1] = 0; // first index on Y
  start[2] = 0; // first index on Z

  ImageType::SizeType size;
  size[0] = 200; // size along X
  size[1] = 200; // size along Y
  size[2] = 200; // size along Z

  ImageType::RegionType region;
  region.SetSize(size);
  region.SetIndex(start);

  image->SetRegions(region);
  image->Allocate();

  std::cout << image << std::endl;

  return EXIT_SUCCESS;
}

Python

#!/usr/bin/env python

Dimension = 3
PixelType = itk.ctype('unsigned char')
ImageType = itk.Image[PixelType, Dimension]

image = ImageType.New()

start = itk.Index[Dimension]()
start[0] = 0  # first index on X
start[1] = 0  # first index on Y
start[2] = 0  # first index on Z

size = itk.Size[Dimension]()
size[0] = 200  # size along X
size[1] = 200  # size along Y
size[2] = 200  # size along Z

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

image.SetRegions(region)
image.Allocate()

print(image)

Classes demonstrated