pca c++
1 #include <QtCore/QCoreApplication>
2 #include "cv.h"
3 #include "highgui.h"
4 #include <iostream>
5 #include <stdio.h>
6 #include <string.h>
7
8 using namespace std;
9
10 int main(int argc, char *argv[])
11 {
12 QCoreApplication a(argc, argv);
13 int start = 0;
14 float arr[360000];
15 for (int index = 1; index <= 10; index++) {
16 char t[3];
17 sprintf(t, "%d", index);
18 string f(t);
19 string fileName = "test\\" + f + ".jpg";
20
21 IplImage* pSrc = cvLoadImage( fileName.c_str(), 1);
22 IplImage* pGray = cvCreateImage(cvGetSize(pSrc),8,1);
23 cvCvtColor(pSrc, pGray, CV_RGB2GRAY);
24 CvMat* mat = cvCreateMat(pGray->height, pGray->width, CV_32FC1);
25 cvConvert(pGray, mat);
26
27 cvReleaseImage( &pSrc ); //释放图像
28 cvReleaseImage( &pGray ); //释放图像
29
30 for (int i=0; i<mat->rows;i++) {
31 for (int j=0; j<mat->cols;j++) {
32 float value = cvmGet(mat, i, j);
33 arr[start] = value;
34 start++;
35 }
36 }
37 }
38 CvMat data = cvMat(10, 36000, CV_32FC1, arr);
39 CvMat* pMean = cvCreateMat(1, 36000, CV_32FC1);
40 CvMat* pEigVals = cvCreateMat(1, 10, CV_32FC1);
41 CvMat* pEigVecs = cvCreateMat(10, 36000, CV_32FC1);
42
43 cvCalcPCA( &data, pMean, pEigVals, pEigVecs, CV_PCA_DATA_AS_ROW );
44 for (int i = 0; i<10; i++) {
45 cout << cvmGet(pEigVals, 0, i) << endl;
46 }
47
48 return a.exec();
49 }
50
2 #include "cv.h"
3 #include "highgui.h"
4 #include <iostream>
5 #include <stdio.h>
6 #include <string.h>
7
8 using namespace std;
9
10 int main(int argc, char *argv[])
11 {
12 QCoreApplication a(argc, argv);
13 int start = 0;
14 float arr[360000];
15 for (int index = 1; index <= 10; index++) {
16 char t[3];
17 sprintf(t, "%d", index);
18 string f(t);
19 string fileName = "test\\" + f + ".jpg";
20
21 IplImage* pSrc = cvLoadImage( fileName.c_str(), 1);
22 IplImage* pGray = cvCreateImage(cvGetSize(pSrc),8,1);
23 cvCvtColor(pSrc, pGray, CV_RGB2GRAY);
24 CvMat* mat = cvCreateMat(pGray->height, pGray->width, CV_32FC1);
25 cvConvert(pGray, mat);
26
27 cvReleaseImage( &pSrc ); //释放图像
28 cvReleaseImage( &pGray ); //释放图像
29
30 for (int i=0; i<mat->rows;i++) {
31 for (int j=0; j<mat->cols;j++) {
32 float value = cvmGet(mat, i, j);
33 arr[start] = value;
34 start++;
35 }
36 }
37 }
38 CvMat data = cvMat(10, 36000, CV_32FC1, arr);
39 CvMat* pMean = cvCreateMat(1, 36000, CV_32FC1);
40 CvMat* pEigVals = cvCreateMat(1, 10, CV_32FC1);
41 CvMat* pEigVecs = cvCreateMat(10, 36000, CV_32FC1);
42
43 cvCalcPCA( &data, pMean, pEigVals, pEigVecs, CV_PCA_DATA_AS_ROW );
44 for (int i = 0; i<10; i++) {
45 cout << cvmGet(pEigVals, 0, i) << endl;
46 }
47
48 return a.exec();
49 }
50