Ask Your Question
0

Opencv PCA Saving

asked 2015-05-12 10:31:45 -0600

Gabriel Cachia gravatar image

I am trying to save my PCA eigenvectors and mean for later projection (to predict values using a SVM), but the dimensions are huge and thus saving and loading of the PCA takes a long time. Is there an alternative to this?

Thank you!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-05-12 12:55:59 -0600

berak gravatar image

updated 2015-05-12 12:56:28 -0600

maybe this helps:

void read_pca(PCA &pca, FILE *f)
{
    int mr,mc;
    fread(&mr, sizeof(int), 1, f);
    fread(&mc, sizeof(int), 1, f);
    pca.mean.create(mr,mc,CV_32F);
    fread(pca.mean.ptr<float>(), mc*mr, 1, f);
    cerr << "mean" << pca.mean.size() << endl;

    int er, ec;
    fread(&er, sizeof(int), 1, f);
    fread(&ec, sizeof(int), 1, f);
    pca.eigenvectors.create(er,ec,CV_32F);
    fread(pca.eigenvectors.ptr<float>(), 1, ec*er, f);
}
void write_pca(PCA &pca, FILE *f)
{
    int mr = pca.mean.rows;
    fwrite(&mr, sizeof(int), 1, f);
    int mc = pca.mean.cols;
    fwrite(&mc, sizeof(int), 1, f);
    fwrite(pca.mean.ptr<float>(), mc*mr, 1, f);
    fflush(f);

    // will have to transpose later !
    int er = pca.eigenvectors.rows;
    fwrite(&er, sizeof(int), 1, f);
    int ec = pca.eigenvectors.cols;
    fwrite(&ec, sizeof(int), 1, f);
    fwrite(pca.eigenvectors.ptr<float>(), 1, ec*er, f);
}
int main( int argc, char *argv[] )
{ 
    PCA pca;
    FILE * f = fopen("some.pca.bin","wb");
    write_pca(pca, f);
    fclose(f);
    // ...
    FILE * f2 = fopen("some.pca.bin","rb");
    read_pca(pca, f2);
    fclose(f2);
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-05-12 10:31:45 -0600

Seen: 426 times

Last updated: May 12 '15