Convert Label Map to Normal Image#
Synopsis#
Convert a LabelMap to a normal image with different values representing each region
Results#
Code#
C++#
#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkImageRegionIterator.h"
#include "itkBinaryImageToLabelMapFilter.h"
#include "itkLabelMapToLabelImageFilter.h"
using ImageType = itk::Image<unsigned char, 2>;
static void
CreateImage(ImageType::Pointer image);
int
main()
{
auto image = ImageType::New();
CreateImage(image);
using BinaryImageToLabelMapFilterType = itk::BinaryImageToLabelMapFilter<ImageType>;
auto binaryImageToLabelMapFilter = BinaryImageToLabelMapFilterType::New();
binaryImageToLabelMapFilter->SetInput(image);
binaryImageToLabelMapFilter->Update();
using LabelMapToLabelImageFilterType =
itk::LabelMapToLabelImageFilter<BinaryImageToLabelMapFilterType::OutputImageType, ImageType>;
auto labelMapToLabelImageFilter = LabelMapToLabelImageFilterType::New();
labelMapToLabelImageFilter->SetInput(binaryImageToLabelMapFilter->GetOutput());
labelMapToLabelImageFilter->Update();
return EXIT_SUCCESS;
}
void
CreateImage(ImageType::Pointer image)
{
// Create a black image with a white square
ImageType::IndexType start;
start.Fill(0);
ImageType::SizeType size;
size.Fill(20);
ImageType::RegionType region;
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.Set(255);
}
else
{
imageIterator.Set(0);
}
++imageIterator;
}
itk::WriteImage(image, "image.png");
}
Classes demonstrated#
-
template<typename TInputImage, typename TOutputImage>
class LabelMapToLabelImageFilter : public itk::LabelMapFilter<TInputImage, TOutputImage> Converts a LabelMap to a labeled image.
LabelMapToBinaryImageFilter to a label image.
This implementation was taken from the Insight Journal paper:
https://www.insight-journal.org/browse/publication/176- Author
Gaetan Lehmann. Biologie du Developpement et de la Reproduction, INRA de Jouy-en-Josas, France.
- See
LabelMapToBinaryImageFilter, LabelMapMaskImageFilter
- ITK Sphinx Examples: