ITK  6.0.0
Insight Toolkit
Examples/IO/CovariantVectorImageExtractComponent.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.
*
*=========================================================================*/
// Software Guide : BeginLatex
//
// This example illustrates how to read an image whose pixel type is
// \code{CovariantVector}, extract one of its components to form a scalar
// image and finally save this image into a file.
//
// The \doxygen{VectorIndexSelectionCastImageFilter} is used to extract
// a scalar from the vector image. It is also possible to cast the component
// type when using this filter. It is the user's responsibility to make sure
// that the cast will not result in any information loss.
//
// Let's start by including the relevant header files.
//
// \index{itk::ImageFileRead!Vector images}
// \index{itk::Vector\-Index\-Selection\-Cast\-Image\-Filter!header}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
// Software Guide : EndCodeSnippet
#include "itkImage.h"
int
main(int argc, char ** argv)
{
// Verify the number of parameters in the command line
if (argc < 4)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " inputVectorImageFile outputScalarImageFile";
std::cerr << " outupNormalizedScalarImageFile";
std::cerr << " componentToExtract" << std::endl;
return EXIT_FAILURE;
}
// Software Guide : BeginLatex
//
// We read an image of \doxygen{CovariantVector} pixels and extract one of
// its components to generate a scalar image of a consistent pixel type.
// Then, we rescale the intensities of this scalar image and write it as an
// image of \code{unsigned short} pixels.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
using ComponentType = float;
constexpr unsigned int Dimension = 2;
using OutputPixelType = unsigned short;
using InputImageType = itk::Image<InputPixelType, Dimension>;
using ComponentImageType = itk::Image<ComponentType, Dimension>;
using OutputImageType = itk::Image<OutputPixelType, Dimension>;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The \doxygen{ImageFileReader} and \doxygen{ImageFileWriter}
// are instantiated using the image types.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The VectorIndexSelectionCastImageFilter is instantiated
// using the input and output image types. A filter object is created with
// the \code{New()} method and assigned to a \doxygen{SmartPointer}.
//
// \index{itk::Vector\-Index\-Selection\-Cast\-Image\-Filter!Instantiation}
// \index{itk::Vector\-Index\-Selection\-Cast\-Image\-Filter!New()}
// \index{itk::Vector\-Index\-Selection\-Cast\-Image\-Filter!Pointer}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
using FilterType =
ComponentImageType>;
auto componentExtractor = FilterType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The VectorIndexSelectionCastImageFilter class requires us to specify
// which of the vector components is to be extracted from the vector image.
// This is done with the \code{SetIndex()} method. In this example we
// obtain this value from the command line arguments.
//
// \index{itk::Vector\-Index\-Selection\-Cast\-Image\-Filter!SetIndex()}
//
// Software Guide : EndLatex
const unsigned int indexOfComponentToExtract = std::stoi(argv[4]);
if (indexOfComponentToExtract >= Dimension)
{
std::cerr << "You are requesting an index out of the range for the "
"Vector dimension"
<< std::endl;
std::cerr << "Vector dimension is = " << Dimension << std::endl;
std::cerr << "but your requested index = " << indexOfComponentToExtract
<< std::endl;
return EXIT_FAILURE;
}
// Software Guide : BeginCodeSnippet
componentExtractor->SetIndex(indexOfComponentToExtract);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The \doxygen{RescaleIntensityImageFilter} filter is instantiated here.
//
// \index{RescaleIntensityImageFilter!Instantiation}
// \index{RescaleIntensityImageFilter!New()}
// \index{RescaleIntensityImageFilter!Pointer}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
using RescaleFilterType =
auto rescaler = RescaleFilterType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The minimum and maximum values for the output image are specified in
// the following. Note the use of the \doxygen{NumericTraits} class which
// allows us to define a number of type-related constants in a generic
// way. The use of traits is a fundamental characteristic of generic
// programming~\cite{Austern1999,Alexandrescu2001}.
//
// \index{RescaleIntensityImageFilter!SetOutputMinimum()}
// \index{RescaleIntensityImageFilter!SetOutputMaximum()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
rescaler->SetOutputMinimum(itk::NumericTraits<OutputPixelType>::min());
rescaler->SetOutputMaximum(itk::NumericTraits<OutputPixelType>::max());
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Below, we create the reader and writer using the \code{New()} method and
// assign the result to a \doxygen{SmartPointer}.
//
// \index{itk::ImageFileReader!New()}
// \index{itk::ImageFileWriter!New()}
// \index{itk::ImageFileReader!SmartPointer}
// \index{itk::ImageFileWriter!SmartPointer}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
auto reader = ReaderType::New();
auto writer = WriterType::New();
// Software Guide : EndCodeSnippet
// Here we recover the file names from the command line arguments
//
const char * inputFilename = argv[1];
const char * outputFilename = argv[3];
// Software Guide : BeginLatex
//
// The name of the file to be read or written is passed to the
// \code{SetFileName()} method.
//
// \index{itk::ImageFileReader!SetFileName()}
// \index{itk::ImageFileWriter!SetFileName()}
// \index{SetFileName()!itk::ImageFileReader}
// \index{SetFileName()!itk::ImageFileWriter}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
reader->SetFileName(inputFilename);
writer->SetFileName(outputFilename);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Below we connect the reader, filter and writer to form the data
// processing pipeline.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
componentExtractor->SetInput(reader->GetOutput());
rescaler->SetInput(componentExtractor->GetOutput());
writer->SetInput(rescaler->GetOutput());
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Finally we execute the pipeline by invoking \code{Update()} on the
// writer. The call is placed in a \code{try/catch} block in case
// exceptions are thrown.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
try
{
writer->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
// Software Guide : EndCodeSnippet
// Here We add another writer that will produce the non-normalized output
// file
//
using ComponentWriterType = itk::ImageFileWriter<ComponentImageType>;
auto componentWriter = ComponentWriterType::New();
componentWriter->SetInput(componentExtractor->GetOutput());
componentWriter->SetFileName(argv[2]);
componentWriter->Update();
return EXIT_SUCCESS;
}
itkVectorIndexSelectionCastImageFilter.h
itkImageFileReader.h
itkImage.h
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itk::VectorIndexSelectionCastImageFilter
Extracts the selected index of the vector that is the input pixel type.
Definition: itkVectorIndexSelectionCastImageFilter.h:88
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:90
itkRescaleIntensityImageFilter.h
itkImageFileWriter.h
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:60
itk::CovariantVector
A templated class holding a n-Dimensional covariant vector.
Definition: itkCovariantVector.h:70
itk::RescaleIntensityImageFilter
Applies a linear transformation to the intensity levels of the input Image.
Definition: itkRescaleIntensityImageFilter.h:133
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itk::GTest::TypedefsAndConstructors::Dimension2::Dimension
constexpr unsigned int Dimension
Definition: itkGTestTypedefsAndConstructors.h:44