Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

why serialized vector of Mat won't work?

hello,

I've written this code which opens tree images and push them with some infos inside vector<ColletorMat> setFrames then I save this vector of Mat within a binary file. so as to load it later and open these images again. however, I didn't get the aim

any will be appreciated

#include <opencv2\opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include "opencv2/core/core.hpp"

#include <iostream>
#include <fstream>

using namespace std;
using namespace cv;

class ColletorMat
{
private:
    int indexFrame;
    bool found;
    Mat frame;

public:

    ColletorMat(int index, bool found, Mat frame)
    {
        this->indexFrame = index;
        this->found = found;
        this->frame = frame;
    }

    ~ColletorMat()
    {

    }

    // settors
    void set_indexFrame(int index)
    {
        this->indexFrame = index;
    }

    void set_found(bool found)
    {
        this->found = found;
    }

    void set_frame(Mat frame)
    {
        this->frame = frame;
    }

    // accessors
    int get_indexFrame()
    {
        return this->indexFrame;
    }

    bool get_found()
    {
        return this->found;
    }

    Mat get_frame()
    {
        return this->frame;
    }

};

int main()
{
    // Save the random generated data
    {
        Mat image1, image2, image3;
        image1 = imread("C:\\opencvVid\\data_seq\\Human3\\0001.jpg");
        image2 = imread("C:\\opencvVid\\data_seq\\Human3\\0002.jpg");
        image3 = imread("C:\\opencvVid\\data_seq\\Human3\\0003.jpg");

        if (image1.empty() || image2.empty() || image3.empty()) {
            std::cout << "error: image not readed from file\n";
            return(0);
        }

        imshow("M1",image1);
        imshow("M2",image2);
        imshow("M3",image3);

        (char)cvWaitKey(0);

        vector<ColletorMat> setFrames;

        ColletorMat colletorMat1(1,false,image1.clone());
        ColletorMat colletorMat2(1,false,image2.clone());
        ColletorMat colletorMat3(1,false,image3.clone());

        setFrames.push_back(colletorMat1);
        setFrames.push_back(colletorMat2);
        setFrames.push_back(colletorMat3);

        ofstream fs("setFrms.bin", fstream::binary);

        fs.write((char *) &setFrames, sizeof(vector<ColletorMat>));
        fs.close();

        ifstream loadFs("setFrms.bin", ios::binary);

        if(!loadFs.is_open()){
            cout << "error while opening the binary file" << endl;
        }

        vector<ColletorMat> setFramesLoad;

        loadFs.read( (char *) &setFramesLoad, sizeof(vector<ColletorMat>));
        cout << "frames loaded up " << setFramesLoad.size() << endl;

        imshow("1",setFramesLoad.at(0).get_frame());
        imshow("2",setFramesLoad.at(1).get_frame());
        imshow("3",setFramesLoad.at(2).get_frame());

        loadFs.close();

    }

    return 0;
}

why serialized vector of Mat won't work?

hello,

I've written this code which opens tree images and push them with some infos inside vector<ColletorMat> setFrames then I save this vector of Mat within a binary file. so as to load it later and open these images again. however, I didn't get the aim

any will be appreciated

#include <opencv2\opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include "opencv2/core/core.hpp"

#include <iostream>
#include <fstream>

using namespace std;
using namespace cv;

class ColletorMat
{
private:
    int indexFrame;
    bool found;
    Mat frame;

public:

    ColletorMat(int index, bool found, Mat frame)
    {
        this->indexFrame = index;
        this->found = found;
        this->frame = frame;
    }

    ~ColletorMat()
    {

    }

    // settors
    void set_indexFrame(int index)
    {
        this->indexFrame = index;
    }

    void set_found(bool found)
    {
        this->found = found;
    }

    void set_frame(Mat frame)
    {
        this->frame = frame;
    }

    // accessors
    int get_indexFrame()
    {
        return this->indexFrame;
    }

    bool get_found()
    {
        return this->found;
    }

    Mat get_frame()
    {
        return this->frame;
    }

};

int main()
{
    // Save the random generated data
     {
        Mat image1, image2, image3;
        image1 = imread("C:\\opencvVid\\data_seq\\Human3\\0001.jpg");
        image2 = imread("C:\\opencvVid\\data_seq\\Human3\\0002.jpg");
        image3 = imread("C:\\opencvVid\\data_seq\\Human3\\0003.jpg");

        if (image1.empty() || image2.empty() || image3.empty()) {
            std::cout << "error: image not readed from file\n";
            return(0);
        }

        imshow("M1",image1);
        imshow("M2",image2);
        imshow("M3",image3);

        (char)cvWaitKey(0);

        vector<ColletorMat> setFrames;

        ColletorMat colletorMat1(1,false,image1.clone());
        ColletorMat colletorMat2(1,false,image2.clone());
        ColletorMat colletorMat3(1,false,image3.clone());

        setFrames.push_back(colletorMat1);
        setFrames.push_back(colletorMat2);
        setFrames.push_back(colletorMat3);

        ofstream fs("setFrms.bin", fstream::binary);

        fs.write((char *) &setFrames, sizeof(vector<ColletorMat>));
        fs.close();

        ifstream loadFs("setFrms.bin", ios::binary);

        if(!loadFs.is_open()){
            cout << "error while opening the binary file" << endl;
        }

        vector<ColletorMat> setFramesLoad;

        loadFs.read( (char *) &setFramesLoad, sizeof(vector<ColletorMat>));
        cout << "frames loaded up " << setFramesLoad.size() << endl;

        imshow("1",setFramesLoad.at(0).get_frame());
        imshow("2",setFramesLoad.at(1).get_frame());
        imshow("3",setFramesLoad.at(2).get_frame());

        loadFs.close();

    }

    return 0;
}