void TestOpenCV( void ) { // a为输入对称方阵 double a[ 5 ][ 5 ] = { { 10.0 , 1.0 , 2.0 , 3.0 , 4.0 }, { 1.0 , 9.0 , - 1.0 , 2.0 , - 3.0 }, { 2.0 , - 1.0 , 7.0 , 3.0 , - 5.0 }, { 3.0 , 2.0 , 3.0 , 12.0 , - 1.0 }, { 4.0 , - 3.0 , - 5.0 , - 1.0 , 15.0 } }; // 构造输入方阵 CvMat SrcMatrix = cvMat( 5 , 5 , CV_64FC1,a); double b[ 5 ][ 5 ] = { { 0.0 , 0.0 , 0.0 , 0.0 , 0.0 }, { 0.0 , 0.0 , 0.0 , 0.0 , 0.0 }, { 0.0 , 0.0 , 0.0 , 0.0 , 0.0 }, { 0.0 , 0.0 , 0.0 , 0.0 , 0.0 }, { 0.0 , 0.0 , 0.0 , 0.0 , 0.0 } }; // 构造输出特征向量矩阵 CvMat ProVector = cvMat( 5 , 5 , CV_64FC1, b); // 构造输出特征值矩阵 double c[ 5 ] = { 0.0 , 0.0 , 0.0 , 0.0 , 0.0 }; CvMat ProValue = cvMat( 5 , 1 , CV_64FC1, c); cvEigenVV( & SrcMatrix, & ProVector, & ProValue, 1.0e-6F ); printf( " /n " ); int i = 0 ; int j = 0 ; for (i = 0 ;i < 5 ;i ++ ) { for (j = 0 ;j < 5 ;j ++ ) printf( " %f/t " ,cvmGet( & ProVector,i,j)); printf( " /n " ); } for (i = 0 ;i < 5 ;i ++ ) { printf( " %f/n " ,cvmGet( & ProValue,i, 0 )); } }
double ** pfMatrix = new double * [nInBandNum]; double ** pfVector = new double * [nInBandNum]; double * pProValue = new double [nInBandNum]; for (i = 0 ;i < nInBandNum;i ++ ) { pfMatrix[i] = new double [nInBandNum]; pfVector[i] = new double [nInBandNum]; pProValue[i] = 0.0 ; } // 初始化参数 for (i = 0 ;i < nInBandNum;i ++ ) { for (j = 0 ;j < nInBandNum;j ++ ) { pfMatrix[i][j] = 0.0 ; pfVector[i][j] = 0.0 ; } } // 对输入对称方阵进行赋值,这一步放过 // 下面是构造输入方阵、特征向量矩阵和特征值矩阵 CvMat * pSrcMatrix = cvCreateMat(nBandNum, nBandNum, CV_64FC1); CvMat * pProVector = cvCreateMat(nBandNum, nBandNum, CV_64FC1); CvMat * pProValue = cvCreateMat(nBandNum, 1 , CV_64FC1); for (i = 0 ;i < nBandNum;i ++ ) { cvmSet(pProValue,i, 0 ,pfProVector[i]); for (j = 0 ;j < nBandNum;j ++ ) { cvmSet(pSrcMatrix,i,j,pfMatrix[i][j]); cvmSet(pProVector,i,j,pfVector[i][j]); } } cvEigenVV(pSrcMatrix,pProVector,pProValue); // 释放矩阵所占内存空间 cvReleaseMat( & pSrcMatrix); cvReleaseMat( & pProVector); cvReleaseMat( & pProValue);