ITK/Examples/Broken/Statistics/ExpectationMaximizationMixtureModelEstimator Image: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
No edit summary
Line 18: Line 18:
typedef itk::Image<PixelType, 2>  ImageType;
typedef itk::Image<PixelType, 2>  ImageType;


void ControlledImage(ImageType::Pointer image);
static void ControlledImage(ImageType::Pointer image);
void RandomImage(ImageType::Pointer image);
static void RandomImage(ImageType::Pointer image);


int main(int argc, char*argv[])
int main(int argc, char*argv[])
Line 302: Line 302:
</source>
</source>


==CMakeLists.txt==
{{ITKCMakeLists|ExpectationMaximizationMixtureModelEstimator_Image}}
<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>

Revision as of 03:06, 2 March 2011

The clusters are not computed correctly.

ExpectationMaximizationMixtureModelEstimator_Image.cxx

<source lang="cpp">

  1. include "itkVector.h"
  2. include "itkListSample.h"
  3. include "itkGaussianMixtureModelComponent.h"
  4. include "itkExpectationMaximizationMixtureModelEstimator.h"
  5. include "itkSampleClassifierFilter.h"
  6. include "itkMaximumDecisionRule2.h"
  7. include "itkImageToListSampleFilter.h"
  8. include "itkCovariantVector.h"
  9. include "itkImageRegionIterator.h"
  10. include "itkImageFileReader.h"
  11. include "itkSimpleFilterWatcher.h"

typedef itk::CovariantVector<unsigned char, 3> PixelType; typedef itk::Image<PixelType, 2> ImageType;

static void ControlledImage(ImageType::Pointer image); static void RandomImage(ImageType::Pointer image);

int main(int argc, char*argv[]) {

 ImageType::Pointer image = ImageType::New();
 RandomImage(image);
 //ControlledImage(image);
 typedef itk::Statistics::ImageToListSampleFilter<ImageType> ImageToListSampleFilterType;
 ImageToListSampleFilterType::Pointer imageToListSampleFilter = ImageToListSampleFilterType::New();
 imageToListSampleFilter->SetInput(image);
 imageToListSampleFilter->Update();
 unsigned int numberOfClasses = 3;
 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
   }
 unsigned int counter = 0;
 for(unsigned int i = 0; i < 3; i++)
   {
   for(unsigned int j = 0; j < 3; j++)
     {
     if(i == j)
       {
       params[3+counter] = 5; // diagonal
       }
     else
       {
         params[3+counter] = 0; // off-diagonal
       }
     counter++;
     }
   }


 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;
 counter = 0;
 for(unsigned int i = 0; i < 3; i++)
   {
   for(unsigned int j = 0; j < 3; j++)
     {
     if(i == j)
       {
       params[3+counter] = 5; // diagonal
       }
     else
       {
         params[3+counter] = 0; // off-diagonal
       }
     counter++;
     }
   }
 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;
 counter = 0;
 for(unsigned int i = 0; i < 3; i++)
   {
   for(unsigned int j = 0; j < 3; j++)
     {
     if(i == j)
       {
       params[3+counter] = 5; // diagonal
       }
     else
       {
         params[3+counter] = 0; // off-diagonal
       }
     counter++;
     }
   }
 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;
 std::cout << "Number of samples: " << imageToListSampleFilter->GetOutput()->GetTotalFrequency() << std::endl;
 // 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;
 std::cout << "Initial proportions: " << initialProportions << std::endl;
 estimator->SetInitialProportions( initialProportions );
 for ( unsigned int i = 0 ; i < numberOfClasses ; i++)
   {
   estimator->AddComponent( components[i]);
   }
 //itk::SimpleFilterWatcher watcher(estimator);
 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 ControlledImage(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;
   }

}

void RandomImage(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();
 itk::ImageRegionIterator<ImageType> imageIterator(image,region);
 imageIterator.GoToBegin();
 while(!imageIterator.IsAtEnd())
   {
   // Get a random color
   itk::CovariantVector<unsigned char, 3> pixel;
   pixel[0] = rand() * 255;
   pixel[1] = rand() * 255;
   pixel[2] = rand() * 255;
   imageIterator.Set(pixel);
   ++imageIterator;
   }

} </source>

CMakeLists.txt

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

project(ExpectationMaximizationMixtureModelEstimator_Image)

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

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

endif()

add_executable(ExpectationMaximizationMixtureModelEstimator_Image MACOSX_BUNDLE ExpectationMaximizationMixtureModelEstimator_Image.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(ExpectationMaximizationMixtureModelEstimator_Image ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(ExpectationMaximizationMixtureModelEstimator_Image ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build ExpectationMaximizationMixtureModelEstimator_Image

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

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

./ExpectationMaximizationMixtureModelEstimator_Image

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.