[Insight-users] KMeans type mismatch

David Doria daviddoria+itk at gmail.com
Thu Jul 23 09:18:43 EDT 2009


I want to do a KMeans clustering of 3d points. So I setup a 3-vector like this:

	typedef itk::Vector< double, 3 > MeasurementVectorType;

and fill it with some points that are clearly in 3 clusters:

	MeasurementVectorType p0, p1, p2, p3, p4, p5, p6, p7, p8;
		
	//cluster 1
	p0[0]=  0.0; p0[1]= 0.0; p0[2]= 0.0;
	p1[0]=  0.1; p1[1]= 0.0; p1[2]= 0.0;
	p2[0]=  0.0; p2[1]= 0.1; p2[2]= 0.0;
	
	//cluster 2
	p3[0]=  5.0; p3[1]=  5.0; p3[2]= 5.0;
	p4[0]=  5.1; p4[1]=  5.0; p4[2]= 5.0;
	p5[0]=  5.0; p5[1]=  5.1; p5[2]= 5.0;
	
	//cluster 3
	p6[0]=  -5.0; p6[1]=  -5.0; p6[2]= -5.0;
	p7[0]=  -5.1; p7[1]=  -5.0; p7[2]= -5.0;
	p8[0]=  -5.0; p8[1]=  -5.1; p8[2]= -5.0;
		
Then I setup the ListSample for the KdTree:

	typedef itk::Statistics::ListSample< MeasurementVectorType > SampleType;
	SampleType::Pointer sample = SampleType::New();
	sample->PushBack(p0);
	sample->PushBack(p1);
	sample->PushBack(p2);
	sample->PushBack(p3);
	sample->PushBack(p4);
	sample->PushBack(p5);
	sample->PushBack(p6);
	sample->PushBack(p7);
	sample->PushBack(p8);
	
	typedef itk::Statistics::WeightedCentroidKdTreeGenerator< SampleType
> TreeGeneratorType;
	//typedef itk::Statistics::KdTreeGenerator< SampleType > TreeGeneratorType;
	TreeGeneratorType::Pointer treeGenerator = TreeGeneratorType::New();
	treeGenerator->SetSample( sample );
	treeGenerator->SetBucketSize( 3 ); //number of measurement vectors in
a terminal node
	treeGenerator->Update();
	
Now I want to seed the KMeans algorithm with 3 points in the 3d space:
	// Once we have the k-d tree, it is a simple procedure to produce k
mean estimates
	
	typedef TreeGeneratorType::KdTreeType TreeType;
	typedef itk::Statistics::KdTreeBasedKmeansEstimator<TreeType> EstimatorType;
	EstimatorType::Pointer estimator = EstimatorType::New();
	
	MeasurementVectorType InitialMean0, InitialMean1, InitialMean2;
	InitialMean0[0] = 1.0;
	InitialMean0[1] = 1.0;
	InitialMean0[2] = 1.0;
	
	InitialMean1[0] = -1.0;
	InitialMean1[1] = -1.0;
	InitialMean1[2] = -1.0;
	
	InitialMean2[0] = 0.0;
	InitialMean2[1] = 0.0;
	InitialMean2[2] = 0.0;

I create a vector of 3d vectors:	
	unsigned int NumClasses = 3;
	EstimatorType::ParametersType initialMeans(NumClasses);
	initialMeans[0] = InitialMean0;
	initialMeans[1] = InitialMean1;
	initialMeans[2] = InitialMean2;

In hopes to do something like this:
		
	estimator->SetParameters( initialMeans );
	estimator->SetKdTree( treeGenerator->GetOutput() );
	estimator->SetMaximumIteration( 200 );
	estimator->SetCentroidPositionChangesThreshold(0.0);
	estimator->StartOptimization();

But it complains on these lines:	
	initialMeans[0] = InitialMean0;

error: cannot convert 'main()::MeasurementVectorType' to 'double' in assignment

How would I setup these initial means?

On a separate note: is there an agglomerative clustering algorithm in
ITK? That is, have it start with the 9 points in a single cluster and
then split the clusters until some "continue splitting" tolerance is
met?

Thanks,

David


More information about the Insight-users mailing list