Merge LabelMaps#

Synopsis#

Merges several labelmaps.

Results#

Output:

number of objects: 4
number of expected objects: 4

Code#

C++#

#include "itkBinaryImageToShapeLabelMapFilter.h"
#include "itkMergeLabelMapFilter.h"

int
main()
{
  using ImageType = itk::Image<int, 3>;

  // Binary Image to Shape Label Map.
  using BI2SLMType = itk::BinaryImageToShapeLabelMapFilter<ImageType>;
  using LabelMapType = BI2SLMType::OutputImageType;
  using LabelObjectType = BI2SLMType::LabelObjectType;

  using MergerType = itk::MergeLabelMapFilter<LabelMapType>;
  auto merger = MergerType::New();
  merger->SetMethod(itk::MergeLabelMapFilterEnums::ChoiceMethod::PACK);

  int noObjects = 4;

  for (int i = 1; i <= noObjects; ++i)
  {
    auto labelMap = LabelMapType::New();
    auto labelObject = LabelObjectType::New();

    labelObject->SetLabel(1);
    labelMap->AddLabelObject(labelObject);
    labelMap->Update();

    merger->SetInput(i - 1, labelMap);
  }

  merger->Update();
  std::cout << "number of objects:  " << merger->GetOutput()->GetNumberOfLabelObjects() << "\n";
  std::cout << "number of expected objects:  " << noObjects << "\n";

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TImage>
class MergeLabelMapFilter : public itk::InPlaceLabelMapFilter<TImage>

Merges several Label Maps.

This filter takes one or more input Label Map and merges them.

SetMethod() can be used to change how the filter manage the labels from the different label maps. KEEP (0): MergeLabelMapFilter do its best to keep the label unchanged, but if a label is already used in a previous label map, a new label is assigned. AGGREGATE (1): If the same label is found several times in the label maps, the label objects with the same label are merged. PACK (2): MergeLabelMapFilter relabel all the label objects by order of processing. No conflict can occur. STRICT (3): MergeLabelMapFilter keeps the labels unchanged and raises an exception if the same label is found in several images.

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

ShapeLabelObject, RelabelComponentImageFilter

ITK Sphinx Examples:

See itk::MergeLabelMapFilter for additional documentation.