Set Pixel Value In One Image

Synopsis

This example demonstrates how to set the value of individual pixels in an image.

Results

Output image

Output image

Code

C++

#include "itkImage.h"
#include "itkImageFileWriter.h"

int
main(int argc, char * argv[])
{
  if (argc != 2)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " <OutputFileName>" << std::endl;
    return EXIT_FAILURE;
  }

  constexpr unsigned int Dimension = 2;
  using PixelType = unsigned char;

  using ImageType = itk::Image<PixelType, Dimension>;

  ImageType::RegionType region;

  ImageType::IndexType start;
  start.Fill(0);

  region.SetIndex(start);

  ImageType::SizeType size;
  size[0] = 200;
  size[1] = 300;

  region.SetSize(size);

  ImageType::Pointer image = ImageType::New();
  image->SetRegions(region);
  image->Allocate();
  image->FillBuffer(itk::NumericTraits<PixelType>::Zero);

  ImageType::IndexType pixelIndex;

  for (ImageType::IndexValueType r = 0; r < 50; r++)
  {
    pixelIndex[0] = 4 * r;
    pixelIndex[1] = 4 * r;

    image->SetPixel(pixelIndex, 128);

    pixelIndex[0] = 4 * r;
    pixelIndex[1] = 200 - 4 * r;

    image->SetPixel(pixelIndex, 255);
  }

  for (ImageType::IndexValueType r = 0; r < 25; r++)
  {
    pixelIndex[0] = 8 * r;
    pixelIndex[1] = 200 + 4 * r;

    image->SetPixel(pixelIndex, 128);

    pixelIndex[0] = 8 * r;
    pixelIndex[1] = 250;

    image->SetPixel(pixelIndex, 180);
  }

  pixelIndex[0] = 95;
  pixelIndex[1] = 150;

  image->SetPixel(pixelIndex, 200);

  pixelIndex[0] = 100;
  pixelIndex[1] = 150;

  image->SetPixel(pixelIndex, 200);

  pixelIndex[0] = 105;
  pixelIndex[1] = 150;

  image->SetPixel(pixelIndex, 200);

  pixelIndex[0] = 100;
  pixelIndex[1] = 155;

  image->SetPixel(pixelIndex, 200);

  pixelIndex[0] = 100;
  pixelIndex[1] = 145;

  image->SetPixel(pixelIndex, 200);

  using WriterType = itk::ImageFileWriter<ImageType>;
  WriterType::Pointer writer = WriterType::New();
  writer->SetInput(image);
  writer->SetFileName(argv[1]);

  try
  {
    writer->Update();
  }
  catch (itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Python

#!/usr/bin/env python
import sys
import itk

if len(sys.argv) != 2:
    print('Usage: ' + sys.argv[0] + ' <OutputFileName>')
    sys.exit(1)

Dimension = 2

PixelType = itk.UC

OutputFileName = sys.argv[1]

ImageType = itk.Image[PixelType, Dimension]

region = itk.ImageRegion[Dimension]()

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

region.SetIndex(start)

size = itk.Size[Dimension]()
size[0] = 200
size[1] = 300

region.SetSize(size)

image = ImageType.New()
image.SetRegions(region)
image.Allocate()
image.FillBuffer(itk.NumericTraits[PixelType].ZeroValue())

pixelIndex = itk.Index[Dimension]()

for r in range(50):
    pixelIndex[0] = 4 * r
    pixelIndex[1] = 4 * r

    image.SetPixel(pixelIndex, 128)

    pixelIndex[0] = 4 * r
    pixelIndex[1] = 200 - 4 * r

    image.SetPixel(pixelIndex, 255)

for r in range(25):
    pixelIndex[0] = 8 * r
    pixelIndex[1] = 200 + 4 * r

    image.SetPixel(pixelIndex, 128)

    pixelIndex[0] = 8 * r
    pixelIndex[1] = 250

    image.SetPixel(pixelIndex, 180)

pixelIndex[0] = 95
pixelIndex[1] = 150

image.SetPixel(pixelIndex, 200)

pixelIndex[0] = 100
pixelIndex[1] = 150

image.SetPixel(pixelIndex, 200)

pixelIndex[0] = 105
pixelIndex[1] = 150

image.SetPixel(pixelIndex, 200)

pixelIndex[0] = 100
pixelIndex[1] = 155

image.SetPixel(pixelIndex, 200)

pixelIndex[0] = 100
pixelIndex[1] = 145

image.SetPixel(pixelIndex, 200)

itk.imwrite(image, OutputFileName)

Classes demonstrated

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 >

See itk::Image for additional documentation.