ITK/Examples/Statistics/KdTree: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(main needs parameters)
(Deprecated content that is moved to sphinx)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
==Description==
{{warning|1=The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/*  pages likely require ITK version 4.13 or earlier releases.   In many cases, the examples on this page no longer conform to the best practices for modern ITK versions.}}
Cluster a collection of measurements using the KMeans algorithm. The name "KdTreeBased" indicates that this is an efficient implementation which uses a KdTree.
 
==KdTree.cxx==
<source lang="cpp">
#include "itkVector.h"
#include "itkListSample.h"
#include "itkWeightedCentroidKdTreeGenerator.h"
#include "itkEuclideanDistanceMetric.h"
 
int main(int, char *[])
{
  typedef itk::Vector< float, 2 > MeasurementVectorType;
 
  typedef itk::Statistics::ListSample< MeasurementVectorType > SampleType;
  SampleType::Pointer sample = SampleType::New();
  sample->SetMeasurementVectorSize( 2 );
 
  MeasurementVectorType mv;
  for (unsigned int i = 0 ; i < 100 ; ++i )
    {
    mv[0] = static_cast<float>(i);
    mv[1] = static_cast<float>(i);
    sample->PushBack( mv );
    }
 
  typedef itk::Statistics::KdTreeGenerator< SampleType > TreeGeneratorType;
  TreeGeneratorType::Pointer treeGenerator = TreeGeneratorType::New();
  treeGenerator->SetSample( sample );
  treeGenerator->SetBucketSize( 16 );
  treeGenerator->Update();
 
  typedef TreeGeneratorType::KdTreeType TreeType;
  typedef TreeType::NearestNeighbors NeighborsType;
  typedef TreeType::KdTreeNodeType NodeType;
 
  TreeType::Pointer tree = treeGenerator->GetOutput();
 
  MeasurementVectorType queryPoint;
  queryPoint[0] = 10.0;
  queryPoint[1] = 7.0;
 
  // K-Neighbor search
  std::cout << "K-Neighbor search:" << std::endl;
  unsigned int numberOfNeighbors = 3;
  TreeType::InstanceIdentifierVectorType neighbors;
  tree->Search( queryPoint, numberOfNeighbors, neighbors ) ;
 
  for ( unsigned int i = 0 ; i < neighbors.size() ; ++i )
    {
    std::cout << tree->GetMeasurementVector( neighbors[i] ) << std::endl;
    }
   
  // Radius search
  std::cout << "Radius search:" << std::endl;
  double radius = 4.0;
   tree->Search( queryPoint, radius, neighbors ) ;
  std::cout << "There are " << neighbors.size() << " neighbors." << std::endl;
  for ( unsigned int i = 0 ; i < neighbors.size() ; ++i )
    {
    std::cout << tree->GetMeasurementVector( neighbors[i] ) << std::endl;
    }
 
  return EXIT_SUCCESS;
}
</source>
 
 
{{ITKVTKCMakeLists|KdTreeGenerator|}}

Latest revision as of 22:00, 6 June 2019

Warning: The media wiki content on this page is no longer maintained. The examples presented on the https://itk.org/Wiki/* pages likely require ITK version 4.13 or earlier releases. In many cases, the examples on this page no longer conform to the best practices for modern ITK versions.