I am trying to load a matrix from .yaml file but opencv gives me the following error :
OpenCV Error: Parsing Error (myFile.yaml(1): valid xml should start with ') OpenCV Error: Parsing Error (myFile.yaml(1): Tag should start with '<'> in unknown function)
this is my Write into Storage file, whcih works fine :
cv::FileStorage fs("myFile.yaml", cv::FileStorage::APPEND);
while(counter<_imgPtrVector.size()){
unsigned char* _pointer=(unsigned char*)_imgPtrVector.at(counter);
cv::Mat _matrixImage(cv::Size( width,height), CV_8UC1,_pointer , cv::Mat::AUTO_STEP);
fs <<"Matrix"<<_matrixImage;
counter++;
}
but when I want to load the data from the same file I got those errors; this is the code for Reading from storage file :
cv::FileStorage f("myFile.yaml", cv::FileStorage::READ );
cv::Mat mat(cv::Size( width,height), CV_8UC1);
if(f.isOpened()){
cv::FileNode n = f["Matrix"];
if (n.type() != cv::FileNode::SEQ){
std::cout << "error!";
}
f["Matrix"] >> mat;
}
The problem might be that you are always appending to an existing file. So you might have to change your code to:
FileStorage fs("test.yml", FileStorage::WRITE);
This will recreate the file each time your program runs.
The OpenCV docs have an example on how to write with XML/YAML Persistence that is pretty clear:
#include "opencv2/opencv.hpp"
#include <time.h>
using namespace cv;
int main(int, char** argv)
{
FileStorage fs("test.yml", FileStorage::WRITE);
fs << "frameCount" << 5;
time_t rawtime; time(&rawtime);
fs << "calibrationDate" << asctime(localtime(&rawtime));
Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
Mat distCoeffs = (Mat_<double>(5,1) << 0.1, 0.01, -0.001, 0, 0);
fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
fs << "features" << "[";
for( int i = 0; i < 3; i++ )
{
int x = rand() % 640;
int y = rand() % 480;
uchar lbp = rand() % 256;
fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
for( int j = 0; j < 8; j++ )
fs << ((lbp >> j) & 1);
fs << "]" << "}";
}
fs << "]";
fs.release();
return 0;
}
And there is another example that shows how to read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With