- (void)test
{
float a[] = {
1.5,2.3,
3.0,1.7,
1.2,2.9,
2.1,2.2,
3.1,3.1,
1.3,2.7,
2.0,1.7,
1.0,2.0,
0.5,0.6,
1.0,0.1
};
const int rows = 10, cols = 2;
CvMat* mat = cvCreateMat(rows, cols, CV_32FC1);
cvSetData(mat, a, mat->step);
// std::cout << "original matrix: " << std::endl;
PrintMatrix(mat, rows, cols);
// std::cout << "================================" << std::endl;
const int dim = 2; // dimension
CvMat* avg2 = cvCreateMat(1, cols, CV_32FC1);
CvMat* eigenVector2 = cvCreateMat(dim, cols, CV_32FC1);
CvMat* eigenValue2 = cvCreateMat(dim, 1, CV_32FC1);
// CvMat* vector_pca=cvCreateMat(dim, 3, CV_32FC1);
cvCalcPCA(mat, avg2, eigenValue2, eigenVector2, CV_PCA_DATA_AS_ROW);
// cvProjectPCA(mat,avg2,eigenVector2,vector_pca);
// std::cout << "average2: " << std::endl;
PrintMatrix(avg2, 1, cols);
// std::cout << "eigenVector2: " << std::endl;
PrintMatrix(eigenVector2, dim, cols);
// std::cout << "eigenValues2: " << std::endl;
PrintMatrix(eigenValue2, dim, 1);
// std::cout << "================================" << std::endl;
// PrintMatrix(vector_pca, dim, 3);
}
下面代码用来把RGBA的图像,转换成CvMat类型,用来进行特征值计算。
const int rows = 100, cols = 100;
IplImage *img_src = Image1;
IplImage *img_resize = cvCreateImage(cvSize(rows,cols), IPL_DEPTH_8U, 3);
cvResize(img_src, img_resize, CV_INTER_LINEAR);
IplImage *img_gray = cvCreateImage(cvSize(rows,cols), IPL_DEPTH_8U, 1);
cvCvtColor(img_resize, img_gray, CV_RGB2GRAY);
CvMat *mat = cvCreateMat(rows, cols, CV_32FC1);
cvConvert(img_gray, mat); //得到转换成100*100维图片后转换成矩阵;