ITK/Examples/ImageProcessing/ScalarConnectedComponentImageFilter

From KitwarePublic
< ITK‎ | Examples
Jump to: navigation, search
ITK Examples Baseline ImageProcessing TestScalarConnectedComponentImageFilter.png

ScalarConnectedComponentImageFilter.cxx

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkLabelToRGBImageFilter.h"
#include "itkRelabelComponentImageFilter.h"
#include "itkLabelStatisticsImageFilter.h"
 
#include "itkScalarConnectedComponentImageFilter.h"
 
#include "itksys/SystemTools.hxx"
#include <sstream>
 
#include "QuickView.h"
 
template <typename TImage>
static void CreateImage(TImage* const image);
 
template<typename TImage, typename TLabelImage>
static void SummarizeLabelStatistics (TImage* image,
                                      TLabelImage* labelImage);
 
int main( int argc, char *argv[])
{
  const unsigned int Dimension = 2;
  typedef short                                  PixelType;
  typedef itk::Image<PixelType, Dimension>       ImageType;
 
  typedef itk::RGBPixel<unsigned char>           RGBPixelType;
  typedef itk::Image<RGBPixelType, Dimension>    RGBImageType;
 
  typedef unsigned int                           LabelPixelType;
  typedef itk::Image<LabelPixelType, Dimension > LabelImageType;
 
  ImageType::Pointer image;
  PixelType distanceThreshold = 4;
  if( argc < 2 )
    {
    image = ImageType::New();
    CreateImage(image.GetPointer());
    }
  else
    {
    typedef itk::ImageFileReader<ImageType> ReaderType;
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName(argv[1]);
    reader->Update();
 
    if (argc > 2)
      {
      distanceThreshold = static_cast<PixelType> (atoi(argv[2]));
      }
    image = reader->GetOutput();
    }
 
 
  typedef itk::ScalarConnectedComponentImageFilter <ImageType, LabelImageType >
    ConnectedComponentImageFilterType;
 
  ConnectedComponentImageFilterType::Pointer connected =
    ConnectedComponentImageFilterType::New ();
  connected->SetInput(image);
  connected->SetDistanceThreshold(distanceThreshold);
 
  typedef itk::RelabelComponentImageFilter <LabelImageType, LabelImageType >
    RelabelFilterType;
  RelabelFilterType::Pointer relabel =
    RelabelFilterType::New();
  RelabelFilterType::ObjectSizeType minSize = 20;
  if (argc > 3)
    {
    minSize = atoi(argv[3]);
    }
  relabel->SetInput(connected->GetOutput());
  relabel->SetMinimumObjectSize(minSize);
  relabel->Update();
 
  SummarizeLabelStatistics (image.GetPointer(), relabel->GetOutput());
 
  typedef itk::LabelToRGBImageFilter<LabelImageType, RGBImageType> RGBFilterType;
  RGBFilterType::Pointer rgbFilter =
    RGBFilterType::New();
  rgbFilter->SetInput( relabel->GetOutput() );
 
  QuickView viewer;
  viewer.AddImage(
    image.GetPointer(),true,
    argc > 1 ? itksys::SystemTools::GetFilenameName(argv[1]) : "Generated image");  
 
  std::stringstream desc;
  desc << "Scalar Connected Components:\n# of Objects: "
       << relabel->GetNumberOfObjects()
       << " Threshold: "
       << itk::NumericTraits<ConnectedComponentImageFilterType::OutputPixelType>::PrintType(connected->GetDistanceThreshold())
       << " Min Size: " << relabel->GetMinimumObjectSize();
  viewer.AddRGBImage(
    rgbFilter->GetOutput(),
    true,
    desc.str());  
 
  viewer.Visualize();
 
  return EXIT_SUCCESS;
}
 
template <typename TImage>
void CreateImage(TImage* const image)
{
  // Create an image with 2 connected components
  typename TImage::IndexType start = {{0,0}};
  start[0] = 0;
  start[1] = 0;
 
  typename TImage::SizeType size;
  unsigned int NumRows = 200;
  unsigned int NumCols = 300;
  size[0] = NumRows;
  size[1] = NumCols;
 
  typename TImage::RegionType region(start, size);
 
  image->SetRegions(region);
  image->Allocate();
 
  // Make a square
  for(typename TImage::IndexValueType r = 20; r < 80; r++)
    {
    for(typename TImage::IndexValueType c = 30; c < 100; c++)
      {
      typename TImage::IndexType pixelIndex = {{r,c}};
 
      image->SetPixel(pixelIndex, 255);
      }
    }
 
  // Make another square
  for(typename TImage::IndexValueType r = 100; r < 130; r++)
    {
    for(typename TImage::IndexValueType c = 115; c < 160; c++)
      {
      typename TImage::IndexType pixelIndex = {{r,c}};
 
      image->SetPixel(pixelIndex, 255);
      }
    }
}
 
template<typename TImage, typename TLabelImage>
void SummarizeLabelStatistics (TImage* image,
                               TLabelImage* labelImage)
{
  typedef itk::LabelStatisticsImageFilter< TImage, TLabelImage >
    LabelStatisticsImageFilterType;
  typename LabelStatisticsImageFilterType::Pointer labelStatisticsImageFilter =
    LabelStatisticsImageFilterType::New();
  labelStatisticsImageFilter->SetLabelInput( labelImage );
  labelStatisticsImageFilter->SetInput(image);
  labelStatisticsImageFilter->UseHistogramsOn(); // needed to compute median
  labelStatisticsImageFilter->Update();
 
  std::cout << "Number of labels: "
            << labelStatisticsImageFilter->GetNumberOfLabels() << std::endl;
  std::cout << std::endl;
 
  typedef typename LabelStatisticsImageFilterType::ValidLabelValuesContainerType ValidLabelValuesType;
  typedef typename LabelStatisticsImageFilterType::LabelPixelType LabelPixelType;
 
  for(typename ValidLabelValuesType::const_iterator vIt = labelStatisticsImageFilter->GetValidLabelValues().begin();
      vIt != labelStatisticsImageFilter->GetValidLabelValues().end();
      ++vIt)
    {
    if ( labelStatisticsImageFilter->HasLabel(*vIt) )
      {
      LabelPixelType labelValue = *vIt;
      std::cout << "Label: " << *vIt << std::endl;
      std::cout << "\tmin: "
                << labelStatisticsImageFilter->GetMinimum( labelValue )
                << std::endl;
      std::cout << "\tmax: "
                << labelStatisticsImageFilter->GetMaximum( labelValue )
                << std::endl;
      std::cout << "\tmedian: "
                << labelStatisticsImageFilter->GetMedian( labelValue )
                << std::endl;
      std::cout << "\tmean: "
                << labelStatisticsImageFilter->GetMean( labelValue )
                << std::endl;
      std::cout << "\tsigma: "
                << labelStatisticsImageFilter->GetSigma( labelValue )
                << std::endl;
      std::cout << "\tvariance: "
                << labelStatisticsImageFilter->GetVariance( labelValue )
                << std::endl;
      std::cout << "\tsum: "
                << labelStatisticsImageFilter->GetSum( labelValue )
                << std::endl;
      std::cout << "\tcount: "
                << labelStatisticsImageFilter->GetCount( labelValue )
                << std::endl;
      std::cout << "\tregion: "
                << labelStatisticsImageFilter->GetRegion( labelValue )
                << std::endl;
      }
    }
 
  }

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
project(ScalarConnectedComponentImageFilter)
 
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
if (ITKVtkGlue_LOADED)
  find_package(VTK REQUIRED)
  include(${VTK_USE_FILE})
endif()
 
add_executable(ScalarConnectedComponentImageFilter MACOSX_BUNDLE ScalarConnectedComponentImageFilter.cxx)
 
if( "${ITK_VERSION_MAJOR}" LESS 4 )
  target_link_libraries(ScalarConnectedComponentImageFilter ITKReview ${ITK_LIBRARIES})
else( "${ITK_VERSION_MAJOR}" LESS 4 )
  target_link_libraries(ScalarConnectedComponentImageFilter ${ITK_LIBRARIES})
endif( "${ITK_VERSION_MAJOR}" LESS 4 )

Download and Build ScalarConnectedComponentImageFilter

Click here to download ScalarConnectedComponentImageFilter and its CMakeLists.txt file. Once the tarball ScalarConnectedComponentImageFilter.tar has been downloaded and extracted,

cd ScalarConnectedComponentImageFilter/build
  • If ITK is installed:
cmake ..
  • If ITK is not installed but compiled on your system, you will need to specify the path to your ITK build:
cmake -DITK_DIR:PATH=/home/me/itk_build ..

Build the project:

make

and run it:

./ScalarConnectedComponentImageFilter

WINDOWS USERS PLEASE NOTE: Be sure to add the ITK bin directory to your path. This will resolve the ITK dll's at run time.

Building All of the Examples

Many of the examples in the ITK Wiki Examples Collection require VTK. You can build all of the the examples by following these instructions. If you are a new VTK user, you may want to try the Superbuild which will build a proper ITK and VTK.

ItkVtkGlue

ITK >= 4

For examples that use QuickView (which depends on VTK), you must have built ITK with Module_ITKVtkGlue=ON.

ITK < 4

Some of the ITK Examples require VTK to display the images. If you download the entire ITK Wiki Examples Collection, the ItkVtkGlue directory will be included and configured. If you wish to just build a few examples, then you will need to download ItkVtkGlue and build it. When you run cmake it will ask you to specify the location of the ItkVtkGlue binary directory.