Crop Image by Specifying Region#

Synopsis#

Crop an image by specifying the region to keep.

Results#

Output:

Image largest region: ImageRegion (0x7f886fc0de00)
Dimension: 2
Index: [0, 0]
Size: [10, 10]

desiredRegion: ImageRegion (0x7ffeef707978)
Dimension: 2
Index: [3, 3]
Size: [4, 4]

new largest region: ImageRegion (0x7f886fc0eb00)
Dimension: 2
Index: [3, 3]
Size: [4, 4]

new: 2
Original: 5

Code#

C++#

// This is different from CropImageFilter only in the way
// that the region to crop is specified.
#include "itkImage.h"
#include <itkExtractImageFilter.h>

int
main()
{
  using ImageType = itk::Image<unsigned char, 2>;

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

  ImageType::SizeType size;
  size.Fill(10);

  ImageType::RegionType region(start, size);

  auto image = ImageType::New();
  image->SetRegions(region);
  image->Allocate();
  image->FillBuffer(5);

  std::cout << "Image largest region: " << image->GetLargestPossibleRegion() << std::endl;

  ImageType::IndexType desiredStart;
  desiredStart.Fill(3);

  ImageType::SizeType desiredSize;
  desiredSize.Fill(4);

  ImageType::RegionType desiredRegion(desiredStart, desiredSize);

  std::cout << "desiredRegion: " << desiredRegion << std::endl;

  using FilterType = itk::ExtractImageFilter<ImageType, ImageType>;
  auto filter = FilterType::New();
  filter->SetExtractionRegion(desiredRegion);
  filter->SetInput(image);
#if ITK_VERSION_MAJOR >= 4
  filter->SetDirectionCollapseToIdentity(); // This is required.
#endif
  filter->Update();

  ImageType::Pointer output = filter->GetOutput();
  output->DisconnectPipeline();
  output->FillBuffer(2);

  itk::Index<2> index;
  index.Fill(5);

  std::cout << "new largest region: " << output->GetLargestPossibleRegion() << std::endl;
  std::cout << "new: " << (int)output->GetPixel(index) << std::endl;
  std::cout << "Original: " << (int)image->GetPixel(index) << std::endl;

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TInputImage, typename TOutputImage>
class ExtractImageFilter : public itk::InPlaceImageFilter<TInputImage, TOutputImage>

Decrease the image size by cropping the image to the selected region bounds.

ExtractImageFilter changes the image boundary of an image by removing pixels outside the target region. The target region must be specified.

ExtractImageFilter also collapses dimensions so that the input image may have more dimensions than the output image (i.e. 4-D input image to a 3-D output image). To specify what dimensions to collapse, the ExtractionRegion must be specified. For any dimension dim where ExtractionRegion.Size[dim] = 0, that dimension is collapsed. The index to collapse on is specified by ExtractionRegion.Index[dim]. For example, we have a image 4D = a 4x4x4x4 image, and we want to get a 3D image, 3D = a 4x4x4 image, specified as [x,y,z,2] from 4D (i.e. the 3rd “time” slice from 4D). The ExtractionRegion.Size = [4,4,4,0] and ExtractionRegion.Index = [0,0,0,2].

The number of dimension in ExtractionRegion.Size and Index must = InputImageDimension. The number of non-zero dimensions in ExtractionRegion.Size must = OutputImageDimension.

The output image produced by this filter will have the same origin as the input image, while the ImageRegion of the output image will start at the starting index value provided in the ExtractRegion parameter. If you are looking for a filter that will re-compute the origin of the output image, and provide an output image region whose index is set to zeros, then you may want to use the RegionOfInterestImageFilter. The output spacing is is simply the collapsed version of the input spacing.

Determining the direction of the collapsed output image from an larger dimensional input space is an ill defined problem in general. It is required that the application developer select the desired transformation strategy for collapsing direction cosines. It is REQUIRED that a strategy be explicitly requested (i.e. there is no working default). Direction Collapsing Strategies: 1) DirectionCollapseToUnknown(); This is the default and the filter can not run when this is set. The reason is to explicitly force the application developer to define their desired behavior. 1) DirectionCollapseToIdentity(); Output has identity direction no matter what 2) DirectionCollapseToSubmatrix(); Output direction is the sub-matrix if it is positive definite, else throw an exception.

This filter is implemented as a multithreaded filter. It provides a DynamicThreadedGenerateData() method for its implementation.

Note

This filter is derived from InPlaceImageFilter. When the input to this filter matched the output requested region, like with streaming filter for input, then setting this filter to run in-place will result in no copying of the bulk pixel data.

See

CropImageFilter

ITK Sphinx Examples:

Subclassed by itk::CropImageFilter< TInputImage, TOutputImage >

See itk::ExtractImageFilter for additional documentation.