<br>Dear ITK-Users,<br><br>I am working on a class to estimate the initial
parameters (mean vector, covariance matrix and weighting vector) for a
Gaussian Mixture model. For that I am using the
KdTreeBasedKmeansEstimator and the refactoring statistic classes. To be
more specifically, I am using the  itk::Statistics::<div id=":4n" class="ii gt">CovarianceSampleFilter
to compute the Mean vector and Covariance matrices. However, the
results I am getting are completely wrong. After tracking the code, it
seems that the mean vector is always zero. <br>
<br>Could anybody help me to solve this problem please? I am not sure if I am missing something. <br><br>Thank you,<br>Ricardo<br><br><br>    ///<br>    /// Create a image of array from different image contrasts<br>    ///<br>


    typedef itk::ScalarToArrayCastImageFilter&lt; InputImageType, ArrayImageType &gt; CasterType;<br>    CasterType::Pointer caster = CasterType::New();<br>    for( int i=0; i &lt; NumberOfContrasts; ++i )<br>        caster-&gt;SetInput( i, ReadMincImage&lt; InputImageType &gt;( inputFileName[i] ) );<br>


    caster-&gt;Update();<br><br><br>    ///<br>    /// Generate sample data<br>    ///<br>    typedef itk::Statistics::ImageToListSampleAdaptor&lt; TArrayImageType &gt; ImageToListSampleAdaptorType;<br>    typename ImageToListSampleAdaptorType::Pointer adaptor = ImageToListSampleAdaptorType::New();<br>


    adaptor-&gt;SetImage( caster-&gt;GetOutput() );<br><br>    ///<br>    /// Create KdTree<br>    /// <br>    typedef itk::Statistics::WeightedCentroidKdTreeGenerator &lt; ImageToListSampleAdaptorType &gt; TreeGeneratorType;<br>


    typename TreeGeneratorType::Pointer treeGenerator = TreeGeneratorType::New();<br>    treeGenerator-&gt;SetSample( adaptor );<br>    treeGenerator-&gt;SetBucketSize( m_BuckedSize );<br>    treeGenerator-&gt;Update();<br>


<br>    typedef itk::Statistics::KdTreeBasedKmeansEstimator &lt; typename TreeGeneratorType::KdTreeType &gt; EstimatorType;<br>    typename EstimatorType::Pointer estimator = EstimatorType::New();<br><br>    ///<br>    /// Estimate classes mean vector<br>


    ///<br>    typename EstimatorType::ParametersType initialMeans( NumberOfContrasts * NumberOfClasses );<br>    estimator-&gt;SetParameters( initialMeans );<br>    estimator-&gt;SetKdTree( treeGenerator-&gt;GetOutput() );<br>


    estimator-&gt;SetMaximumIteration( m_MaxNumberOfIterations );<br>    estimator-&gt;SetCentroidPositionChangesThreshold( m_CentroidChangePosThresh );<br>    estimator-&gt;StartOptimization();<br><br>    typename EstimatorType::ParametersType estimatedMeans = estimator-&gt;GetParameters();<br>


    <br>    ///<br>    /// Compute classes labels <br>    ///    <br>    typedef itk::Statistics::SampleClassifierFilter&lt; ImageToListSampleAdaptorType &gt;  SampleClassifierFilterType;<br><br>    typedef typename SampleClassifierFilterType::ClassLabelVectorObjectType  ClassLabelVectorObjectType;<br>


    typename ClassLabelVectorObjectType::Pointer classLabelsObject = ClassLabelVectorObjectType::New();<br>    <br>    typedef typename SampleClassifierFilterType::ClassLabelVectorType  ClassLabelVectorType;<br>    ClassLabelVectorType&amp; classLabelVector = classLabelsObject-&gt;Get();<br>


    for( unsigned int i=0; i &lt; NumberOfClasses; i++ )<br>         classLabelVector.push_back( typename SampleClassifierFilterType::ClassLabelType( (i + 1) * 100 ) );<br><br>      //Set a decision rule type<br>      typedef itk::Statistics::MinimumDecisionRule2  DecisionRuleType;<br>


      typename DecisionRuleType::Pointer decisionRule = DecisionRuleType::New();<br><br>      const typename SampleClassifierFilterType::MembershipFunctionVectorObjectType *membershipFunctionsObject = estimator-&gt;GetOutput();<br>


      const typename SampleClassifierFilterType::MembershipFunctionVectorType membershipFunctions = membershipFunctionsObject-&gt;Get();<br><br>      //Instantiate and pass all the required inputs to the filter<br>    typename SampleClassifierFilterType::Pointer classifier = SampleClassifierFilterType::New();<br>


    classifier-&gt;SetInput( adaptor );<br>    classifier-&gt;SetNumberOfClasses( NumberOfClasses );<br>    classifier-&gt;SetClassLabels( classLabelsObject );<br>    classifier-&gt;SetDecisionRule( decisionRule );<br>    classifier-&gt;SetMembershipFunctions( membershipFunctionsObject );<br>


    classifier-&gt;Update();<br><br>    const typename SampleClassifierFilterType::MembershipSampleType* membershipSample = classifier-&gt;GetOutput();<br><br>/*<br>    const typename SampleClassifierFilterType::MembershipSampleType::ClassLabelType label1( 100 );<br>


      const typename SampleClassifierFilterType::MembershipSampleType::ClassSampleType* subSample1 = membershipSample-&gt;GetClassSample( label1 );<br>    std::cout &lt;&lt; subSample1-&gt;GetTotalFrequency() &lt;&lt; std::endl;<br>


<br>    const typename SampleClassifierFilterType::MembershipSampleType::ClassLabelType label2( 200 );<br>      const typename SampleClassifierFilterType::MembershipSampleType::ClassSampleType* subSample2 = membershipSample-&gt;GetClassSample( label2 );<br>


    std::cout &lt;&lt; subSample2-&gt;GetTotalFrequency() &lt;&lt; std::endl;<br><br>    const typename SampleClassifierFilterType::MembershipSampleType::ClassLabelType label3( 300 );<br>      const typename SampleClassifierFilterType::MembershipSampleType::ClassSampleType* subSample3 = membershipSample-&gt;GetClassSample( label3 );<br>


    std::cout &lt;&lt; subSample3-&gt;GetTotalFrequency() &lt;&lt; std::endl;<br>*/    <br>    <br>    <br><br>    ///<br>    /// Compute the covariance matrices and the weight vectors<br>    ///<br>    typedef typename SampleClassifierFilterType::MembershipSampleType::ClassSampleType    ClassSampleType;<br>


    typedef itk::Statistics::CovarianceSampleFilter&lt; ClassSampleType &gt;                     CovarianceSampleFilterType;<br>    <br>    /// Resize weight class vector<br>    TGMM_InitStrategy&lt; TArrayImageType &gt;::m_WeightClassesVector.SetSize( NumberOfClasses );<br>


        <br>    /// Now, get the mean &amp; covariance into the parameters vector<br>    for( unsigned int i=0; i &lt; NumberOfClasses; ++i )<br>    {<br>        typename SampleClassifierFilterType::MembershipSampleType::ClassLabelType classLabel( (i + 1) * 100 );<br>


    <br>        /// Get a pointer only to the samples of classLabel        <br>        const ClassSampleType* sampleClass = membershipSample-&gt;GetClassSample( classLabel );<br><br>/*  The result here is okay<br>std::cout &lt;&lt; sampleClass-&gt;Size() &lt;&lt; std::endl;    <br>


<br>        double sum = 0;<br>        for( int ii=0; ii &lt; sampleClass-&gt;Size(); ii++ )<br>        {<br>            typename ClassSampleType::MeasurementVectorType val = sampleClass-&gt;GetMeasurementVectorByIndex( ii );<br>


            sum = sum + val[0];<br>        }<br>std::cout &lt;&lt; &quot;Mean value = &quot; &lt;&lt; (double)( sum / sampleClass-&gt;Size() ) &lt;&lt; std::endl;<br>*/<br><br><br>        /// Compute covariance matrix        <br>


        typename CovarianceSampleFilterType::Pointer covarianceFilter = CovarianceSampleFilterType::New(); <br>        covarianceFilter-&gt;SetInput( sampleClass );<br>        covarianceFilter-&gt;Update();<br>        <br>


          const typename CovarianceSampleFilterType::MeasurementVectorDecoratedType *MeanDecorator = covarianceFilter-&gt;GetMeanOutput();<br>          typename CovarianceSampleFilterType::MeasurementVectorType mean = MeanDecorator-&gt;Get();<br>


<br>/* The results here are wrong */          <br>std::cout &lt;&lt; &quot;Mean Vector1: &quot; &lt;&lt; MeanDecorator-&gt;Get() &lt;&lt; std::endl;<br>std::cout &lt;&lt; &quot;Mean Vector2: &quot; &lt;&lt; covarianceFilter-&gt;GetMean() &lt;&lt; std::endl;<br>


 <br>          const typename CovarianceSampleFilterType::MatrixDecoratedType *CovDecorator = covarianceFilter-&gt;GetCovarianceMatrixOutput();<br>          typename CovarianceSampleFilterType::MatrixType cov = CovDecorator-&gt;Get();<br>


<br>/* The results here are wrong */          <br>
std::cout &lt;&lt; &quot;Covariance Matrix1: &quot; &lt;&lt; CovDecorator-&gt;Get() &lt;&lt; std::endl;<br>std::cout &lt;&lt; &quot;Covariance Matrix2: &quot; &lt;&lt; covarianceFilter-&gt;GetCovarianceMatrix() &lt;&lt; std::endl;<br>


<br>        /// Create a parameter array for the current class <br>        ParametersType *params = new ParametersType( NumberOfContrasts * NumberOfContrasts + NumberOfContrasts );<br><br>        unsigned int index = 0;<br>


        for( unsigned j=0; j &lt; NumberOfContrasts; j++ )<br>        {<br>            /// Copy mean vector<br>            params-&gt;SetElement( index++, static_cast&lt; double &gt;( estimatedMeans[i * NumberOfContrasts + j] ) );<br>


            <br>            /// Copy covariance matrix<br>            for( unsigned k=0; k &lt; NumberOfContrasts; k++ )<br>                params-&gt;SetElement( index++, (double)cov[j][k] );<br>        }        <br><br>


        TGMM_InitStrategy&lt; TArrayImageType &gt;::m_Parameters.push_back( params );         <br>        TGMM_InitStrategy&lt; TArrayImageType &gt;::m_WeightClassesVector.SetElement( i, (double)sampleClass-&gt;GetTotalFrequency() / adaptor-&gt;GetTotalFrequency() );<br>


    <br>    }</div>