1 | initial version |
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, f);
fclose(f2);
}
2 | No.2 Revision |
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, f);
f2);
fclose(f2);
}