Shrink Image#
Synopsis#
Shrink an image.
Results#
Output:
Original size: [100, 100]
New size: [50, 33]
Code#
C++#
#include "itkImage.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkShrinkImageFilter.h"
using ImageType = itk::Image<unsigned char, 2>;
static void
CreateImage(ImageType::Pointer image);
int
main()
{
auto image = ImageType::New();
CreateImage(image);
std::cout << "Original size: " << image->GetLargestPossibleRegion().GetSize() << std::endl;
using ShrinkImageFilterType = itk::ShrinkImageFilter<ImageType, ImageType>;
auto shrinkFilter = ShrinkImageFilterType::New();
shrinkFilter->SetInput(image);
shrinkFilter->SetShrinkFactor(0, 2); // shrink the first dimension by a factor of 2 (i.e. 100 gets changed to 50)
shrinkFilter->SetShrinkFactor(1, 3); // shrink the second dimension by a factor of 3 (i.e. 100 gets changed to 33)
shrinkFilter->Update();
std::cout << "New size: " << shrinkFilter->GetOutput()->GetLargestPossibleRegion().GetSize() << std::endl;
return EXIT_SUCCESS;
}
void
CreateImage(ImageType::Pointer image)
{
// Create an image with 2 connected components
ImageType::IndexType start;
start.Fill(0);
ImageType::SizeType size;
size.Fill(100);
ImageType::RegionType region(start, size);
image->SetRegions(region);
image->Allocate();
image->FillBuffer(0);
// Make a white square
for (unsigned int r = 20; r < 80; ++r)
{
for (unsigned int c = 20; c < 30; ++c)
{
ImageType::IndexType pixelIndex;
pixelIndex[0] = r;
pixelIndex[1] = c;
image->SetPixel(pixelIndex, 255);
}
}
}
Classes demonstrated#
-
template<typename TInputImage, typename TOutputImage>
class ShrinkImageFilter : public itk::ImageToImageFilter<TInputImage, TOutputImage> Reduce the size of an image by an integer factor in each dimension.
ShrinkImageFilter reduces the size of an image by an integer factor in each dimension. The algorithm implemented is a simple subsample. The output image size in each dimension is given by:
outputSize[j] = max( std::floor(inputSize[j]/shrinkFactor[j]), 1 );
NOTE: The physical centers of the input and output will be the same. Because of this, the Origin of the output may not be the same as the Origin of the input. Since this filter produces an image which is a different resolution, origin and with different pixel spacing than its input image, it needs to override several of the methods defined in ProcessObject in order to properly manage the pipeline execution model. In particular, this filter overrides ProcessObject::GenerateInputRequestedRegion() and ProcessObject::GenerateOutputInformation().
This filter is implemented as a multithreaded filter. It provides a DynamicThreadedGenerateData() method for its implementation.
- ITK Sphinx Examples: