Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

if you want to use PCA to "compress" your train data set, you will need a single Mat with each image flattened to a row:

Mat td;
for (image : trainset) {
    image.convertTo(image, CV_32F); // we need float data
    td.push_back(image.reshape(1,1)); // flat row 
}

now you can build the PCA:

int num_components = 500; // how many features to retain    
PCA pca(td, Mat(), cv::PCA::DATA_AS_ROW, num_components);

this will take a while, after it finished, you can project your train data to PCA space (this is, where the actual reduction happens !)

Mat trainData; // for ml, now
for (image : trainset) {
    image.convertTo(image, CV_32F); // we need float data
    Mat projected = pca.project(image.reshape(1,1)); // flat row 
    trainData.push_back(projected); // reduced to num_components features
}

later, for testing, you'll do the same reduction, so you have to keeep the "trained" PCA object around:

image.convertTo(image, CV_32F); // we need float data
Mat projected = pca.project(image.reshape(1,1)); // flat row, reduced to num_components  
ml->predict(projected);

if you want to use PCA to "compress" your train data set, you will need a single Mat with each image flattened to a row:row. all images need to be the same size here(also i'd suggest, you crop and resize an image like above to something smaller, like 150x150, else your PCA will take ages !):

Mat td;
for (image : trainset) {
    image.convertTo(image, CV_32F); // we need float data
    td.push_back(image.reshape(1,1)); // flat row 
}

now you can build the PCA:

int num_components = 500; // how many features to retain    
PCA pca(td, Mat(), cv::PCA::DATA_AS_ROW, num_components);

this will take a while, after it finished, you can project your train data to PCA space (this is, where the actual reduction happens !)

Mat trainData; // for ml, now
for (image : trainset) {
    image.convertTo(image, CV_32F); // we need float data
    Mat projected = pca.project(image.reshape(1,1)); // flat row 
    trainData.push_back(projected); // reduced to num_components features
}

later, for testing, you'll do the same reduction, so you have to keeep the "trained" PCA object around:

image.convertTo(image, CV_32F); // we need float data
Mat projected = pca.project(image.reshape(1,1)); // flat row, reduced to num_components  
ml->predict(projected);