ITK/Examples/WishList/Segmentation/MeanShiftClustering

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search

This example uses the "old" statistics framework. MeanShift is not yet implemented (11/30/2010) in the new framework, so it is currently unavailable.

This example is 140 lines, we should probably get it down to < 50.

MeanShiftClustering.cxx

<source lang="cpp">

  1. include <itkImageFileReader.h>
  2. include <itkImageRegionIterator.h>
  3. include <itkScalarImageToListAdaptor.h>
  4. include <itkKdTree.h>
  5. include <itkKdTreeGenerator.h>
  6. include <itkMeanShiftModeCacheMethod.h>
  7. include <itkHypersphereKernelMeanShiftModeSeeker.h>
  8. include <itkSampleMeanShiftBlurringFilter.h>
  9. include <itkSampleMeanShiftClusteringFilter.h>
  1. include <itkImageFileWriter.h>


int main(int argc, char* argv[] ) {

 if (argc < 2)
   {
     std::cout << "ERROR: data file name argument missing." 
               << std::endl ;
     return EXIT_FAILURE;
   }
 std::string inputFilename = argv[1];
 
 typedef unsigned char PixelType ;
 typedef itk::Image< PixelType, 2 > ImageType ;
 typedef itk::ImageFileReader< ImageType > ImageReaderType ;
 ImageReaderType::Pointer imageReader = ImageReaderType::New() ;
 imageReader->SetFileName(inputFilename.c_str());
 imageReader->Update() ;
 ImageType::Pointer image = imageReader->GetOutput() ;
 
 typedef itk::Statistics::ScalarImageToListAdaptor< ImageType >
   ListSampleType ;
 
 ListSampleType::Pointer listSample = 
   ListSampleType::New() ;
 listSample->SetImage( image ) ;
 typedef itk::Statistics::KdTreeGenerator< ListSampleType > 
   TreeGeneratorType ;
 TreeGeneratorType::Pointer treeGenerator = TreeGeneratorType::New() ;
 treeGenerator->SetSample( listSample ) ;
 treeGenerator->SetBucketSize( 200 ) ;
 treeGenerator->Update() ;
 typedef TreeGeneratorType::KdTreeType TreeType ;
 TreeType::Pointer tree = treeGenerator->GetOutput() ;
 typedef itk::Statistics::HypersphereKernelMeanShiftModeSeeker< 
   TreeType > ModeSeekerType ;
 ModeSeekerType::Pointer modeSeeker = ModeSeekerType::New() ;
 modeSeeker->SetInputSample( tree ) ;

// modeSeeker->SetInputSample( listSample ) ;

 modeSeeker->SetSearchRadius( 4.0 ) ;
 typedef itk::Statistics::MeanShiftModeCacheMethod< TreeType::MeasurementVectorType > CacheMethodType ;
 CacheMethodType::Pointer cacheMethod = CacheMethodType::New() ;
 cacheMethod->SetMaximumEntries(255) ;
 cacheMethod->SetMaximumConsecutiveFailures(100) ;
 cacheMethod->SetHitRatioThreshold( 0.5 ) ;
 modeSeeker->SetCacheMethod( cacheMethod.GetPointer() ) ;
 typedef itk::Statistics::SampleMeanShiftBlurringFilter< TreeType >
   FilterType ;
 FilterType::Pointer filter = FilterType::New() ;
 filter->SetInputSample( tree ) ;
 filter->SetMeanShiftModeSeeker( modeSeeker ) ;
 filter->Update() ;
 std::cout << "Cache statistics: " << std::endl ;
 cacheMethod->Print(std::cout) ;
 typedef ImageType OutputImageType ;
 OutputImageType::Pointer outputImage = OutputImageType::New() ;
 outputImage->SetRegions( image->GetLargestPossibleRegion() ) ;
 outputImage->Allocate() ;
 typedef itk::ImageRegionIterator< OutputImageType > ImageIteratorType ;
 ImageIteratorType io_iter( outputImage,
                            outputImage->GetLargestPossibleRegion() ) ;
 io_iter.GoToBegin() ;
 FilterType::OutputType::Pointer output = filter->GetOutput() ;
 FilterType::OutputType::Iterator fo_iter = output->Begin() ;
 FilterType::OutputType::Iterator fo_end = output->End() ;
 while ( fo_iter != fo_end )
   {
   io_iter.Set( (PixelType) fo_iter.GetMeasurementVector()[0]) ;
   ++fo_iter ;
   ++io_iter ;
   }
 ListSampleType::Pointer listSample2 = ListSampleType::New() ;
 listSample2->SetImage( outputImage ) ;
 TreeGeneratorType::Pointer treeGenerator2 = TreeGeneratorType::New() ;
 treeGenerator2->SetSample( listSample2 ) ;
 treeGenerator2->SetBucketSize( 200 ) ;
 treeGenerator2->Update() ;
 typedef itk::Statistics::SampleMeanShiftClusteringFilter< TreeType >
   ClusteringMethodType ;
 ClusteringMethodType::Pointer clusteringMethod =
   ClusteringMethodType::New() ;
 clusteringMethod->SetInputSample( treeGenerator2->GetOutput() ) ;
 clusteringMethod->SetThreshold( 0.5 ) ;
 clusteringMethod->SetMinimumClusterSize( 16 ) ;
 clusteringMethod->DebugOn() ;
 clusteringMethod->Update() ;
 // save clustered image
 OutputImageType::Pointer clusterMap = OutputImageType::New() ;
 clusterMap->SetRegions( image->GetLargestPossibleRegion() ) ;
 clusterMap->Allocate() ;
 
 ImageIteratorType m_iter( clusterMap, 
                           clusterMap->GetLargestPossibleRegion() ) ;
 m_iter.GoToBegin() ;
 
 ClusteringMethodType::ClusterLabelsType clusterLabels = 
   clusteringMethod->GetOutput() ;
 
 ClusteringMethodType::ClusterLabelsType::iterator co_iter = 
   clusterLabels.begin() ;
 
 while ( co_iter != clusterLabels.end() )
   {
   m_iter.Set( (PixelType) *co_iter ) ;
   ++co_iter ;
   ++m_iter ;
   }
 
 typedef itk::ImageFileWriter< OutputImageType > ImageWriterType ;
 ImageWriterType::Pointer map_writer = ImageWriterType::New() ;
 map_writer->SetFileName("clustered_sf4.png") ;
 map_writer->SetInput( clusterMap ) ;
 map_writer->Update() ;


 return EXIT_SUCCESS;

}

</source>

CMakeLists.txt

<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)

project(MeanShiftClustering)

find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)

 find_package(VTK REQUIRED)
 include(${VTK_USE_FILE})

endif()

add_executable(MeanShiftClustering MACOSX_BUNDLE MeanShiftClustering.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(MeanShiftClustering ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(MeanShiftClustering ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build MeanShiftClustering

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

cd MeanShiftClustering/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:

./MeanShiftClustering

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.