ITK/Examples/Statistics/KdTreeBasedKmeansEstimator3D: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
mNo edit summary
 
(9 intermediate revisions by 4 users not shown)
Line 6: Line 6:


==Output==
==Output==
The input is shown on the left. It consists of a single collection of 3D points that lend themselves to easy clustering into 2 clusters. The output clusters are shown on the right. Points belonging to the same <span class="plainlinks">[http://www.mycaal.com/ <span style="color:black;font-weight:normal; text-decoration:none!important; background:none!important; text-decoration:none;">loan modification</span>] cluster as shown in the same color.
The input is shown on the left. It consists of a single collection of 3D points that lend themselves to easy clustering into 2 clusters. The output clusters are shown on the right. Points belonging to the same cluster as shown in the same color.


==KdTreeBasedKMeansClustering_3D.cxx==
==KdTreeBasedKMeansClustering3D.cxx==
<source lang="cpp">
<source lang="cpp">
#include "itkDecisionRule.h"
#include "itkDecisionRule.h"
Line 25: Line 25:
#include "itkSampleClassifierFilter.h"
#include "itkSampleClassifierFilter.h"
#include "itkNormalVariateGenerator.h"
#include "itkNormalVariateGenerator.h"
 
#include "vtkVersion.h"
#include "vtkActor.h"
#include "vtkActor.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkInteractorStyleTrackballCamera.h"
Line 36: Line 37:
#include "vtkSmartPointer.h"
#include "vtkSmartPointer.h"
#include "vtkVertexGlyphFilter.h"
#include "vtkVertexGlyphFilter.h"
 
int main(int, char *[])
int main(int, char *[])
{
{
Line 42: Line 43:
   typedef itk::Statistics::ListSample< MeasurementVectorType > SampleType;
   typedef itk::Statistics::ListSample< MeasurementVectorType > SampleType;
   SampleType::Pointer sample = SampleType::New();
   SampleType::Pointer sample = SampleType::New();
 
   typedef itk::Statistics::NormalVariateGenerator NormalGeneratorType;
   typedef itk::Statistics::NormalVariateGenerator NormalGeneratorType;
   NormalGeneratorType::Pointer normalGenerator = NormalGeneratorType::New();
   NormalGeneratorType::Pointer normalGenerator = NormalGeneratorType::New();
 
   normalGenerator->Initialize( 101 );
   normalGenerator->Initialize( 101 );
 
   MeasurementVectorType mv;
   MeasurementVectorType mv;
   double mean = 100;
   double mean = 100;
Line 58: Line 59:
     sample->PushBack( mv );
     sample->PushBack( mv );
     }
     }
 
   normalGenerator->Initialize( 3024 );
   normalGenerator->Initialize( 3024 );
   mean = 200;
   mean = 200;
Line 69: Line 70:
     sample->PushBack( mv );
     sample->PushBack( mv );
     }
     }
 
   typedef itk::Statistics::WeightedCentroidKdTreeGenerator< SampleType >
   typedef itk::Statistics::WeightedCentroidKdTreeGenerator< SampleType >
     TreeGeneratorType;
     TreeGeneratorType;
   TreeGeneratorType::Pointer treeGenerator = TreeGeneratorType::New();
   TreeGeneratorType::Pointer treeGenerator = TreeGeneratorType::New();
 
   treeGenerator->SetSample( sample );
   treeGenerator->SetSample( sample );
   treeGenerator->SetBucketSize( 16 );
   treeGenerator->SetBucketSize( 16 );
   treeGenerator->Update();
   treeGenerator->Update();
 
   typedef TreeGeneratorType::KdTreeType TreeType;
   typedef TreeGeneratorType::KdTreeType TreeType;
   typedef itk::Statistics::KdTreeBasedKmeansEstimator<TreeType> EstimatorType;
   typedef itk::Statistics::KdTreeBasedKmeansEstimator<TreeType> EstimatorType;
   EstimatorType::Pointer estimator = EstimatorType::New();
   EstimatorType::Pointer estimator = EstimatorType::New();
 
   EstimatorType::ParametersType initialMeans(6);
   EstimatorType::ParametersType initialMeans(6);
   initialMeans[0] = 0.0; // Cluster 1, mean[0]
   initialMeans[0] = 0.0; // Cluster 1, mean[0]
Line 89: Line 90:
   initialMeans[4] = 5.0; // Cluster 2, mean[1]
   initialMeans[4] = 5.0; // Cluster 2, mean[1]
   initialMeans[5] = 5.0; // Cluster 2, mean[2]
   initialMeans[5] = 5.0; // Cluster 2, mean[2]
 
   estimator->SetParameters( initialMeans );
   estimator->SetParameters( initialMeans );
   estimator->SetKdTree( treeGenerator->GetOutput() );
   estimator->SetKdTree( treeGenerator->GetOutput() );
Line 95: Line 96:
   estimator->SetCentroidPositionChangesThreshold(0.0);
   estimator->SetCentroidPositionChangesThreshold(0.0);
   estimator->StartOptimization();
   estimator->StartOptimization();
 
   EstimatorType::ParametersType estimatedMeans = estimator->GetParameters();
   EstimatorType::ParametersType estimatedMeans = estimator->GetParameters();
 
   for ( unsigned int i = 0 ; i < 6 ; i+=2 )
   for ( unsigned int i = 0 ; i < 6 ; i+=2 )
     {
     {
Line 103: Line 104:
     std::cout << "    estimated mean : " << estimatedMeans[i] << " , " << estimatedMeans[i+1] << std::endl;
     std::cout << "    estimated mean : " << estimatedMeans[i] << " , " << estimatedMeans[i+1] << std::endl;
     }
     }
 
   typedef itk::Statistics::DistanceToCentroidMembershipFunction< MeasurementVectorType >
   typedef itk::Statistics::DistanceToCentroidMembershipFunction< MeasurementVectorType >
     MembershipFunctionType;
     MembershipFunctionType;
   typedef MembershipFunctionType::Pointer                      MembershipFunctionPointer;
   typedef MembershipFunctionType::Pointer                      MembershipFunctionPointer;
 
#if ITK_VERSION_MAJOR < 4
#if ITK_VERSION_MAJOR < 4
   typedef itk::Statistics::MinimumDecisionRule2 DecisionRuleType;
   typedef itk::Statistics::MinimumDecisionRule2 DecisionRuleType;
Line 114: Line 115:
#endif
#endif
   DecisionRuleType::Pointer decisionRule = DecisionRuleType::New();
   DecisionRuleType::Pointer decisionRule = DecisionRuleType::New();
 
   typedef itk::Statistics::SampleClassifierFilter< SampleType > ClassifierType;
   typedef itk::Statistics::SampleClassifierFilter< SampleType > ClassifierType;
   ClassifierType::Pointer classifier = ClassifierType::New();
   ClassifierType::Pointer classifier = ClassifierType::New();
 
   classifier->SetDecisionRule(decisionRule);
   classifier->SetDecisionRule(decisionRule);
   classifier->SetInput( sample );
   classifier->SetInput( sample );
   classifier->SetNumberOfClasses( 2 );
   classifier->SetNumberOfClasses( 2 );
 
   typedef ClassifierType::ClassLabelVectorObjectType              ClassLabelVectorObjectType;
   typedef ClassifierType::ClassLabelVectorObjectType              ClassLabelVectorObjectType;
   typedef ClassifierType::ClassLabelVectorType                    ClassLabelVectorType;
   typedef ClassifierType::ClassLabelVectorType                    ClassLabelVectorType;
   typedef ClassifierType::MembershipFunctionVectorObjectType      MembershipFunctionVectorObjectType;
   typedef ClassifierType::MembershipFunctionVectorObjectType      MembershipFunctionVectorObjectType;
   typedef ClassifierType::MembershipFunctionVectorType            MembershipFunctionVectorType;
   typedef ClassifierType::MembershipFunctionVectorType            MembershipFunctionVectorType;
 
   ClassLabelVectorObjectType::Pointer  classLabelsObject = ClassLabelVectorObjectType::New();
   ClassLabelVectorObjectType::Pointer  classLabelsObject = ClassLabelVectorObjectType::New();
   classifier->SetClassLabels( classLabelsObject );
   classifier->SetClassLabels( classLabelsObject );
 
   ClassLabelVectorType &  classLabelsVector = classLabelsObject->Get();
   ClassLabelVectorType &  classLabelsVector = classLabelsObject->Get();
   classLabelsVector.push_back( 100 );
   classLabelsVector.push_back( 100 );
   classLabelsVector.push_back( 200 );
   classLabelsVector.push_back( 200 );
 
 
   MembershipFunctionVectorObjectType::Pointer membershipFunctionsObject =
   MembershipFunctionVectorObjectType::Pointer membershipFunctionsObject =
     MembershipFunctionVectorObjectType::New();
     MembershipFunctionVectorObjectType::New();
   classifier->SetMembershipFunctions( membershipFunctionsObject );
   classifier->SetMembershipFunctions( membershipFunctionsObject );
 
   MembershipFunctionVectorType &  membershipFunctionsVector = membershipFunctionsObject->Get();
   MembershipFunctionVectorType &  membershipFunctionsVector = membershipFunctionsObject->Get();
 
   MembershipFunctionType::CentroidType origin( sample->GetMeasurementVectorSize() );
   MembershipFunctionType::CentroidType origin( sample->GetMeasurementVectorSize() );
   int index = 0;
   int index = 0;
Line 153: Line 154:
     membershipFunctionsVector.push_back( membershipFunction.GetPointer() );
     membershipFunctionsVector.push_back( membershipFunction.GetPointer() );
     }
     }
 
   classifier->Update();
   classifier->Update();
 
   const ClassifierType::MembershipSampleType* membershipSample = classifier->GetOutput();
   const ClassifierType::MembershipSampleType* membershipSample = classifier->GetOutput();
   ClassifierType::MembershipSampleType::ConstIterator iter = membershipSample->Begin();
   ClassifierType::MembershipSampleType::ConstIterator iter = membershipSample->Begin();
 
   while ( iter != membershipSample->End() )
   while ( iter != membershipSample->End() )
     {
     {
Line 166: Line 167:
     ++iter;
     ++iter;
     }
     }
 
   // Visualize
   // Visualize
   vtkSmartPointer<vtkPoints> points1 =
   vtkSmartPointer<vtkPoints> points1 =
Line 172: Line 173:
   vtkSmartPointer<vtkPoints> points2 =
   vtkSmartPointer<vtkPoints> points2 =
     vtkSmartPointer<vtkPoints>::New();
     vtkSmartPointer<vtkPoints>::New();
 
   iter = membershipSample->Begin();
   iter = membershipSample->Begin();
   while ( iter != membershipSample->End() )
   while ( iter != membershipSample->End() )
Line 192: Line 193:
     ++iter;
     ++iter;
     }
     }
 
   vtkSmartPointer<vtkPolyData> polyData1 =
   vtkSmartPointer<vtkPolyData> polyData1 =
     vtkSmartPointer<vtkPolyData>::New();
     vtkSmartPointer<vtkPolyData>::New();
Line 198: Line 199:
   vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter1 =
   vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter1 =
     vtkSmartPointer<vtkVertexGlyphFilter>::New();
     vtkSmartPointer<vtkVertexGlyphFilter>::New();
#if VTK_MAJOR_VERSION <= 5
   glyphFilter1->SetInputConnection(polyData1->GetProducerPort());
   glyphFilter1->SetInputConnection(polyData1->GetProducerPort());
#else
  glyphFilter1->SetInputData(polyData1);
#endif
   glyphFilter1->Update();
   glyphFilter1->Update();
   vtkSmartPointer<vtkPolyDataMapper> mapper1 =
   vtkSmartPointer<vtkPolyDataMapper> mapper1 =
Line 208: Line 213:
   actor1->GetProperty()->SetPointSize(3);
   actor1->GetProperty()->SetPointSize(3);
   actor1->SetMapper(mapper1);
   actor1->SetMapper(mapper1);
 
   vtkSmartPointer<vtkPolyData> polyData2 =
   vtkSmartPointer<vtkPolyData> polyData2 =
     vtkSmartPointer<vtkPolyData>::New();
     vtkSmartPointer<vtkPolyData>::New();
Line 214: Line 219:
   vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter2 =
   vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter2 =
     vtkSmartPointer<vtkVertexGlyphFilter>::New();
     vtkSmartPointer<vtkVertexGlyphFilter>::New();
#if VTK_MAJOR_VERSION <= 5
   glyphFilter2->SetInputConnection(polyData2->GetProducerPort());
   glyphFilter2->SetInputConnection(polyData2->GetProducerPort());
#else
  glyphFilter2->SetInputData(polyData2);
#endif
   glyphFilter2->Update();
   glyphFilter2->Update();
   vtkSmartPointer<vtkPolyDataMapper> mapper2 =
   vtkSmartPointer<vtkPolyDataMapper> mapper2 =
Line 224: Line 233:
   actor2->GetProperty()->SetPointSize(3);
   actor2->GetProperty()->SetPointSize(3);
   actor2->SetMapper(mapper2);
   actor2->SetMapper(mapper2);
 
   vtkSmartPointer<vtkRenderWindow> renderWindow =
   vtkSmartPointer<vtkRenderWindow> renderWindow =
     vtkSmartPointer<vtkRenderWindow>::New();
     vtkSmartPointer<vtkRenderWindow>::New();
   renderWindow->SetSize(300,300);
   renderWindow->SetSize(300,300);
 
   vtkSmartPointer<vtkRenderer> renderer =
   vtkSmartPointer<vtkRenderer> renderer =
     vtkSmartPointer<vtkRenderer>::New();
     vtkSmartPointer<vtkRenderer>::New();
   renderWindow->AddRenderer(renderer);
   renderWindow->AddRenderer(renderer);
 
   renderer->AddActor(actor1);
   renderer->AddActor(actor1);
   renderer->AddActor(actor2);
   renderer->AddActor(actor2);
   renderer->ResetCamera();
   renderer->ResetCamera();
   renderer->Render();
    
 
   vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
   vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
     vtkSmartPointer<vtkRenderWindowInteractor>::New();
     vtkSmartPointer<vtkRenderWindowInteractor>::New();
   vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
   vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
     vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
     vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
 
   renderWindowInteractor->SetInteractorStyle(style);
   renderWindowInteractor->SetInteractorStyle(style);
   renderWindowInteractor->SetRenderWindow(renderWindow);
   renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindowInteractor->Initialize();
   renderWindowInteractor->Start();
   renderWindowInteractor->Start();
 
   return EXIT_SUCCESS;
   return EXIT_SUCCESS;
}
}
Line 255: Line 260:




{{ITKVTKCMakeLists|KdTreeBasedKMeansClustering_3D|}}
{{ITKVTKCMakeLists|{{SUBPAGENAME}}|}}

Latest revision as of 15:46, 24 August 2013

ITK Examples Baseline Statistics TestKdTreeBasedKMeansClustering 3D.png

Description

Cluster a collection of measurements using the KMeans algorithm. The name "KdTreeBased" indicates that this is an efficient implementation which uses a KdTree.

ITK Classes Demonstrated

Output

The input is shown on the left. It consists of a single collection of 3D points that lend themselves to easy clustering into 2 clusters. The output clusters are shown on the right. Points belonging to the same cluster as shown in the same color.

KdTreeBasedKMeansClustering3D.cxx

<source lang="cpp">

  1. include "itkDecisionRule.h"
  2. include "itkVector.h"
  3. include "itkListSample.h"
  4. include "itkKdTree.h"
  5. include "itkWeightedCentroidKdTreeGenerator.h"
  6. include "itkKdTreeBasedKmeansEstimator.h"
  7. if ITK_VERSION_MAJOR < 4
  8. include "itkMinimumDecisionRule2.h"
  9. else
  10. include "itkMinimumDecisionRule.h"
  11. endif
  12. include "itkEuclideanDistanceMetric.h"
  13. include "itkDistanceToCentroidMembershipFunction.h"
  14. include "itkSampleClassifierFilter.h"
  15. include "itkNormalVariateGenerator.h"
  1. include "vtkVersion.h"
  2. include "vtkActor.h"
  3. include "vtkInteractorStyleTrackballCamera.h"
  4. include "vtkPolyData.h"
  5. include "vtkPolyDataMapper.h"
  6. include "vtkProperty.h"
  7. include "vtkRenderer.h"
  8. include "vtkRenderWindow.h"
  9. include "vtkRenderWindowInteractor.h"
  10. include "vtkSmartPointer.h"
  11. include "vtkVertexGlyphFilter.h"

int main(int, char *[]) {

 typedef itk::Vector< double, 3 > MeasurementVectorType;
 typedef itk::Statistics::ListSample< MeasurementVectorType > SampleType;
 SampleType::Pointer sample = SampleType::New();

 typedef itk::Statistics::NormalVariateGenerator NormalGeneratorType;
 NormalGeneratorType::Pointer normalGenerator = NormalGeneratorType::New();

 normalGenerator->Initialize( 101 );

 MeasurementVectorType mv;
 double mean = 100;
 double standardDeviation = 30;
 for ( unsigned int i = 0 ; i < 100 ; ++i )
   {
   mv[0] = ( normalGenerator->GetVariate() * standardDeviation ) + mean;
   mv[1] = ( normalGenerator->GetVariate() * standardDeviation ) + mean;
   mv[2] = ( normalGenerator->GetVariate() * standardDeviation ) + mean;
   sample->PushBack( mv );
   }

 normalGenerator->Initialize( 3024 );
 mean = 200;
 standardDeviation = 30;
 for ( unsigned int i = 0 ; i < 100 ; ++i )
   {
   mv[0] = ( normalGenerator->GetVariate() * standardDeviation ) + mean;
   mv[1] = ( normalGenerator->GetVariate() * standardDeviation ) + mean;
   mv[2] = ( normalGenerator->GetVariate() * standardDeviation ) + mean;
   sample->PushBack( mv );
   }

 typedef itk::Statistics::WeightedCentroidKdTreeGenerator< SampleType >
   TreeGeneratorType;
 TreeGeneratorType::Pointer treeGenerator = TreeGeneratorType::New();

 treeGenerator->SetSample( sample );
 treeGenerator->SetBucketSize( 16 );
 treeGenerator->Update();

 typedef TreeGeneratorType::KdTreeType TreeType;
 typedef itk::Statistics::KdTreeBasedKmeansEstimator<TreeType> EstimatorType;
 EstimatorType::Pointer estimator = EstimatorType::New();

 EstimatorType::ParametersType initialMeans(6);
 initialMeans[0] = 0.0; // Cluster 1, mean[0]
 initialMeans[1] = 0.0; // Cluster 1, mean[1]
 initialMeans[2] = 0.0; // Cluster 1, mean[2]
 initialMeans[3] = 5.0; // Cluster 2, mean[0]
 initialMeans[4] = 5.0; // Cluster 2, mean[1]
 initialMeans[5] = 5.0; // Cluster 2, mean[2]

 estimator->SetParameters( initialMeans );
 estimator->SetKdTree( treeGenerator->GetOutput() );
 estimator->SetMaximumIteration( 200 );
 estimator->SetCentroidPositionChangesThreshold(0.0);
 estimator->StartOptimization();

 EstimatorType::ParametersType estimatedMeans = estimator->GetParameters();

 for ( unsigned int i = 0 ; i < 6 ; i+=2 )
   {
   std::cout << "cluster[" << i << "] " << std::endl;
   std::cout << "    estimated mean : " << estimatedMeans[i] << " , " << estimatedMeans[i+1] << std::endl;
   }

 typedef itk::Statistics::DistanceToCentroidMembershipFunction< MeasurementVectorType >
   MembershipFunctionType;
 typedef MembershipFunctionType::Pointer                      MembershipFunctionPointer;

  1. if ITK_VERSION_MAJOR < 4
 typedef itk::Statistics::MinimumDecisionRule2 DecisionRuleType;
  1. else
 typedef itk::Statistics::MinimumDecisionRule DecisionRuleType;
  1. endif
 DecisionRuleType::Pointer decisionRule = DecisionRuleType::New();

 typedef itk::Statistics::SampleClassifierFilter< SampleType > ClassifierType;
 ClassifierType::Pointer classifier = ClassifierType::New();

 classifier->SetDecisionRule(decisionRule);
 classifier->SetInput( sample );
 classifier->SetNumberOfClasses( 2 );

 typedef ClassifierType::ClassLabelVectorObjectType               ClassLabelVectorObjectType;
 typedef ClassifierType::ClassLabelVectorType                     ClassLabelVectorType;
 typedef ClassifierType::MembershipFunctionVectorObjectType       MembershipFunctionVectorObjectType;
 typedef ClassifierType::MembershipFunctionVectorType             MembershipFunctionVectorType;

 ClassLabelVectorObjectType::Pointer  classLabelsObject = ClassLabelVectorObjectType::New();
 classifier->SetClassLabels( classLabelsObject );

 ClassLabelVectorType &  classLabelsVector = classLabelsObject->Get();
 classLabelsVector.push_back( 100 );
 classLabelsVector.push_back( 200 );


 MembershipFunctionVectorObjectType::Pointer membershipFunctionsObject =
   MembershipFunctionVectorObjectType::New();
 classifier->SetMembershipFunctions( membershipFunctionsObject );

 MembershipFunctionVectorType &  membershipFunctionsVector = membershipFunctionsObject->Get();

 MembershipFunctionType::CentroidType origin( sample->GetMeasurementVectorSize() );
 int index = 0;
 for ( unsigned int i = 0 ; i < 2 ; i++ )
   {
   MembershipFunctionPointer membershipFunction = MembershipFunctionType::New();
   for ( unsigned int j = 0 ; j < sample->GetMeasurementVectorSize(); j++ )
     {
     origin[j] = estimatedMeans[index++];
     }
   membershipFunction->SetCentroid( origin );
   membershipFunctionsVector.push_back( membershipFunction.GetPointer() );
   }

 classifier->Update();

 const ClassifierType::MembershipSampleType* membershipSample = classifier->GetOutput();
 ClassifierType::MembershipSampleType::ConstIterator iter = membershipSample->Begin();

 while ( iter != membershipSample->End() )
   {
   std::cout << "measurement vector = " << iter.GetMeasurementVector()
             << "class label = " << iter.GetClassLabel()
             << std::endl;
   ++iter;
   }

 // Visualize
 vtkSmartPointer<vtkPoints> points1 =
   vtkSmartPointer<vtkPoints>::New();
 vtkSmartPointer<vtkPoints> points2 =
   vtkSmartPointer<vtkPoints>::New();

 iter = membershipSample->Begin();
 while ( iter != membershipSample->End() )
   {
   if(iter.GetClassLabel() == 100)
     {
     points1->InsertNextPoint(
       iter.GetMeasurementVector()[0],
       iter.GetMeasurementVector()[1],
       iter.GetMeasurementVector()[2]);
     }
   else
     {
     points2->InsertNextPoint(
       iter.GetMeasurementVector()[0],
       iter.GetMeasurementVector()[1],
       iter.GetMeasurementVector()[2]);
     }
   ++iter;
   }

 vtkSmartPointer<vtkPolyData> polyData1 =
   vtkSmartPointer<vtkPolyData>::New();
 polyData1->SetPoints(points1);
 vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter1 =
   vtkSmartPointer<vtkVertexGlyphFilter>::New();
  1. if VTK_MAJOR_VERSION <= 5
 glyphFilter1->SetInputConnection(polyData1->GetProducerPort());
  1. else
 glyphFilter1->SetInputData(polyData1);
  1. endif
 glyphFilter1->Update();
 vtkSmartPointer<vtkPolyDataMapper> mapper1 =
   vtkSmartPointer<vtkPolyDataMapper>::New();
 mapper1->SetInputConnection(glyphFilter1->GetOutputPort());
 vtkSmartPointer<vtkActor> actor1 =
   vtkSmartPointer<vtkActor>::New();
 actor1->GetProperty()->SetColor(0,1,0);
 actor1->GetProperty()->SetPointSize(3);
 actor1->SetMapper(mapper1);

 vtkSmartPointer<vtkPolyData> polyData2 =
   vtkSmartPointer<vtkPolyData>::New();
 polyData2->SetPoints(points2);
 vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter2 =
   vtkSmartPointer<vtkVertexGlyphFilter>::New();
  1. if VTK_MAJOR_VERSION <= 5
 glyphFilter2->SetInputConnection(polyData2->GetProducerPort());
  1. else
 glyphFilter2->SetInputData(polyData2);
  1. endif
 glyphFilter2->Update();
 vtkSmartPointer<vtkPolyDataMapper> mapper2 =
   vtkSmartPointer<vtkPolyDataMapper>::New();
 mapper2->SetInputConnection(glyphFilter2->GetOutputPort());
 vtkSmartPointer<vtkActor> actor2 =
   vtkSmartPointer<vtkActor>::New();
 actor2->GetProperty()->SetColor(1,0,0);
 actor2->GetProperty()->SetPointSize(3);
 actor2->SetMapper(mapper2);

 vtkSmartPointer<vtkRenderWindow> renderWindow =
   vtkSmartPointer<vtkRenderWindow>::New();
 renderWindow->SetSize(300,300);

 vtkSmartPointer<vtkRenderer> renderer =
   vtkSmartPointer<vtkRenderer>::New();
 renderWindow->AddRenderer(renderer);

 renderer->AddActor(actor1);
 renderer->AddActor(actor2);
 renderer->ResetCamera();
 
 vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
   vtkSmartPointer<vtkRenderWindowInteractor>::New();
 vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
   vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();

 renderWindowInteractor->SetInteractorStyle(style);
 renderWindowInteractor->SetRenderWindow(renderWindow);
 renderWindowInteractor->Start();

 return EXIT_SUCCESS;

} </source>


CMakeLists.txt

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

project(KdTreeBasedKmeansEstimator3D)

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

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

else()

 find_package(ItkVtkGlue REQUIRED)
 include(${ItkVtkGlue_USE_FILE})
 set(Glue ItkVtkGlue)

endif()

add_executable(KdTreeBasedKmeansEstimator3D MACOSX_BUNDLE KdTreeBasedKmeansEstimator3D.cxx) target_link_libraries(KdTreeBasedKmeansEstimator3D

 ${Glue}  ${VTK_LIBRARIES} ${ITK_LIBRARIES})

</syntaxhighlight>

Download and Build KdTreeBasedKmeansEstimator3D

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

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

./KdTreeBasedKmeansEstimator3D

WINDOWS USERS PLEASE NOTE: Be sure to add the VTK and ITK bin directories to your path. This will resolve the VTK and 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.