## Sunday, August 14, 2005

### Matrix in OpenCV (draft)

1a. initilize the values:
float xd[] = {1,cols/2,cols,cols,cols,cols/2,1,1};
CvMat Mxd = cvMat(1,8,CV_32FC1, xd);

//(1,8) is the same as matlab. 1x8 matrix
The values inside the matrix:
((float*)(Mru.data.ptr + Mru.step*0))[i] //on row, i=0-7

1b. initialize the values 2:
CvMat* Mxdd = cvCreateMat(2,4,CV_32FC1);
CV_MAT_ELEM( *Mxdd, float, 0, 0 ) = 1.f;
CV_MAT_ELEM( *Mxdd, float, 0, 1 ) = 2.f;
CV_MAT_ELEM( *Mxdd, float, 0, 2 ) = 3.f;
CV_MAT_ELEM( *Mxdd, float, 0, 3 ) = 4.f;
CV_MAT_ELEM( *Mxdd, float, 1, 0 ) = 5.f;
CV_MAT_ELEM( *Mxdd, float, 1, 1 ) = 6.f;
CV_MAT_ELEM( *Mxdd, float, 1, 2 ) = 8.f;
CV_MAT_ELEM( *Mxdd, float, 1, 3 ) = 9.f;
for (int i=0; i 2; i++ )
{
for (int j=0; j 4; j++)
printf("%f\n", ((float*)(Mxdd->data.ptr + Mxdd->step*i))[j]);
}

2. create matrix directly:
CvMat* Mru = cvCreateMat(3,3,CV_32FC1);
The values inside the matrix:
((float*)(Mru->data.ptr + Mru->step*1))[i]

3. read from the matrix elements:
float xdd[] = {0,1,2,3,4,5,6,7};
CvMat Mxdd = cvMat(2,4,CV_32FC1, xdd);
for (int i=0; i 2; i++ )
{
for (int j=0; j 4; j++)
printf("%f\n", ((float*)(Mxdd.data.ptr + Mxdd.step*i))[j]);
}

4. write to matrix element:
float x[] = {0,1,2,3,4,5,6,7};
for (int i=0; i<2; i++ )
for (int j=0; j<4; j++)
CV_MAT_ELEM( *Mxdd, float, i, j ) = x[4*i + j];
//x is a 2x4 matrix

4. Operation:
CvMat *Mxd_tmp = cvCloneMat(&Mxd); // clone
cvSubS( Mxu2_tmp, cvScalarAll(cx*cols), Mxu2_tmp); // sub scale
cvConvertScale( Mxu2_tmp, Mxu2_tmp, 1.0/s); // mulit or div scale
cvPow( &Mru, &Mru, 1./2 ); // sqrt or square
cvEigenVV( Ma_temp, Mrts, Mevals,DBL_EPSILON); //eigenvalue, size are (3,3),(3,3),(3,1) respectively.