I haven't found a working example of implementing PCA analysis using Opencv that suits my case, so I'm not sure what is wrong with my implementation, which follows:
void PreProcessor::performPca()
{ const int type = CV_64F; cv::Mat pt_mat = cv::Mat::zeros(numPatients, numFeatures, type); for (int y = 0; y < numPatients; y++) { for (int x = 0; x < numFeatures; x++) { pt_mat.at<double>(y,x) = features[y].at(x); } } cv::PCA pt_pca(pt_mat, cv::Mat(), CV_PCA_DATA_AS_ROW, numFeatures); cv::Mat dataprojected(pt_mat.rows, numFeatures, pt_mat.type()); for (int i = 0; i < pt_mat.rows; i++) { dataprojected.row(i) = pt_pca.project(pt_mat.row(i)); } features.clear(); for (int i =0; i< dataprojected.rows; i++) { std::vector<double> pcaFeat = dataprojected.row(i); features.insert(std::pair<int, std::vector<double> >(i, pcaFeat)); }
}
This member function attempts to find the principal components of features
, which is already filled and is declared as std::map<int,std::vector<double>> features
. At the end it refills it with the principal components. NumPatients
is the number of vectors in features
and numFeatures
is the number of elements in a vector. The above code debugs and runs, but the resulting features
is mostly filled with zeros. Is there something wrong with my understanding of how PCA of Opencv works?