ITK  5.2.0
Insight Toolkit
SphinxExamples/src/Numerics/Statistics/ComputeHistogramOfMaskedRegion/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"
#include "itkRGBPixel.h"
using RGBPixelType = itk::RGBPixel<unsigned char>;
using RGBImageType = itk::Image<RGBPixelType, 2>;
using UnsignedCharImageType = itk::Image<unsigned char, 2>;
static void
CreateImage(RGBImageType::Pointer image);
static void CreateHalfMask(itk::ImageRegion<2>, UnsignedCharImageType::Pointer mask);
int
main(int, char *[])
{
constexpr unsigned int MeasurementVectorSize = 3; // RGB
RGBImageType::Pointer image = RGBImageType::New();
CreateImage(image);
UnsignedCharImageType::Pointer mask = UnsignedCharImageType::New();
CreateHalfMask(image->GetLargestPossibleRegion(), mask);
using HistogramMeasurementVectorType = HistogramFilterType::HistogramMeasurementVectorType;
using HistogramSizeType = HistogramFilterType::HistogramSizeType;
using HistogramType = HistogramFilterType::HistogramType;
HistogramFilterType::Pointer histogramFilter = HistogramFilterType::New();
histogramFilter->SetInput(image);
histogramFilter->SetMaskImage(mask);
histogramFilter->SetAutoMinimumMaximum(true);
HistogramSizeType histogramSize(MeasurementVectorSize);
histogramSize[0] = 4; // number of bins for the Red channel
histogramSize[1] = 4; // number of bins for the Green channel
histogramSize[2] = 4; // number of bins for the Blue channel
histogramFilter->SetHistogramSize(histogramSize);
histogramFilter->SetMarginalScale(10); // Required (could this be set in the filter?)
histogramFilter->Update();
const HistogramType * histogram = histogramFilter->GetOutput();
HistogramType::ConstIterator histogramIterator = histogram->Begin();
// std::string filename = "/home/doriad/histogram.txt";
// std::ofstream fout(filename.c_str());
while (histogramIterator != histogram->End())
{
// std::cout << "Index = " << histogramIterator.GetMeasurementVector() << "Frequency = " <<
// histogramIterator.GetFrequency() << std::endl; std::cout << "Index = " << histogramIterator.GetIndex() <<
// "Frequency = " << histogramIterator.GetFrequency() << std::endl; fout << "Index = " <<
// histogram->GetIndex(histogramItr.GetMeasurementVector()) << "Frequency = " << histogramItr.GetFrequency() <<
// std::endl;
++histogramIterator;
}
// fout.close();
HistogramType::MeasurementVectorType mv(3);
mv[0] = 255;
mv[1] = 0;
mv[2] = 0;
std::cout << "Frequency = " << histogram->GetFrequency(histogram->GetIndex(mv)) << std::endl;
return EXIT_SUCCESS;
}
void
CreateImage(RGBImageType::Pointer image)
{
// Create a black image with a red square and a green square.
// This should produce a histogram with very strong spikes.
size[0] = 3;
size[1] = 3;
start[0] = 0;
start[1] = 0;
region.SetIndex(start);
region.SetSize(size);
image->SetRegions(region);
image->Allocate();
itk::ImageRegionIteratorWithIndex<RGBImageType> iterator(image, image->GetLargestPossibleRegion());
iterator.GoToBegin();
RGBPixelType redPixel;
redPixel.SetRed(255);
redPixel.SetGreen(0);
redPixel.SetBlue(0);
RGBPixelType blackPixel;
blackPixel.SetRed(0);
blackPixel.SetGreen(0);
blackPixel.SetBlue(0);
itk::ImageRegionIterator<RGBImageType> imageIterator(image, region);
while (!imageIterator.IsAtEnd())
{
imageIterator.Set(blackPixel);
++imageIterator;
}
index[0] = 0;
index[1] = 0;
image->SetPixel(index, redPixel);
index[0] = 1;
index[1] = 0;
image->SetPixel(index, redPixel);
}
void CreateHalfMask(itk::ImageRegion<2> region, UnsignedCharImageType::Pointer mask)
{
mask->SetRegions(region);
mask->Allocate();
mask->FillBuffer(0);
itk::Size<2> regionSize = region.GetSize();
// Make the left half of the mask white and the right half black
while (!imageIterator.IsAtEnd())
{
if (imageIterator.GetIndex()[0] > regionSize[0] / 2)
{
imageIterator.Set(0);
}
else
{
imageIterator.Set(1);
}
++imageIterator;
}
RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
rescaleFilter->SetInput(mask);
rescaleFilter->Update();
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("mask.png");
writer->SetInput(rescaleFilter->GetOutput());
writer->Update();
}
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itk::Statistics::MaskedImageToHistogramFilter
Generate a histogram from the masked pixels of an image.
Definition: itkMaskedImageToHistogramFilter.h:46
itkRGBPixel.h
itk::Size< 2 >
itk::ImageRegion
An image region represents a structured region of data.
Definition: itkImageRegion.h:69
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkImage.h
itk::ImageRegion::GetSize
const SizeType & GetSize() const
Definition: itkImageRegion.h:181
itkImageRegionIterator.h
itkImageRegionIteratorWithIndex.h
itk::ImageRegionIterator
A multi-dimensional iterator templated over image type that walks a region of pixels.
Definition: itkImageRegionIterator.h:80
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:88
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itkRescaleIntensityImageFilter.h
itk::ImageRegionIteratorWithIndex
A multi-dimensional iterator templated over image type that walks pixels within a region and is speci...
Definition: itkImageRegionIteratorWithIndex.h:73
itkImageFileWriter.h
itk::ImageConstIteratorWithIndex::GoToBegin
void GoToBegin()
itk::ImageRegion::SetIndex
void SetIndex(const IndexType &index)
Definition: itkImageRegion.h:153
itk::RescaleIntensityImageFilter
Applies a linear transformation to the intensity levels of the input Image.
Definition: itkRescaleIntensityImageFilter.h:154
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itkMaskedImageToHistogramFilter.h