ITK  5.3.0
Insight Toolkit
SphinxExamples/src/Filtering/ImageGradient/ApplyGradientRecursiveGaussianWithVectorInput/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.
*
*=========================================================================*/
#include "itkImage.h"
int
main(int argc, char * argv[])
{
if (argc != 6)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << " <InputFileName> <OutputFileName1X> <OutputFileName1Y> <OutputFileName2X> <OutputFileName2Y>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
const char * inputFileName = argv[1];
const char * outputFileName1X = argv[2];
const char * outputFileName1Y = argv[3];
const char * outputFileName2X = argv[4];
const char * outputFileName2Y = argv[5];
const char * filenames[4];
filenames[0] = outputFileName1X;
filenames[1] = outputFileName1Y;
filenames[2] = outputFileName2X;
filenames[3] = outputFileName2Y;
constexpr unsigned int ImageDimension = 2;
constexpr unsigned int VectorDimension = 2;
constexpr unsigned int CovDimension = 4;
using PixelType = unsigned char;
using DoublePixelType = double;
const auto input = itk::ReadImage<ImageType>(inputFileName);
// Invert the input image
InvertType::Pointer inverter = InvertType::New();
inverter->SetInput(input);
// Cast the image to double type.
CasterType::Pointer caster = CasterType::New();
CasterType::Pointer caster2 = CasterType::New();
// Create an image, were each pixel has 2 values: first value is the value
// coming from the input image, second value is coming from the inverted
// image
ComposeType::Pointer composer = ComposeType::New();
caster->SetInput(input);
composer->SetInput(0, caster->GetOutput());
caster2->SetInput(inverter->GetOutput());
composer->SetInput(1, caster2->GetOutput());
// Apply the gradient filter on the two images, this will return and image
// with 4 values per pixel: two X and Y gradients
FilterType::Pointer filter = FilterType::New();
filter->SetInput(composer->GetOutput());
// Set up the filter to select each gradient
IndexSelectionType::Pointer indexSelectionFilter = IndexSelectionType::New();
indexSelectionFilter->SetInput(filter->GetOutput());
// Rescale for png output
RescalerType::Pointer rescaler = RescalerType::New();
rescaler->SetOutputMinimum(itk::NumericTraits<PixelType>::min());
rescaler->SetOutputMaximum(itk::NumericTraits<PixelType>::max());
rescaler->SetInput(indexSelectionFilter->GetOutput());
// Write the X and Y images
for (int i = 0; i < 4; ++i)
{
indexSelectionFilter->SetIndex(i);
try
{
itk::WriteImage(rescaler->GetOutput(), filenames[i]);
}
catch (itk::ExceptionObject & error)
{
std::cerr << "Error: " << error << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
itkVectorIndexSelectionCastImageFilter.h
itk::CastImageFilter
Casts input pixels to output pixel type.
Definition: itkCastImageFilter.h:100
itk::InvertIntensityImageFilter
Invert the intensity of an image.
Definition: itkInvertIntensityImageFilter.h:90
itkComposeImageFilter.h
itk::Vector
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
itkImageFileReader.h
itkImage.h
itkCastImageFilter.h
itk::VectorIndexSelectionCastImageFilter
Extracts the selected index of the vector that is the input pixel type.
Definition: itkVectorIndexSelectionCastImageFilter.h:88
itkGradientRecursiveGaussianImageFilter.h
itk::GradientRecursiveGaussianImageFilter
Computes the gradient of an image by convolution with the first derivative of a Gaussian.
Definition: itkGradientRecursiveGaussianImageFilter.h:59
itkRescaleIntensityImageFilter.h
itkVectorMagnitudeImageFilter.h
itkImageFileWriter.h
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::CovariantVector
A templated class holding a n-Dimensional covariant vector.
Definition: itkCovariantVector.h:70
itk::ComposeImageFilter
ComposeImageFilter combine several scalar images into a multicomponent image.
Definition: itkComposeImageFilter.h:62
itk::RescaleIntensityImageFilter
Applies a linear transformation to the intensity levels of the input Image.
Definition: itkRescaleIntensityImageFilter.h:147
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
itk::WriteImage
ITK_TEMPLATE_EXPORT void WriteImage(TImagePointer &&image, const std::string &filename, bool compress=false)
Definition: itkImageFileWriter.h:254
itkInvertIntensityImageFilter.h