<DIV id=RTEContent>Hi all, </DIV>  <DIV>&nbsp;</DIV>  <DIV>I have a problem with the Expectation Maximization algorithm. I read the tutorial and looked at the mailing list but there is not a single example of how I would read an image, generate its histogram, feed it into the EM and get the results (although all of them exist and work seperately, my&nbsp;combination is resisting to work). Below is the code,&nbsp;&nbsp;EM is terminating with&nbsp; * termination code=&nbsp; 1 *. And the clusters are getting *0* mean and variance values. I would appreciate if you could comment on what is wrong. The image I am using has two nicely gaussian classes, not to be cut, I will send it in a the next email. Thanks a lot for help! </DIV>  <DIV>Esen</DIV>  <DIV>&nbsp;</DIV>  <DIV><STRONG>Results:</STRONG></DIV>  <DIV>Cluster[0]<BR>&nbsp;&nbsp;&nbsp; Parameters:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [0, 0]<BR>&nbsp;&nbsp;&nbsp;
 Proportion:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1<BR>Cluster[1]<BR>&nbsp;&nbsp;&nbsp; Parameters:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [0, 0]<BR>&nbsp;&nbsp;&nbsp; Proportion:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.22156e-053<BR><STRONG>Code:</STRONG></DIV>  <DIV>&nbsp;</DIV>  <DIV>#include "itkScalarImageToHistogramGenerator.h"<BR>#include "itkImage.h"<BR>#include "itkVector.h"<BR>#include "itkListSample.h"<BR>#include "itkGaussianMixtureModelComponent.h"<BR>#include "itkExpectationMaximizationMixtureModelEstimator.h"<BR>// Software Guide : EndCodeSnippet</DIV>  <DIV>#include "itkImageFileReader.h"</DIV>  <DIV>int main( int argc, char * argv [] )<BR>{</DIV>  <DIV>/*<BR>&nbsp; if( argc &lt; 2 )<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp; std::cerr &lt;&lt; "Missing command line arguments" &lt;&lt; std::endl;<BR>&nbsp;&nbsp;&nbsp; std::cerr &lt;&lt; "Usage :&nbsp; ImageHistogram1&nbsp; inputImageFileName " &lt;&lt;
 std::endl;<BR>&nbsp;&nbsp;&nbsp; return -1;<BR>&nbsp;&nbsp;&nbsp; }<BR>*/</DIV>  <DIV>&nbsp; typedef unsigned char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PixelType;<BR>&nbsp; const unsigned int&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dimension = 2;</DIV>  <DIV>&nbsp; typedef itk::Image&lt;PixelType, Dimension &gt; ImageType;</DIV>  <DIV>&nbsp; typedef itk::ImageFileReader&lt; ImageType &gt; ReaderType;</DIV>  <DIV>&nbsp; ReaderType::Pointer reader = ReaderType::New();</DIV>  <DIV>&nbsp;// reader-&gt;SetFileName( argv[1] );<BR>&nbsp;&nbsp; reader-&gt;SetFileName( "Simulatedchecker.png" );</DIV>  <DIV>&nbsp; try<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp; reader-&gt;Update();<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; catch( itk::ExceptionObject &amp; excp )<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp; std::cerr &lt;&lt; "Problem encoutered while reading image file : " &lt;&lt; argv[1] &lt;&lt; std::endl;<BR>&nbsp;&nbsp;&nbsp; std::cerr &lt;&lt; excp &lt;&lt;
 std::endl;<BR>&nbsp;&nbsp;&nbsp; return -1;<BR>&nbsp;&nbsp;&nbsp; }</DIV>  <DIV>&nbsp; typedef itk::Statistics::ScalarImageToHistogramGenerator&lt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ImageType <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &gt;&nbsp;&nbsp; HistogramGeneratorType;</DIV>  <DIV>&nbsp; HistogramGeneratorType::Pointer histogramGenerator = HistogramGeneratorType::New();</DIV>  <DIV>&nbsp;
 histogramGenerator-&gt;SetInput(&nbsp; reader-&gt;GetOutput() );</DIV>  <DIV>&nbsp; histogramGenerator-&gt;SetNumberOfBins( 255 );<BR>&nbsp; histogramGenerator-&gt;SetMarginalScale( 10.0 );<BR>&nbsp; histogramGenerator-&gt;Compute();</DIV>  <DIV>&nbsp; typedef HistogramGeneratorType::HistogramType&nbsp; HistogramType;</DIV>  <DIV>&nbsp; const HistogramType * histogram = histogramGenerator-&gt;GetOutput();</DIV>  <DIV>&nbsp; const unsigned int histogramSize = histogram-&gt;Size();</DIV>  <DIV>&nbsp; std::cout &lt;&lt; "Histogram size " &lt;&lt; histogramSize &lt;&lt; std::endl;</DIV>  <DIV>&nbsp; unsigned int bin;<BR>&nbsp; for( bin=0; bin &lt; histogramSize; bin++ )<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "bin = " &lt;&lt; bin &lt;&lt; " frequency = ";<BR>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; histogram-&gt;GetFrequency( bin, 0 ) &lt;&lt; std::endl;<BR>&nbsp;&nbsp;&nbsp; }</DIV>  <DIV>&nbsp;</DIV>  <DIV>&nbsp; //*******************************<BR>&nbsp;
 //=====end of histogram ========<BR>&nbsp; //======start EM ================</DIV>  <DIV>&nbsp;unsigned int numberOfClasses = 2;<BR>&nbsp; typedef itk::Vector&lt; double, 1 &gt; MeasurementVectorType;<BR>&nbsp; typedef itk::Statistics::ListSample&lt; MeasurementVectorType &gt; SampleType;<BR>&nbsp; SampleType::Pointer sample = SampleType::New();<BR>&nbsp; <BR>&nbsp; for ( unsigned int i = 0 ; i &lt; histogramSize ; ++i )<BR>&nbsp;&nbsp;&nbsp; {</DIV>  <DIV>&nbsp;&nbsp;&nbsp;&nbsp; sample-&gt;PushBack( histogram-&gt;GetFrequency( bin, 0 ) );//0 is for first channel<BR>&nbsp;&nbsp;&nbsp; }</DIV>  <DIV>&nbsp;&nbsp; //initial parameters for EM<BR>&nbsp; typedef itk::Array&lt; double &gt; ParametersType;<BR>&nbsp; ParametersType params( 2 );</DIV>  <DIV>&nbsp; std::vector&lt; ParametersType &gt; initialParameters( numberOfClasses );<BR>&nbsp; params[0] = 50.0;<BR>&nbsp; params[1] = 100.0;<BR>&nbsp; initialParameters[0] = params;</DIV>  <DIV>&nbsp; params[0] = 200.0;<BR>&nbsp; params[1] =
 150.0;<BR>&nbsp; initialParameters[1] = params;<BR>&nbsp; <BR>&nbsp; // EM will estimate with Gaussians<BR>&nbsp; typedef itk::Statistics::GaussianMixtureModelComponent&lt; SampleType &gt; <BR>&nbsp;&nbsp;&nbsp; ComponentType;</DIV>  <DIV>&nbsp; std::vector&lt; ComponentType::Pointer &gt; components;<BR>&nbsp; for ( unsigned int i = 0 ; i &lt; numberOfClasses ; i++ )<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp; components.push_back( ComponentType::New() );<BR>&nbsp;&nbsp;&nbsp; (components[i])-&gt;SetSample( sample );<BR>&nbsp;&nbsp;&nbsp; (components[i])-&gt;SetParameters( initialParameters[i] );<BR>&nbsp;&nbsp;&nbsp; }</DIV>  <DIV>&nbsp; typedef itk::Statistics::ExpectationMaximizationMixtureModelEstimator&lt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SampleType &gt; EstimatorType;<BR>&nbsp; EstimatorType::Pointer estimator = EstimatorType::New();</DIV> 
 <DIV>&nbsp; estimator-&gt;SetSample( sample );<BR>&nbsp; estimator-&gt;SetMaximumIteration( 300 );</DIV>  <DIV>&nbsp; itk::Array&lt; double &gt; initialProportions(numberOfClasses);<BR>&nbsp; initialProportions[0] = 0.5;<BR>&nbsp; initialProportions[1] = 0.5;</DIV>  <DIV>&nbsp; estimator-&gt;SetInitialProportions( initialProportions );<BR>&nbsp; <BR>&nbsp; for ( unsigned int i = 0 ; i &lt; numberOfClasses ; i++)<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp; estimator-&gt;AddComponent( (ComponentType::Superclass*)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (components[i]).GetPointer() );<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; <BR>&nbsp; try<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp; estimator-&gt;Update();<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; catch( itk::ExceptionObject &amp; excp )<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp; std::cerr &lt;&lt; "Problem
 encoutered while estimating" &lt;&lt; std::endl;<BR>&nbsp;&nbsp;&nbsp; std::cerr &lt;&lt; excp &lt;&lt; std::endl;<BR>&nbsp;&nbsp;&nbsp; return -1;<BR>&nbsp;&nbsp;&nbsp; }</DIV>  <DIV><BR>&nbsp; int termincode = estimator-&gt;GetTerminationCode (); // CONVERGED = 0, NOT_CONVERGED = 1 }<BR>&nbsp;&nbsp; std::cout&lt;&lt;"termination code=&nbsp; "&lt;&lt;termincode&lt;&lt;std::endl;</DIV>  <DIV><BR>&nbsp; for ( unsigned int i = 0 ; i &lt; numberOfClasses ; i++ )<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "Cluster[" &lt;&lt; i &lt;&lt; "]" &lt;&lt; std::endl;<BR>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "&nbsp;&nbsp;&nbsp; Parameters:" &lt;&lt; std::endl;<BR>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " &lt;&lt; (components[i])-&gt;GetFullParameters() <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; std::endl;<BR>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "&nbsp;&nbsp;&nbsp; Proportion:
 ";<BR>&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " &lt;&lt; (*estimator-&gt;GetProportions())[i] &lt;&lt; std::endl;<BR>&nbsp;&nbsp;&nbsp; }</DIV>  <DIV>&nbsp; return 0;<BR>&nbsp; <BR>}</DIV><p>
                <hr size=1> <a href="http://us.lrd.yahoo.com/_ylc=X3oDMTFqODRtdXQ4BF9TAzMyOTc1MDIEX3MDOTY2ODgxNjkEcG9zAzEEc2VjA21haWwtZm9vdGVyBHNsawNmYw--/SIG=110oav78o/**http%3a//farechase.yahoo.com/">Yahoo! FareChase - Search multiple travel sites in one click.</a>