ITK  5.4.0
Insight Toolkit
SphinxExamples/src/Filtering/ImageGrid/ChangeImageOriginSpacingOrDirection/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
*
* https://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.
*
*=========================================================================*/
#include "itkVersor.h"
int
main(int argc, char * argv[])
{
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " <inputFileName>"
<< " [scalingFactor]"
<< " [translationX translationY translationZ]"
<< " [rotationZinDegrees]" << std::endl;
return EXIT_FAILURE;
}
const char * inputFileName = argv[1];
double scalingFactor = 1.0;
if (argc > 3)
{
scalingFactor = std::stod(argv[3]);
}
double translationX = 0.0;
if (argc > 3)
{
translationX = std::stod(argv[3]);
}
double translationY = 0.0;
if (argc > 4)
{
translationY = std::stod(argv[4]);
}
double translationZ = 0.0;
if (argc > 5)
{
translationZ = std::stod(argv[5]);
}
double rotationZ = 0.0;
if (argc > 6)
{
rotationZ = std::stod(argv[6]);
}
constexpr unsigned int Dimension = 3;
using PixelType = unsigned char;
using ReaderType = itk::ImageFileReader<ImageType>;
auto reader = ReaderType::New();
reader->SetFileName(inputFileName);
try
{
reader->UpdateOutputInformation();
}
catch (const itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
ImageType::ConstPointer inputImage = reader->GetOutput();
std::cout << "Original image: " << inputImage << std::endl;
auto filter = FilterType::New();
filter->SetInput(reader->GetOutput());
const ImageType::SpacingType spacing(scalingFactor);
filter->SetOutputSpacing(spacing);
filter->ChangeSpacingOn();
translation[0] = translationX;
translation[1] = translationY;
translation[2] = translationZ;
ImageType::PointType origin = inputImage->GetOrigin();
origin += translation;
filter->SetOutputOrigin(origin);
filter->ChangeOriginOn();
const double angleInRadians = rotationZ * itk::Math::pi / 180.0;
rotation.SetRotationAroundZ(angleInRadians);
const ImageType::DirectionType direction = inputImage->GetDirection();
const ImageType::DirectionType newDirection = direction * rotation.GetMatrix();
filter->SetOutputDirection(newDirection);
filter->ChangeDirectionOn();
try
{
filter->UpdateOutputInformation();
}
catch (const itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
std::cout << "**************************************" << std::endl;
ImageType::ConstPointer output = filter->GetOutput();
std::cout << "Changed image: " << output << std::endl;
return EXIT_SUCCESS;
}
ConstPointer
SmartPointer< const Self > ConstPointer
Definition: itkAddImageFilter.h:94
itk::GTest::TypedefsAndConstructors::Dimension2::DirectionType
ImageBaseType::DirectionType DirectionType
Definition: itkGTestTypedefsAndConstructors.h:52
itk::GTest::TypedefsAndConstructors::Dimension2::VectorType
ImageBaseType::SpacingType VectorType
Definition: itkGTestTypedefsAndConstructors.h:53
itk::GTest::TypedefsAndConstructors::Dimension2::PointType
ImageBaseType::PointType PointType
Definition: itkGTestTypedefsAndConstructors.h:51
itk::Versor::SetRotationAroundZ
void SetRotationAroundZ(ValueType angle)
itkImageFileReader.h
itk::ChangeInformationImageFilter
Change the origin, spacing and/or region of an Image.
Definition: itkChangeInformationImageFilter.h:49
itk::Versor
A templated class holding a unit quaternion.
Definition: itkVersor.h:50
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itkVersor.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itk::Versor::GetMatrix
MatrixType GetMatrix() const
itk::Math::pi
static constexpr double pi
Definition: itkMath.h:66
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44
itkChangeInformationImageFilter.h