ITK/Examples/Statistics/KdTreeBasedKmeansEstimator: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
(Make the code work)
 
Line 41: Line 41:
   sample->PushBack(mv);
   sample->PushBack(mv);
    
    
  // If you uncomment these, it does not work (Exception: vectors are not the same size (3 and 0)
   mv[0] = 195.88; mv[1] = 191.88; mv[2] = 188.88;
//   mv[0] = 195.88; mv[1] = 191.88; mv[2] = 188.88;
   sample->PushBack(mv);
//   sample->PushBack(mv);
   mv[0] = 207.36; mv[1] = 206.36; mv[2] = 202.36;
//   mv[0] = 207.36; mv[1] = 206.36; mv[2] = 202.36;
   sample->PushBack(mv);
//   sample->PushBack(mv);
   mv[0] = 176.672; mv[1] = 176.008; mv[2] = 171.336;
//   mv[0] = 176.672; mv[1] = 176.008; mv[2] = 171.336;
   sample->PushBack(mv);
//   sample->PushBack(mv);
   mv[0] = 180.176; mv[1] = 181.176; mv[2] = 174.088;
//   mv[0] = 180.176; mv[1] = 181.176; mv[2] = 174.088;
   sample->PushBack(mv);
//   sample->PushBack(mv);
   mv[0] = 171.304; mv[1] = 172.304; mv[2] = 166.304;
//   mv[0] = 171.304; mv[1] = 172.304; mv[2] = 166.304;
   sample->PushBack(mv);
//   sample->PushBack(mv);
   mv[0] = 161; mv[1] = 162; mv[2] = 156.032;
//   mv[0] = 161; mv[1] = 162; mv[2] = 156.032;
   sample->PushBack(mv);
//   sample->PushBack(mv);
   mv[0] = 166.96; mv[1] = 167.96; mv[2] = 162.96;
//   mv[0] = 166.96; mv[1] = 167.96; mv[2] = 162.96;
   sample->PushBack(mv);
//   sample->PushBack(mv);
   mv[0] = 169; mv[1] = 170; mv[2] = 164.392;
//   mv[0] = 169; mv[1] = 170; mv[2] = 164.392;
   sample->PushBack(mv);
//   sample->PushBack(mv);
   mv[0] = 165.104; mv[1] = 166.104; mv[2] = 160.104;
//   mv[0] = 165.104; mv[1] = 166.104; mv[2] = 160.104;
   sample->PushBack(mv);
//   sample->PushBack(mv);
   mv[0] = 170.264; mv[1] = 171.264; mv[2] = 164.896;
//   mv[0] = 170.264; mv[1] = 171.264; mv[2] = 164.896;
   sample->PushBack(mv);
//   sample->PushBack(mv);
   mv[0] = 165.472; mv[1] = 165.944; mv[2] = 158.888;
//   mv[0] = 165.472; mv[1] = 165.944; mv[2] = 158.888;
   sample->PushBack(mv);
//   sample->PushBack(mv);


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


Line 79: Line 77:


   EstimatorType::ParametersType initialMeans(2*3);
   EstimatorType::ParametersType initialMeans(2*3);
   initialMeans.Fill(0.0f);
   initialMeans.Fill(165.0f);
  initialMeans[3] = 200;
  initialMeans[4] = 200;
  initialMeans[5] = 200;


   estimator->SetParameters( initialMeans );
   estimator->SetParameters( initialMeans );

Latest revision as of 14:49, 30 August 2017

KdTreeBasedKmeansEstimator.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. include "itkMinimumDecisionRule.h"
  8. include "itkEuclideanDistanceMetric.h"
  9. include "itkDistanceToCentroidMembershipFunction.h"
  10. include "itkSampleClassifierFilter.h"
  11. include "itkNormalVariateGenerator.h"


int main(int, char *[]) {

 typedef itk::Vector< double, 3 > MeasurementVectorType;
 typedef itk::Statistics::ListSample< MeasurementVectorType > SampleType;
 SampleType::Pointer sample = SampleType::New();
 sample->SetMeasurementVectorSize( 3 );
 
 MeasurementVectorType mv;
 mv[0] = 181; mv[1] = 181; mv[2] = 173;
 sample->PushBack(mv);
 mv[0] = 165.376; mv[1] = 166.376; mv[2] = 160.376;
 sample->PushBack(mv);
 mv[0] = 139.64; mv[1] = 140.64; mv[2] = 134.64;
 sample->PushBack(mv);
 mv[0] = 178.688; mv[1] = 181.416; mv[2] = 174.552;
 sample->PushBack(mv);
 mv[0] = 175.328; mv[1] = 176.328; mv[2] = 168.328;
 sample->PushBack(mv);
 mv[0] = 180.52; mv[1] = 181.52; mv[2] = 173.52;
 sample->PushBack(mv);
 mv[0] = 174.456; mv[1] = 176.456; mv[2] = 165.456;
 sample->PushBack(mv);
 mv[0] = 180; mv[1] = 177; mv[2] = 172;
 sample->PushBack(mv);
 mv[0] = 179.696; mv[1] = 176.696; mv[2] = 171.696;
 sample->PushBack(mv);
 
 mv[0] = 195.88; mv[1] = 191.88; mv[2] = 188.88;
 sample->PushBack(mv);
 mv[0] = 207.36; mv[1] = 206.36; mv[2] = 202.36;
 sample->PushBack(mv);
 mv[0] = 176.672; mv[1] = 176.008; mv[2] = 171.336;
 sample->PushBack(mv);
 mv[0] = 180.176; mv[1] = 181.176; mv[2] = 174.088;
 sample->PushBack(mv);
 mv[0] = 171.304; mv[1] = 172.304; mv[2] = 166.304;
 sample->PushBack(mv);
 mv[0] = 161; mv[1] = 162; mv[2] = 156.032;
 sample->PushBack(mv);
 mv[0] = 166.96; mv[1] = 167.96; mv[2] = 162.96;
 sample->PushBack(mv);
 mv[0] = 169; mv[1] = 170; mv[2] = 164.392;
 sample->PushBack(mv);
 mv[0] = 165.104; mv[1] = 166.104; mv[2] = 160.104;
 sample->PushBack(mv);
 mv[0] = 170.264; mv[1] = 171.264; mv[2] = 164.896;
 sample->PushBack(mv);
 mv[0] = 165.472; mv[1] = 165.944; mv[2] = 158.888;
 sample->PushBack(mv);
 typedef itk::Statistics::WeightedCentroidKdTreeGenerator< SampleType > TreeGeneratorType;
 TreeGeneratorType::Pointer treeGenerator = TreeGeneratorType::New();
 treeGenerator->SetSample( sample );
 treeGenerator->SetBucketSize( 16 );
 std::cout << "Tree measurement vector size: " << treeGenerator->GetMeasurementVectorSize() << std::endl;
 treeGenerator->Update();
 typedef TreeGeneratorType::KdTreeType TreeType;
 typedef itk::Statistics::KdTreeBasedKmeansEstimator<TreeType> EstimatorType;
 EstimatorType::Pointer estimator = EstimatorType::New();
 EstimatorType::ParametersType initialMeans(2*3);
 initialMeans.Fill(165.0f);
 initialMeans[3] = 200;
 initialMeans[4] = 200;
 initialMeans[5] = 200;
 estimator->SetParameters( initialMeans );
 estimator->SetKdTree( treeGenerator->GetOutput() );
 estimator->SetMaximumIteration( 200 );
 estimator->SetCentroidPositionChangesThreshold(0.0);
 estimator->StartOptimization();
 EstimatorType::ParametersType estimatedMeans = estimator->GetParameters();
 std::cout << "estimatedMeans: " << estimatedMeans << std::endl;
 return EXIT_SUCCESS;

} </source>

CMakeLists.txt

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

project(KdTreeBasedKmeansEstimator)

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

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

endif()

add_executable(KdTreeBasedKmeansEstimator MACOSX_BUNDLE KdTreeBasedKmeansEstimator.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(KdTreeBasedKmeansEstimator ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(KdTreeBasedKmeansEstimator ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build KdTreeBasedKmeansEstimator

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

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

./KdTreeBasedKmeansEstimator

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.