static void
CreateImage(RGBImageType::Pointer image);
int
main(int, char *[])
{
constexpr unsigned int MeasurementVectorSize = 3;
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;
histogramSize[1] = 4;
histogramSize[2] = 4;
histogramFilter->SetHistogramSize(histogramSize);
histogramFilter->SetMarginalScale(10);
histogramFilter->Update();
const HistogramType * histogram = histogramFilter->GetOutput();
HistogramType::ConstIterator histogramIterator = histogram->Begin();
while (histogramIterator != histogram->End())
{
++histogramIterator;
}
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)
{
size[0] = 3;
size[1] = 3;
start[0] = 0;
start[1] = 0;
region.SetSize(size);
image->SetRegions(region);
image->Allocate();
RGBPixelType redPixel;
redPixel.SetRed(255);
redPixel.SetGreen(0);
redPixel.SetBlue(0);
RGBPixelType blackPixel;
blackPixel.SetRed(0);
blackPixel.SetGreen(0);
blackPixel.SetBlue(0);
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);
}
{
mask->SetRegions(region);
mask->Allocate();
mask->FillBuffer(0);
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();
}