Keep Binary Regions That Meet Specific Properties#

Synopsis#

Keep only regions that meet a specified threshold of a specified property.

Results#

input.mhd

input.mhd#

output.mhd

output.mhd#

Code#

C++#

#include "itkBinaryStatisticsOpeningImageFilter.h"
#include "itkImageFileWriter.h"

using ImageType = itk::Image<unsigned char, 2>;
void
CreateImage(ImageType::Pointer image1, ImageType::Pointer image2);

int
main()
{
  auto binaryImage = ImageType::New();
  auto featureImage = ImageType::New();

  CreateImage(binaryImage, featureImage);

  using BinaryOpeningType = itk::BinaryStatisticsOpeningImageFilter<ImageType, ImageType>;
  auto opening = BinaryOpeningType::New();
  opening->SetInput(binaryImage);
  opening->SetFeatureImage(featureImage);
  opening->SetBackgroundValue(0);
  opening->SetForegroundValue(255);
  opening->SetLambda(150);
  opening->SetAttribute(BinaryOpeningType::LabelObjectType::MEAN);
  opening->Update();

  itk::WriteImage(featureImage, "input.mhd");
  itk::WriteImage(opening->GetOutput(), "output.mhd");

  return EXIT_SUCCESS;
}

void
CreateImage(ImageType::Pointer image1, ImageType::Pointer image2)
{
  // Create an image with 2 connected components
  ImageType::RegionType region;
  ImageType::IndexType  start;
  start[0] = 0;
  start[1] = 0;

  ImageType::SizeType size;
  size[0] = 200;
  size[1] = 200;

  region.SetSize(size);
  region.SetIndex(start);

  image1->SetRegions(region);
  image1->Allocate();
  image1->FillBuffer(0);

  image2->SetRegions(region);
  image2->Allocate();
  image2->FillBuffer(0);

  // Make a square
  for (unsigned int r = 20; r < 80; ++r)
  {
    for (unsigned int c = 30; c < 100; ++c)
    {
      ImageType::IndexType pixelIndex;
      pixelIndex[0] = r;
      pixelIndex[1] = c;

      image1->SetPixel(pixelIndex, 255);
      image2->SetPixel(pixelIndex, 100);
    }
  }

  // Make another square
  for (unsigned int r = 100; r < 130; ++r)
  {
    for (unsigned int c = 115; c < 160; ++c)
    {
      ImageType::IndexType pixelIndex;
      pixelIndex[0] = r;
      pixelIndex[1] = c;

      image1->SetPixel(pixelIndex, 255);
      image2->SetPixel(pixelIndex, 200);
    }
  }
}

Classes demonstrated#

template<typename TInputImage, typename TFeatureImage>
class BinaryStatisticsOpeningImageFilter : public itk::ImageToImageFilter<TInputImage, TInputImage>

Remove objects based on the value of their Statistics attribute.

The BinaryStatisticsOpeningImageFilter removes the objects in a binary image with an attribute value smaller or greater than a threshold called Lambda. The attributes are those of the StatisticsLabelObject.

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

StatisticsLabelObject, LabelStatisticsOpeningImageFilter, BinaryStatisticsOpeningImageFilter

ITK Sphinx Examples:

See itk::BinaryStatisticsOpeningImageFilter for additional documentation.