views:

60

answers:

0

Hi everyone, Here is my code to calculate covariance matrix over a particular area of color image,But it always gives all zeroes.Please help me where am going wrong.

IplImage* img = cvLoadImage("image.png",1);    
cvShowImage("input",img);
 cvSetImageROI(img,cvRect(10,10,30,30));
       {
    CvMat* covarMatrix=cvCreateMat(3,3,CV_32FC1);
    CvMat* avg=cvCreateMat(3,1,CV_32FC1);
    CvMat* Vector[9];
    int m,n;

 for( m=0;m<3;m++) 
    {
    for(n=0;n<3;n++)
      { 
   double vec[3];
   CvMat* Mat=cvCreateMat(3,1,CV_32FC1);    
   CvScalar Scalar1=cvGet2D(img,m,n);
   vec[0]=Scalar1.val[0];
   vec[1]=Scalar1.val[1];
   vec[2]=Scalar1.val[2];

  cvSetData(Mat,vec,CV_AUTO_STEP);
  Vector [m*3+n]=Mat;
      }
}
    cvCalcCovarMatrix((const CvArr**)&Vector,9,covarMatrix,avg,CV_COVAR_NORMAL);

for(int i=0;i<3;i++)
    {
    for(int j=0;j<3;j++)
               {
       printf ("%f ", cvGetReal2D(covarMatrix,i,j));
       }
      printf ("\n");
    }

    cvResetImageROI(img);
}