ITK/Examples/Broken/Statistics/ExpectationMaximizationMixtureModelEstimator Image: Difference between revisions
Daviddoria (talk | contribs) (Created page with "The clusters are not computed correctly. The output is: <source lang="text"> Cluster[0] Parameters: [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5] Proportion: -na...") |
Daviddoria (talk | contribs) (Converted from itkImageToListSampleAdaptor to itkImageToListSampleFilter) |
||
Line 24: | Line 24: | ||
#include "itkSampleClassifierFilter.h" | #include "itkSampleClassifierFilter.h" | ||
#include "itkMaximumDecisionRule2.h" | #include "itkMaximumDecisionRule2.h" | ||
#include "itkImageToListSampleAdaptor.h" | //#include "itkImageToListSampleAdaptor.h" | ||
#include "itkImageToListSampleFilter.h" | |||
#include "itkCovariantVector.h" | #include "itkCovariantVector.h" | ||
#include "itkImageRegionIterator.h" | #include "itkImageRegionIterator.h" | ||
#include "itkImageFileReader.h" | |||
typedef itk::CovariantVector<unsigned char, 3> PixelType; | typedef itk::CovariantVector<unsigned char, 3> PixelType; | ||
Line 33: | Line 35: | ||
void CreateImage(ImageType::Pointer image); | void CreateImage(ImageType::Pointer image); | ||
int main(int, char*[]) | int main(int argc, char*argv[]) | ||
{ | { | ||
/* | |||
// Read a real image file | |||
typedef itk::ImageFileReader<ImageType> ReaderType; | |||
ReaderType::Pointer reader = ReaderType::New(); | |||
std::cout << "Reading " << argv[1] << std::endl; | |||
reader->SetFileName(argv[1]); | |||
reader->Update(); | |||
*/ | |||
ImageType::Pointer image = ImageType::New(); | ImageType::Pointer image = ImageType::New(); | ||
CreateImage(image); | CreateImage(image); | ||
//image->Graft(reader->GetOutput()); | |||
//typedef itk::Statistics::ImageToListSampleAdaptor<ImageType> ImageToListSampleAdaptorType; | |||
//ImageToListSampleAdaptorType::Pointer imageToListSampleAdaptor = ImageToListSampleAdaptorType::New(); | |||
typedef itk::Statistics::ImageToListSampleFilter<ImageType> ImageToListSampleFilterType; | |||
ImageToListSampleFilterType::Pointer imageToListSampleFilter = ImageToListSampleFilterType::New(); | |||
imageToListSampleFilter->SetInput(image); | |||
imageToListSampleFilter->Update(); | |||
unsigned int numberOfClasses = 3; | unsigned int numberOfClasses = 3; | ||
Line 89: | Line 103: | ||
std::cout << initialParameters[i] << std::endl; | std::cout << initialParameters[i] << std::endl; | ||
} | } | ||
typedef itk::Statistics::GaussianMixtureModelComponent< | typedef itk::Statistics::GaussianMixtureModelComponent< ImageToListSampleFilterType::ListSampleType > | ||
ComponentType; | ComponentType; | ||
Line 98: | Line 112: | ||
{ | { | ||
components.push_back( ComponentType::New() ); | components.push_back( ComponentType::New() ); | ||
(components[i])->SetSample( | (components[i])->SetSample( imageToListSampleFilter->GetOutput() ); | ||
(components[i])->SetParameters( initialParameters[i] ); | (components[i])->SetParameters( initialParameters[i] ); | ||
} | } | ||
typedef itk::Statistics::ExpectationMaximizationMixtureModelEstimator< | typedef itk::Statistics::ExpectationMaximizationMixtureModelEstimator< | ||
ImageToListSampleFilterType::ListSampleType > EstimatorType; | |||
EstimatorType::Pointer estimator = EstimatorType::New(); | EstimatorType::Pointer estimator = EstimatorType::New(); | ||
estimator->SetSample( | estimator->SetSample( imageToListSampleFilter->GetOutput() ); | ||
estimator->SetMaximumIteration( 200 ); | estimator->SetMaximumIteration( 200 ); | ||
Line 137: | Line 151: | ||
// Display the membership of each sample | // Display the membership of each sample | ||
typedef itk::Statistics::SampleClassifierFilter< | typedef itk::Statistics::SampleClassifierFilter< ImageToListSampleFilterType::ListSampleType > FilterType; | ||
typedef itk::Statistics::MaximumDecisionRule2 DecisionRuleType; | typedef itk::Statistics::MaximumDecisionRule2 DecisionRuleType; | ||
Line 160: | Line 174: | ||
FilterType::Pointer sampleClassifierFilter = FilterType::New(); | FilterType::Pointer sampleClassifierFilter = FilterType::New(); | ||
sampleClassifierFilter->SetInput( | sampleClassifierFilter->SetInput( imageToListSampleFilter->GetOutput() ); | ||
sampleClassifierFilter->SetNumberOfClasses( numberOfClasses ); | sampleClassifierFilter->SetNumberOfClasses( numberOfClasses ); | ||
sampleClassifierFilter->SetClassLabels( classLabelsObject ); | sampleClassifierFilter->SetClassLabels( classLabelsObject ); | ||
Line 213: | Line 227: | ||
black[1] = 0; | black[1] = 0; | ||
black[2] = 0; | black[2] = 0; | ||
itk::ImageRegionIterator<ImageType> imageIterator(image,region); | itk::ImageRegionIterator<ImageType> imageIterator(image,region); | ||
imageIterator.GoToBegin(); | imageIterator.GoToBegin(); | ||
while(!imageIterator.IsAtEnd()) | while(!imageIterator.IsAtEnd()) | ||
{ | { |
Revision as of 23:44, 22 November 2010
The clusters are not computed correctly. The output is: <source lang="text"> Cluster[0]
Parameters: [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5] Proportion: -nan
Cluster[1]
Parameters: [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5] Proportion: -nan
Cluster[2]
Parameters: [0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5] Proportion: -nan
</source>
ExpectationMaximizationMixtureModelEstimator_Image.cxx
<source lang="cpp">
- include "itkVector.h"
- include "itkListSample.h"
- include "itkGaussianMixtureModelComponent.h"
- include "itkExpectationMaximizationMixtureModelEstimator.h"
- include "itkNormalVariateGenerator.h"
- include "itkSampleClassifierFilter.h"
- include "itkMaximumDecisionRule2.h"
//#include "itkImageToListSampleAdaptor.h"
- include "itkImageToListSampleFilter.h"
- include "itkCovariantVector.h"
- include "itkImageRegionIterator.h"
- include "itkImageFileReader.h"
typedef itk::CovariantVector<unsigned char, 3> PixelType; typedef itk::Image<PixelType, 2> ImageType;
void CreateImage(ImageType::Pointer image);
int main(int argc, char*argv[]) {
/* // Read a real image file typedef itk::ImageFileReader<ImageType> ReaderType; ReaderType::Pointer reader = ReaderType::New(); std::cout << "Reading " << argv[1] << std::endl; reader->SetFileName(argv[1]); reader->Update(); */ ImageType::Pointer image = ImageType::New();
CreateImage(image); //image->Graft(reader->GetOutput());
//typedef itk::Statistics::ImageToListSampleAdaptor<ImageType> ImageToListSampleAdaptorType; //ImageToListSampleAdaptorType::Pointer imageToListSampleAdaptor = ImageToListSampleAdaptorType::New(); typedef itk::Statistics::ImageToListSampleFilter<ImageType> ImageToListSampleFilterType; ImageToListSampleFilterType::Pointer imageToListSampleFilter = ImageToListSampleFilterType::New(); imageToListSampleFilter->SetInput(image); imageToListSampleFilter->Update();
unsigned int numberOfClasses = 3;
typedef itk::Statistics::NormalVariateGenerator NormalGeneratorType; NormalGeneratorType::Pointer normalGenerator = NormalGeneratorType::New();
typedef itk::Array< double > ParametersType; ParametersType params( numberOfClasses + numberOfClasses*numberOfClasses ); // 3 for means and 9 for 3x3 covariance
// Create the first set (for the first cluster/model) of initial parameters std::vector< ParametersType > initialParameters( numberOfClasses ); for(unsigned int i = 0; i < 3; i++) { params[i] = 5.0; // mean of dimension i } for(unsigned int i = 3; i < 12; i++) { params[i] = 5.0; // covariance }
initialParameters[0] = params;
// Create the second set (for the second cluster/model) of initial parameters params[0] = 210.0; params[1] = 5.0; params[2] = 5.0; for(unsigned int i = 3; i < 12; i++) { params[i] = 5.0; // covariance } initialParameters[1] = params;
// Create the third set (for the third cluster/model) of initial parameters params[0] = 5.0; params[1] = 210.0; params[2] = 5.0; for(unsigned int i = 3; i < 12; i++) { params[i] = 5.0; // covariance } initialParameters[2] = params;
std::cout << "Initial parameters: " << std::endl; for ( unsigned int i = 0 ; i < numberOfClasses ; i++ ) { std::cout << initialParameters[i] << std::endl; }
typedef itk::Statistics::GaussianMixtureModelComponent< ImageToListSampleFilterType::ListSampleType > ComponentType;
// Create the components std::vector< ComponentType::Pointer > components; for ( unsigned int i = 0 ; i < numberOfClasses ; i++ ) { components.push_back( ComponentType::New() ); (components[i])->SetSample( imageToListSampleFilter->GetOutput() ); (components[i])->SetParameters( initialParameters[i] ); }
typedef itk::Statistics::ExpectationMaximizationMixtureModelEstimator< ImageToListSampleFilterType::ListSampleType > EstimatorType; EstimatorType::Pointer estimator = EstimatorType::New();
estimator->SetSample( imageToListSampleFilter->GetOutput() ); estimator->SetMaximumIteration( 200 );
itk::Array< double > initialProportions(numberOfClasses); initialProportions[0] = 0.33; initialProportions[1] = 0.33; initialProportions[2] = 0.33;
estimator->SetInitialProportions( initialProportions );
for ( unsigned int i = 0 ; i < numberOfClasses ; i++) { estimator->AddComponent( (ComponentType::Superclass*) (components[i]).GetPointer() ); }
estimator->Update();
// Output the results for ( unsigned int i = 0 ; i < numberOfClasses ; i++ ) { std::cout << "Cluster[" << i << "]" << std::endl; std::cout << " Parameters:" << std::endl; std::cout << " " << (components[i])->GetFullParameters() << std::endl; std::cout << " Proportion: "; // Outputs: // mean of dimension 1, mean of dimension 2, covariance(0,0), covariance(0,1), covariance(1,0), covariance(1,1) std::cout << " " << estimator->GetProportions()[i] << std::endl; }
// Display the membership of each sample typedef itk::Statistics::SampleClassifierFilter< ImageToListSampleFilterType::ListSampleType > FilterType;
typedef itk::Statistics::MaximumDecisionRule2 DecisionRuleType; DecisionRuleType::Pointer decisionRule = DecisionRuleType::New();
typedef FilterType::ClassLabelVectorObjectType ClassLabelVectorObjectType; typedef FilterType::ClassLabelVectorType ClassLabelVectorType;
ClassLabelVectorObjectType::Pointer classLabelsObject = ClassLabelVectorObjectType::New(); ClassLabelVectorType & classLabelVector = classLabelsObject->Get();
typedef FilterType::ClassLabelType ClassLabelType;
ClassLabelType class0 = 0; classLabelVector.push_back( class0 );
ClassLabelType class1 = 1; classLabelVector.push_back( class1 );
ClassLabelType class2 = 2; classLabelVector.push_back( class2 );
FilterType::Pointer sampleClassifierFilter = FilterType::New(); sampleClassifierFilter->SetInput( imageToListSampleFilter->GetOutput() ); sampleClassifierFilter->SetNumberOfClasses( numberOfClasses ); sampleClassifierFilter->SetClassLabels( classLabelsObject ); sampleClassifierFilter->SetDecisionRule( decisionRule ); sampleClassifierFilter->SetMembershipFunctions( estimator->GetOutput() ); sampleClassifierFilter->Update();
const FilterType::MembershipSampleType* membershipSample = sampleClassifierFilter->GetOutput(); FilterType::MembershipSampleType::ConstIterator iter = membershipSample->Begin();
while ( iter != membershipSample->End() ) { std::cout << (int)iter.GetMeasurementVector()[0] << " " << (int)iter.GetMeasurementVector()[1] << " " << (int)iter.GetMeasurementVector()[2] << " : " << iter.GetClassLabel() << std::endl; ++iter; }
return EXIT_SUCCESS;
}
void CreateImage(ImageType::Pointer image) {
// Create an image ImageType::RegionType region; ImageType::IndexType start; start[0] = 0; start[1] = 0;
ImageType::SizeType size; size[0] = 10; size[1] = 10;
region.SetSize(size); region.SetIndex(start);
image->SetRegions(region); image->Allocate();
// Make a red and a green square itk::CovariantVector<unsigned char, 3> green; green[0] = 0; green[1] = 255; green[2] = 0;
itk::CovariantVector<unsigned char, 3> red; red[0] = 255; red[1] = 0; red[2] = 0;
itk::CovariantVector<unsigned char, 3> black; black[0] = 0; black[1] = 0; black[2] = 0;
itk::ImageRegionIterator<ImageType> imageIterator(image,region); imageIterator.GoToBegin();
while(!imageIterator.IsAtEnd()) { if(imageIterator.GetIndex()[0] > 2 && imageIterator.GetIndex()[0] < 5 && imageIterator.GetIndex()[1] > 2 && imageIterator.GetIndex()[1] < 5) { imageIterator.Set(green); } else if(imageIterator.GetIndex()[0] > 6 && imageIterator.GetIndex()[0] < 9 && imageIterator.GetIndex()[1] > 6 && imageIterator.GetIndex()[1] < 9) { imageIterator.Set(red); } else { imageIterator.Set(black); } ++imageIterator; }
} </source>
CMakeLists.txt
<source lang="cmake"> cmake_minimum_required(VERSION 2.6)
PROJECT(ExpectationMaximizationMixtureModelEstimator)
FIND_PACKAGE(ITK REQUIRED) INCLUDE(${ITK_USE_FILE})
ADD_EXECUTABLE(ExpectationMaximizationMixtureModelEstimator_Image ExpectationMaximizationMixtureModelEstimator_Image.cxx) TARGET_LINK_LIBRARIES(ExpectationMaximizationMixtureModelEstimator_Image ITKBasicFilters ITKCommon ITKIO ITKStatistics)
</source>