Iterate Region in Image With Write Access#

Synopsis#

Iterate over a region of an image (with write access).

Results#

../../../../_images/bafkreia6f57kj22tmfdijxbost4c47xz3mpixwicor7ix34zvrll3qcbqm

Yinyang.png#

../../../../_images/bafkreidhs2evgjf6avdclcv2ul33x3auyimjiwpebvgqd4stwxghm5ivhy

Yinyang.png In VTK Window#

Code#

C++#

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageRegionIterator.h"

#include <itkImageToVTKImageFilter.h>

#include "vtkVersion.h"
#include "vtkImageViewer.h"
#include "vtkImageMapper3D.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkImageActor.h"
#include "vtkInteractorStyleImage.h"
#include "vtkRenderer.h"

int
main(int argc, char * argv[])
{
  if (argc < 2)
  {
    std::cerr << "Required: filename" << std::endl;
    return EXIT_FAILURE;
  }

  using ImageType = itk::Image<unsigned char, 2>;

  ImageType::Pointer image = itk::ReadImage<ImageType>(argv[1]);

  ImageType::SizeType regionSize;
  regionSize[0] = 5;
  regionSize[1] = 4;

  ImageType::IndexType regionIndex;
  regionIndex[0] = 0;
  regionIndex[1] = 0;

  ImageType::RegionType region;
  region.SetSize(regionSize);
  region.SetIndex(regionIndex);

  itk::ImageRegionIterator<ImageType> imageIterator(image, region);

  while (!imageIterator.IsAtEnd())
  {
    // Get the value of the current pixel
    // unsigned char val = imageIterator.Get();
    // std::cout << (int)val << std::endl;

    // Set the current pixel to white
    imageIterator.Set(255);

    ++imageIterator;
  }

  // Visualize
  using ConnectorType = itk::ImageToVTKImageFilter<ImageType>;
  auto connector = ConnectorType::New();
  connector->SetInput(image);

  vtkSmartPointer<vtkImageActor> actor = vtkSmartPointer<vtkImageActor>::New();
#if VTK_MAJOR_VERSION <= 5
  actor->SetInput(connector->GetOutput());
#else
  connector->Update();
  actor->GetMapper()->SetInputData(connector->GetOutput());
#endif

  vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();

  vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
  interactor->SetRenderWindow(renderWindow);

  vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
  renderWindow->AddRenderer(renderer);

  renderer->AddActor(actor);
  renderer->ResetCamera();

  renderWindow->Render();

  vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New();

  interactor->SetInteractorStyle(style);

  interactor->Start();

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TImage>
class ImageRegionIterator : public itk::ImageRegionConstIterator<TImage>

A multi-dimensional iterator templated over image type that walks a region of pixels.

The itk::ImageRegionIterator is optimized for iteration speed and is the first choice for iterative, pixel-wise operations on an image. ImageRegionIterator is the least specialized of the ITK image iterator classes. ImageRegionIterator is templated over the image type, and is constrained to walk only within the specified region and along a line parallel to one of the coordinate axes, “wrapping” to the next line as it reaches the boundary of the image. To walk the entire image, specify the region to be image->GetRequestedRegion().

Most of the functionality is inherited from the ImageRegionConstIterator. The current class only adds write access to image pixels.

example ImageRegionIterator.cxx

MORE INFORMATION

For a complete description of the ITK Image Iterators and their API, please see the Iterators chapter in the ITK Software Guide. The ITK Software Guide is available in print and as a free .pdf download from https://www.itk.org.

See also

ImageConstIterator

See also

ConditionalConstIterator

See also

ConstNeighborhoodIterator

See also

ConstShapedNeighborhoodIterator

See also

ConstSliceIterator

See also

CorrespondenceDataStructureIterator

See also

FloodFilledFunctionConditionalConstIterator

See also

FloodFilledImageFunctionConditionalConstIterator

See also

FloodFilledImageFunctionConditionalIterator

See also

FloodFilledSpatialFunctionConditionalConstIterator

See also

FloodFilledSpatialFunctionConditionalIterator

See also

ImageConstIterator

See also

ImageConstIteratorWithIndex

See also

ImageIterator

See also

ImageIteratorWithIndex

See also

ImageLinearConstIteratorWithIndex

See also

ImageLinearIteratorWithIndex

See also

ImageRandomConstIteratorWithIndex

See also

ImageRandomIteratorWithIndex

See also

ImageRegionConstIterator

See also

ImageRegionConstIteratorWithIndex

See also

ImageRegionExclusionConstIteratorWithIndex

See also

ImageRegionExclusionIteratorWithIndex

See also

ImageRegionIterator

See also

ImageRegionIteratorWithIndex

See also

ImageRegionReverseConstIterator

See also

ImageRegionReverseIterator

See also

ImageReverseConstIterator

See also

ImageReverseIterator

See also

ImageSliceConstIteratorWithIndex

See also

ImageSliceIteratorWithIndex

See also

NeighborhoodIterator

See also

PathConstIterator

See also

PathIterator

See also

ShapedNeighborhoodIterator

See also

SliceIterator

See also

ImageConstIteratorWithIndex

See also

ImageRegionRange

See also

ImageRegionIndexRange

ITK Sphinx Examples:

See itk::ImageRegionIterator for additional documentation.