ITK  5.3.0
Insight Toolkit
SphinxExamples/src/Filtering/ImageGrid/ResampleAnImage/Code.cxx
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
int
main(int argc, char * argv[])
{
if (argc != 4)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << " <InputFileName> <OutputFileName> <scale>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
const char * inputFileName = argv[1];
const char * outputFileName = argv[2];
const float scale = std::stod(argv[3]);
constexpr unsigned int Dimension = 2;
using PixelType = unsigned char;
using ScalarType = double;
const typename ImageType::Pointer inputImage = itk::ReadImage<ImageType>(inputFileName);
const typename ImageType::RegionType inputRegion = inputImage->GetLargestPossibleRegion();
const typename ImageType::SizeType inputSize = inputRegion.GetSize();
const typename ImageType::SpacingType inputSpacing = inputImage->GetSpacing();
const typename ImageType::PointType inputOrigin = inputImage->GetOrigin();
/*
* We will scale the objects in the image by the factor `scale`; that is they
* will be shrunk (scale < 1.0) or enlarged (scale > 1.0). However, the
* number of pixels for each dimension of the output image will equal the
* corresponding number of pixels in the input image, with padding (if
* shrunk) or cropping (if enlarged) as necessary. Furthermore, the physical
* distance between adjacent pixels will be the same in the input and the
* output images. In contrast, if you want to change the resolution of the
* image without changing the represented physical size of the objects in the
* image, omit the transform and instead use:
*
* outputSize[d] = inputSize[d] * scale;
* outputSpacing[d] = inputSpacing[d] / scale;
* outputOrigin[d] = inputOrigin[d] + 0.5 * (outputSpacing[d] - inputSpacing[d]);
*
* in the loop over dimensions.
*/
typename ImageType::SizeType outputSize = inputSize;
typename ImageType::SpacingType outputSpacing = inputSpacing;
typename ImageType::PointType outputOrigin = inputOrigin;
using ScaleTransformType = itk::ScaleTransform<ScalarType, Dimension>;
typename ScaleTransformType::Pointer scaleTransform = ScaleTransformType::New();
typename ScaleTransformType::ParametersType scaleTransformParameters = scaleTransform->GetParameters();
itk::Point<ScalarType, Dimension> scaleTransformCenter;
for (unsigned int d = 0; d < Dimension; ++d)
{
scaleTransformParameters[d] = scale;
scaleTransformCenter[d] = static_cast<ScalarType>(static_cast<IndexValueType>(inputSize[d] / 2));
}
scaleTransform->SetParameters(scaleTransformParameters);
scaleTransform->SetCenter(scaleTransformCenter);
typename LinearInterpolatorType::Pointer interpolator = LinearInterpolatorType::New();
typename ResampleFilterType::Pointer resampleFilter = ResampleFilterType::New();
resampleFilter->SetInput(inputImage);
resampleFilter->SetTransform(scaleTransform);
resampleFilter->SetInterpolator(interpolator);
resampleFilter->SetSize(outputSize);
resampleFilter->SetOutputSpacing(outputSpacing);
resampleFilter->SetOutputOrigin(outputOrigin);
try
{
itk::WriteImage(resampleFilter->GetOutput(), outputFileName);
}
catch (itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
itkLinearInterpolateImageFunction.h
itk::GTest::TypedefsAndConstructors::Dimension2::PointType
ImageBaseType::PointType PointType
Definition: itkGTestTypedefsAndConstructors.h:51
itk::Index::IndexValueType
::itk::IndexValueType IndexValueType
Definition: itkIndex.h:78
itkImageFileReader.h
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itk::ScaleTransform
Scale transformation of a vector space (e.g. space coordinates)
Definition: itkScaleTransform.h:41
itk::LinearInterpolateImageFunction
Linearly interpolate an image at specified positions.
Definition: itkLinearInterpolateImageFunction.h:50
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itkImageFileWriter.h
itk::IndexValueType
signed long IndexValueType
Definition: itkIntTypes.h:90
itk::ResampleImageFilter
Resample an image via a coordinate transform.
Definition: itkResampleImageFilter.h:90
itk::Point
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:53
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
itkResampleImageFilter.h
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itk::WriteImage
ITK_TEMPLATE_EXPORT void WriteImage(TImagePointer &&image, const std::string &filename, bool compress=false)
Definition: itkImageFileWriter.h:254
itkScaleTransform.h
itk::Size::GetSize
const SizeValueType * GetSize() const
Definition: itkSize.h:169