Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Using OpenCV's pca write and read for saving PCA data

I am using OpenCV's function for PCA analysis. I want to save and read back the PCA structure using the provided functions. The PCA analysis part is OK, and the function serializes the object to the output file OK. The problem is I cannot read back the serialized object. My code is as follows,

from the main function:

Mat_<double> C = (Mat_<double>(2,6) << 1, 0, 3, 2, 1, 5, 6, 2, 0, 4, 0, 9);
doPCA(vec);

The doPCA function:

string file_name = "/media/femkha/HDB_FemkhaAcer/images2/cplus/pca_data";
const int MAX_COMPONENTS = 2;
{
    FileStorage fs(file_name,FileStorage::WRITE);
    PCA pca(vec,Mat(),PCA::DATA_AS_ROW,MAX_COMPONENTS);
    pca.write(fs);
    fs.release();
}
{
    FileStorage fs2; 
    fs2.open(file_name,FileStorage::READ);
    FileNode fn = fs2["PCA"];
}

The generated file is as follows: generated PCA

Using OpenCV's pca write and read for saving PCA data

I am using OpenCV's function for PCA analysis. I want to save and read back the PCA structure using the provided functions. The PCA analysis part is OK, and the function serializes the object to the output file OK. The problem is I cannot read back the serialized object. My code is as follows,

from the main function:

Mat_<double> C = (Mat_<double>(2,6) << 1, 0, 3, 2, 1, 5, 6, 2, 0, 4, 0, 9);
doPCA(vec);

The doPCA function:

string file_name = "/media/femkha/HDB_FemkhaAcer/images2/cplus/pca_data";
const int MAX_COMPONENTS = 2;
{
    FileStorage fs(file_name,FileStorage::WRITE);
    PCA pca(vec,Mat(),PCA::DATA_AS_ROW,MAX_COMPONENTS);
    pca.write(fs);
    fs.release();
}
{
    FileStorage fs2; 
    fs2.open(file_name,FileStorage::READ);
    FileNode fn = fs2["PCA"];
}

The generated file is as follows: generated PCA

%YAML:1.0
name: PCA
vectors: !!opencv-matrix
   rows: 2
   cols: 6
   dt: d
   data: [ 6.5094455490411940e-01, 2.6037782196164777e-01,
       -3.9056673294247168e-01, 2.6037782196164777e-01,
       -1.3018891098082389e-01, 5.2075564392329554e-01, 0., 0., 0., 0.,
       0., 0. ]
values: !!opencv-matrix
   rows: 2
   cols: 1
   dt: d
   data: [ 1.4750000000000000e+01, 0. ]
mean: !!opencv-matrix
   rows: 1
   cols: 6
   dt: d
   data: [ 3.5000000000000000e+00, 1., 1.5000000000000000e+00, 3.,
       5.0000000000000000e-01, 7. ]