ITK  5.0.0
Insight Segmentation and Registration Toolkit
WikiExamples/ImageProcessing/ShapeOpeningLabelMapFilter.cxx
#include "itkImage.h"
using ImageType = itk::Image<unsigned char, 2>;
using LabelImageType = itk::Image<unsigned char, 2>;
static void CreateImage(ImageType::Pointer image);
int main(int, char *[])
{
ImageType::Pointer image = ImageType::New();
CreateImage(image);
// Create a ShapeLabelMap from the image
using BinaryImageToShapeLabelMapFilterType = itk::BinaryImageToShapeLabelMapFilter<ImageType>;
BinaryImageToShapeLabelMapFilterType::Pointer binaryImageToShapeLabelMapFilter = BinaryImageToShapeLabelMapFilterType::New();
binaryImageToShapeLabelMapFilter->SetInput(image);
binaryImageToShapeLabelMapFilter->Update();
// Remove label objects that have PERIMETER greater than 50
ShapeOpeningLabelMapFilterType::Pointer shapeOpeningLabelMapFilter = ShapeOpeningLabelMapFilterType::New();
shapeOpeningLabelMapFilter->SetInput( binaryImageToShapeLabelMapFilter->GetOutput() );
shapeOpeningLabelMapFilter->SetLambda( 50 );
shapeOpeningLabelMapFilter->ReverseOrderingOn();
shapeOpeningLabelMapFilter->SetAttribute( ShapeOpeningLabelMapFilterType::LabelObjectType::PERIMETER);
shapeOpeningLabelMapFilter->Update();
// Create a label image
LabelMapToLabelImageFilterType::Pointer labelMapToLabelImageFilter = LabelMapToLabelImageFilterType::New();
labelMapToLabelImageFilter->SetInput(shapeOpeningLabelMapFilter->GetOutput());
labelMapToLabelImageFilter->Update();
using RGBPixelType = itk::RGBPixel<unsigned char>;
using RGBImageType = itk::Image<RGBPixelType, 2>;
// Color each label/object a different color
RGBFilterType::Pointer colormapImageFilter = RGBFilterType::New();
colormapImageFilter->SetInput(labelMapToLabelImageFilter->GetOutput());
colormapImageFilter->SetColormap( RGBFilterType::Jet );
colormapImageFilter->Update();
// Write the output
WriterType::Pointer writer = WriterType::New();
writer->SetInput( colormapImageFilter->GetOutput() );
writer->SetFileName("output.png");
writer->Update();
return EXIT_SUCCESS;
}
void CreateImage(ImageType::Pointer image)
{
// Create a black image with three white squares
start.Fill(0);
size.Fill(200);
region.SetSize(size);
region.SetIndex(start);
image->SetRegions(region);
image->Allocate();
itk::ImageRegionIterator<ImageType> imageIterator(image,image->GetLargestPossibleRegion());
// Make a square
while(!imageIterator.IsAtEnd())
{
if(((imageIterator.GetIndex()[0] > 5 && imageIterator.GetIndex()[0] < 10) &&
(imageIterator.GetIndex()[1] > 5 && imageIterator.GetIndex()[1] < 10)) ||
((imageIterator.GetIndex()[0] > 50 && imageIterator.GetIndex()[0] < 60) &&
(imageIterator.GetIndex()[1] > 50 && imageIterator.GetIndex()[1] < 60)) ||
((imageIterator.GetIndex()[0] > 100 && imageIterator.GetIndex()[0] < 130) &&
(imageIterator.GetIndex()[1] > 100 && imageIterator.GetIndex()[1] < 130)) )
{
imageIterator.Set(255);
}
else
{
imageIterator.Set(0);
}
++imageIterator;
}
WriterType::Pointer writer = WriterType::New();
writer->SetFileName("input.png");
writer->SetInput(image);
writer->Update();
}