Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

Vector::VectorTem<Real64> Impala::Core::Feature::MarkovStationaryFeature ( Matrix::Mat *  cooccuranceMatrix  )  [inline]

Definition at line 39 of file MarkovStationaryFeature.h.

References Impala::Core::Array::Add(), Impala::Core::Matrix::GetDiagonal(), Impala::Core::Matrix::MatCopy(), Impala::Core::Matrix::MatE(), Impala::Core::Matrix::MatMul(), Impala::Core::Matrix::MatNrCol(), Impala::Core::Matrix::MatNrRow(), Impala::Core::Matrix::MatSumAxis0(), Impala::Core::Matrix::MatSumAxis1(), Impala::Core::Array::MulVal(), Impala::Core::Array::SetVal(), and Impala::Core::Vector::Sum().

Referenced by Impala::Core::VideoSet::LbpEval::HandleNewFrame(), MarkovStationaryFeatureTimed(), and testMSF().

00040 {
00041     using namespace Matrix;
00042     const int ITERATION_COUNT = 100;
00043 
00044     int n = MatNrRow(cooccuranceMatrix);
00045     if(MatNrCol(cooccuranceMatrix) != n)
00046     {
00047         // it cannot be!
00048         std::cerr << "cooccuranceMatrix must be square" << std::endl;
00049         throw "bye";
00050     }
00051 
00052     /* descriptor storage */
00053     Vector::VectorTem<Real64> descriptor(2 * n);
00054 
00055     /* normalize the co-ocurrance matrix */
00056     Mat* p = MatCreate<Mat>(n, n);
00057     Mat* normalizationFactors = MatSumAxis1(cooccuranceMatrix);
00058     for(int vj = 0; vj < n; vj++)
00059     {
00060         if(*MatE(normalizationFactors, vj, 0) == 0.0)
00061         {
00062             *MatE(normalizationFactors, vj, 0) = 1.0;
00063         }
00064         for(int i = 0; i < n; i++) 
00065         {
00066             *MatE(p, vj, i) = *MatE(cooccuranceMatrix, vj, i) / *MatE(normalizationFactors, vj, 0);
00067         }
00068     }
00069     //Dump(cooccuranceMatrix);
00070     //Dump(normalizationFactors);
00071     //Dump(p);
00072     delete normalizationFactors;
00073 
00074     /* we now have a transition matrix in p */
00075     Mat* identity = MatCreate<Mat>(n, n);
00076     SetVal(identity, 0.0);
00077     // set to identity matrix
00078     for(int i = 0; i < n; i++) 
00079     {
00080         *MatE(identity, i, i) = 1.0;
00081     }
00082     Mat* a = MatCopy(identity);
00083     Mat* previousPower = identity;
00084     // identity will be deleted in the loop as previousPower
00085     for(int k = 0; k < ITERATION_COUNT; k++)
00086     {
00087         Mat* currentPower = MatMul(previousPower, p);
00088         // perform matrix addition
00089         Add(a, a, currentPower);
00090         delete previousPower;
00091         previousPower = currentPower;
00092     }
00093     //std::cout << "previous:" << std::endl; Dump(previousPower);
00094     delete previousPower;
00095     MulVal(a, a, 1.0 / (ITERATION_COUNT + 1));
00096     //std::cout << "a_n:" << std::endl; Dump(a);
00097     
00098     /* construct the stationary disribution by taking the averages of the
00099        rows of a (all rows of a should be equal upon convergence) */
00100     Mat* stationarySum = MatSumAxis0(a);
00101     //Dump(stationarySum);
00102     for(int i = 0; i < n; i++)
00103     {
00104         descriptor[n + i] = *MatE(stationarySum, 0, i) / n;
00105     }
00106     delete a;
00107     delete stationarySum;
00108 
00109     /* construct initial distribution by normalizing the self-transition */
00110     Vector::VectorTem<Real64> diagonal = GetDiagonal(cooccuranceMatrix);
00111     Real64 normalizationFactor = Sum(diagonal);
00112     if(normalizationFactor == 0.0) normalizationFactor = 1.0;
00113     Vector::VectorTem<Real64> initialDistribution = diagonal * (1.0 / normalizationFactor);
00114     
00115     /* combine initial distribution and stationary distribution into a single
00116        vector */    
00117     for(int i = 0; i < n; i++)
00118     {
00119         descriptor[i] = initialDistribution[i];
00120     }
00121     delete p;
00122     
00123     return descriptor;
00124 }

Here is the call graph for this function:


Generated on Fri Mar 19 11:08:25 2010 for ImpalaSrc by  doxygen 1.5.1