SimpleITK  
N4BiasFieldCorrection/N4BiasFieldCorrection.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 <SimpleITK.h>
#include <iostream>
#include <stdlib.h>
namespace sitk = itk::simple;
int
main(int argc, char * argv[])
{
if (argc < 2)
{
std::cerr << "Usage: N4BiasFieldCorrection inputImage outputImage";
std::cerr << " [shrinkFactor] [maskImage] [numberOfIterations]";
std::cerr << " [numberOfFittingLevels]\n";
return 1;
}
sitk::Image inputImage = sitk::ReadImage(argv[1], sitk::sitkFloat32);
sitk::Image image = inputImage;
sitk::Image maskImage;
if (argc > 4)
{
maskImage = sitk::ReadImage(argv[4], sitk::sitkUInt8);
}
else
{
maskImage = sitk::OtsuThreshold(image, 0, 1, 200);
}
unsigned int shrinkFactor = 1;
if (argc > 3)
{
shrinkFactor = atoi(argv[3]);
std::vector<unsigned int> shrink(inputImage.GetDimension(), shrinkFactor);
image = sitk::Shrink(inputImage, shrink);
maskImage = sitk::Shrink(maskImage, shrink);
}
unsigned int numFittingLevels = 4;
if (argc > 6)
{
numFittingLevels = atoi(argv[6]);
}
if (argc > 5)
{
unsigned int it = atoi(argv[5]);
std::vector<unsigned int> iterations(numFittingLevels, it);
corrector.SetMaximumNumberOfIterations(iterations);
}
sitk::Image corrected_image = corrector.Execute(image, maskImage);
sitk::Image log_bias_field = corrector.GetLogBiasFieldAsImage(inputImage);
sitk::Image corrected_image_full_resolution = sitk::Divide(inputImage, sitk::Exp(log_bias_field));
sitk::WriteImage(corrected_image_full_resolution, argv[2]);
if (shrinkFactor > 1)
{
sitk::WriteImage(corrected_image, "CXX-Example-N4BiasFieldCorrection-shrunk.nrrd");
}
if (getenv("SITK_NOSHOW") == NULL)
sitk::Show(corrected_image, "N4 Corrected");
return 0;
}
itk::simple::Image
The Image class for SimpleITK.
Definition: sitkImage.h:76
itk::simple::Exp
Image Exp(Image &&image1)
Computes the exponential function of each pixel.
itk::simple::Show
void SITKIO_EXPORT Show(const Image &image, const std::string &title="", const bool debugOn=ProcessObject::GetGlobalDefaultDebug())
SimpleITK.h
itk::simple::N4BiasFieldCorrectionImageFilter
Implementation of the N4 bias field correction algorithm.
Definition: sitkN4BiasFieldCorrectionImageFilter.h:73
itk::simple::ReadImage
SITKIO_EXPORT Image ReadImage(const PathType &filename, PixelIDValueEnum outputPixelType=sitkUnknown, const std::string &imageIO="")
ReadImage is a procedural interface to the ImageFileReader class which is convenient for most image r...
itk::simple::Shrink
Image Shrink(const Image &image1, std::vector< unsigned int > shrinkFactors=std::vector< unsigned int >(3, 1))
Reduce the size of an image by an integer factor in each dimension.
itk::simple::OtsuThreshold
Image OtsuThreshold(const Image &image, const Image &maskImage, uint8_t insideValue=1u, uint8_t outsideValue=0u, uint32_t numberOfHistogramBins=128u, bool maskOutput=true, uint8_t maskValue=255u, bool returnBinMidpoint=false)
Threshold an image using the Otsu Threshold.
itk::simple::Image::GetDimension
unsigned int GetDimension() const
itk::simple::WriteImage
SITKIO_EXPORT void WriteImage(const Image &image, const PathType &fileName, bool useCompression=false, int compressionLevel=-1)
WriteImage is a procedural interface to the ImageFileWriter. class which is convenient for many image...
itk::simple::N4BiasFieldCorrectionImageFilter::SetMaximumNumberOfIterations
Self & SetMaximumNumberOfIterations(std::vector< uint32_t > MaximumNumberOfIterations)
Definition: sitkN4BiasFieldCorrectionImageFilter.h:101
itk::simple::Divide
Image Divide(Image &&image1, const Image &image2)
Pixel-wise division of two images.
itk::simple::N4BiasFieldCorrectionImageFilter::GetLogBiasFieldAsImage
Image GetLogBiasFieldAsImage(Image referenceImage) const
The computed log bias field correction. Typically, a reduced size image is used as input to the N4 fi...
Definition: sitkN4BiasFieldCorrectionImageFilter.h:219
itk::simple::N4BiasFieldCorrectionImageFilter::Execute
Image Execute(const Image &image, const Image &maskImage)
itk::simple
Definition: sitkAdditionalProcedures.h:28