Ask Your Question

Revision history [back]

check the code below

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

#include <iostream>

using namespace cv;
using namespace std;


bool saveMats(const Mat m1, const Mat m2, String file_path)
{
    FileStorage fs(file_path, FileStorage::WRITE);

    fs << "name" << "object1";
    fs << "object1" << m1;
    fs << "object2" << m2;

    return true;
}


int main()
{

    Mat m1(5, 5, CV_8UC1, Scalar(127));
    Mat m2(6, 6, CV_8UC1, Scalar(128));
    saveMats(m1, m2, "test.yml");

    FileStorage fs("test.yml", FileStorage::READ);
    FileNode fn = fs.root();

    Mat r1,r2;
    cv::read(fn["object1"], r1);
    cv::read(fn["object2"], r2);
    cout << r1 << endl;
    cout << r2;
    imshow("just for waiting", r1);
    waitKey();

    return 0;
}