Scale All Pixel’s Sum to Constant¶
Synopsis¶
Scale all pixels so that their sum is a specified constant.
Results¶
Output:
0.111111
0.111111
0.111111
0.111111
0.111111
0.111111
0.111111
0.111111
0.111111
Code¶
C++¶
#include "itkImage.h"
#include "itkImageRegionConstIterator.h"
#include "itkNormalizeToConstantImageFilter.h"
using ImageType = itk::Image<float, 2>;
static void
CreateImage(ImageType::Pointer image);
int
main(int, char *[])
{
  // Create an image
  ImageType::Pointer image = ImageType::New();
  CreateImage(image);
  using NormalizeToConstantImageFilterType = itk::NormalizeToConstantImageFilter<ImageType, ImageType>;
  NormalizeToConstantImageFilterType::Pointer normalizeToConstantImageFilter =
    NormalizeToConstantImageFilterType::New();
  normalizeToConstantImageFilter->SetInput(image);
  normalizeToConstantImageFilter->SetConstant(1);
  normalizeToConstantImageFilter->Update();
  itk::ImageRegionConstIterator<ImageType> imageIterator(
    normalizeToConstantImageFilter->GetOutput(),
    normalizeToConstantImageFilter->GetOutput()->GetLargestPossibleRegion());
  // The output pixels should all be 1/9 (=0.11111)
  while (!imageIterator.IsAtEnd())
  {
    std::cout << imageIterator.Get() << std::endl;
    ++imageIterator;
  }
  return EXIT_SUCCESS;
}
static void
CreateImage(ImageType::Pointer image)
{
  // Create an image full of 1's
  ImageType::IndexType start;
  start.Fill(0);
  ImageType::SizeType size;
  size.Fill(3);
  ImageType::RegionType region(start, size);
  image->SetRegions(region);
  image->Allocate();
  image->FillBuffer(1);
}
Classes demonstrated¶
- 
template<typename 
TInputImage, typenameTOutputImage>
classNormalizeToConstantImageFilter: public itk::ImageToImageFilter<TInputImage, TOutputImage> Scales image pixel intensities to make the sum of all pixels equal a user-defined constant.
The default value of the constant is 1. It can be changed with SetConstant().
This transform is especially useful for normalizing a convolution kernel.
This code was contributed in the Insight Journal paper: “FFT based
convolution” by Lehmann G.
http://insight-journal.org/browse/publication/717- Author
 Gaetan Lehmann. Biologie du Developpement et de la Reproduction, INRA de Jouy-en-Josas, France.
- See
 NormalizeImageFilter
- See
 StatisticsImageFilter
- See
 DivideImageFilter
- ITK Sphinx Examples:
 

