我想在OpenCV中創建一個PCA模型來保存像素座標。作爲一個實驗,我有兩組像素座標映射出兩個近似圓。每組座標都有48個x,y對。我正在試驗下面的代碼,它從文件讀取座標並將它們存儲在Mat結構中。但是,我認爲這是不正確的,openCV中的PCA似乎在互聯網上很差。OpenCV PCA問題
Mat m(2, 48, CV_32FC2); // matrix with 2 rows of 48 cols of floats held in two channels
pFile = fopen("data.txt", "r");
for (int i=0; i<48; i++){
int x, y;
fscanf(pFile, "%d%c%c%d%c", &x, &c, &c, &y, &c);
m.at<Vec2f>(0 , i)[0] = (float)x; // store x in row 0, col i in channel 0
m.at<Vec2f>(0 , i)[1] = (float)y; // store y in row 0, col i in channel 1
}
for (int i=0; i<48; i++){
int x, y;
fscanf(pFile, "%d%c%c%d%c", &x, &c, &c, &y, &c);
m.at<Vec2f>(1 , i)[0] = (float)x; // store x in row 1, col i in channel 0
m.at<Vec2f>(1 , i)[1] = (float)y; // store y in row 1, col i in channel 1
}
PCA pca(m, Mat(), CV_PCA_DATA_AS_ROW, 2); // 2 principle components??? Not sure what to put here e.g. is it 2 for two data sets or 48 for number of elements?
for (int i=0; i<48; i++){
float x = pca.mean.at<Vec2f>(i,0)[0]; //get average x
float y = pca.mean.at<Vec2f>(i,0)[1]; //get average y
printf("\n x=%f, y=%f", x, y);
}
但是,這在創建pca對象時崩潰。我知道這是一個非常基本的問題,但我有點失落,並希望有人能讓我開始在開放的簡歷pca。
您是否看過[OpenCV文檔的PCA部分](http://opencv.itseez.com/modules/core/doc/operations_on_arrays.html#pca)? –