Skip to content

Commit

Permalink
ENH: Add example of OrientImageFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
blowekamp authored and hjmjohnson committed Nov 20, 2024
1 parent 677b761 commit d3a8a1f
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Examples/Filtering/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ add_executable(BinaryThresholdImageFilter BinaryThresholdImageFilter.cxx)
target_link_libraries(BinaryThresholdImageFilter ${ITK_LIBRARIES})
add_executable(BinaryMedianImageFilter BinaryMedianImageFilter.cxx)
target_link_libraries(BinaryMedianImageFilter ${ITK_LIBRARIES})
add_executable(OrientImageFilter OrientImageFilter.cxx)
target_link_libraries(OrientImageFilter ${ITK_LIBRARIES})
add_executable(OtsuThresholdImageFilter OtsuThresholdImageFilter.cxx)
target_link_libraries(OtsuThresholdImageFilter ${ITK_LIBRARIES})
add_executable(OtsuMultipleThresholdImageFilter OtsuMultipleThresholdImageFilter.cxx)
Expand Down
109 changes: 109 additions & 0 deletions Examples/Filtering/OrientImageFilter.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*=========================================================================
*
* 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 "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include "itkOrientImageFilter.h"

int
main(int argc, char * argv[])
{
if (argc < 4)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0]
<< " inputImageFile outputImageFile "
"desiredPositiveCoordinateOrientation"
<< std::endl;
return EXIT_FAILURE;
}

// Get the inputs from the command line in C++ style
std::string inputImageFile = argv[1];
std::string outputImageFile = argv[2];

itk::AnatomicalOrientation desiredCoordinateOrientation{
itk::AnatomicalOrientation::CreateFromPositiveStringEncoding(argv[3])
};

if (desiredCoordinateOrientation ==
itk::AnatomicalOrientation::PositiveEnum::INVALID)
{
std::cerr << "Invalid desiredPositiveCoordinateOrientation: " << argv[3]
<< std::endl;
std::cerr << "Valid values are of the form LPS, RIP, etc.";
std::cerr << "Where each letter is either L or R, P or A, I or S."
<< std::endl;
return EXIT_FAILURE;
}

// Define the image and reader types
using PixelType = unsigned short;
using ImageType = itk::Image<PixelType, 3>;
using ReaderType = itk::ImageFileReader<ImageType>;

// Create and set up a reader
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(inputImageFile);

reader->UpdateOutputInformation();

auto pixelType = reader->GetImageIO()->GetPixelType();
auto componentType = reader->GetImageIO()->GetComponentType();

if (pixelType != itk::IOPixelEnum::SCALAR ||
componentType != itk::IOComponentEnum::USHORT)
{
itkGenericOutputMacro(
"The input image is being converted to scalar unsigned short.");
}

auto inputDirection = reader->GetOutput()->GetDirection();

std::cout << "Input image direction: " << std::endl;
inputDirection.PrintSelf(std::cout, itk::Indent(2));

std::cout << "The input image has the following orientation: " << std::endl;
std::cout << '\t' << itk::AnatomicalOrientation(inputDirection)
<< std::endl;

std::cout << "The re-orienting to approximately the desired orientation: "
<< std::endl;
std::cout << '\t' << desiredCoordinateOrientation << std::endl;


// Create and set up an OrientImageFilter
using OrientImageFilterType = itk::OrientImageFilter<ImageType, ImageType>;
OrientImageFilterType::Pointer orienter = OrientImageFilterType::New();
orienter->SetInput(reader->GetOutput());
orienter->UseImageDirectionOn();
orienter->SetDesiredCoordinateOrientation(desiredCoordinateOrientation);


// Create and set up a writer
using WriterType = itk::ImageFileWriter<ImageType>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outputImageFile);
writer->SetInput(orienter->GetOutput());

writer->Update();

return EXIT_SUCCESS;
}

0 comments on commit d3a8a1f

Please sign in to comment.