Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

serializing each object alone, then I deserializing each object respectively by adding these functions

void matwrite(ofstream& fs, const Mat& mat, int index, bool checking){

    // Data Object
    int indexFrame = index;
    bool found = checking;
    fs.write((char*)&indexFrame, sizeof(int));    // indexFrame
    fs.write((char*)&found, sizeof(bool));    // bool checking

    // Header
    int type = mat.type();
    int channels = mat.channels();
    fs.write((char*)&mat.rows, sizeof(int));    // rows
    fs.write((char*)&mat.cols, sizeof(int));    // cols
    fs.write((char*)&type, sizeof(int));        // type
    fs.write((char*)&channels, sizeof(int));    // channels

    // Data
    if (mat.isContinuous())
    {
        fs.write(mat.ptr<char>(0), (mat.dataend - mat.datastart));
    }
    else
    {
        int rowsz = CV_ELEM_SIZE(type) * mat.cols;
        for (int r = 0; r < mat.rows; ++r)
        {
            fs.write(mat.ptr<char>(r), rowsz);
        }
    }
}   

ColletorMat matread(ifstream& fs){

    // Data Object
    int indexFrame;
    bool found;
    fs.read((char*)&indexFrame, sizeof(int));     //
    fs.read((char*)&found, sizeof(bool));         //

    // Header
    int rows, cols, type, channels;
    fs.read((char*)&rows, sizeof(int));         // rows
    fs.read((char*)&cols, sizeof(int));         // cols
    fs.read((char*)&type, sizeof(int));         // type
    fs.read((char*)&channels, sizeof(int));     // channels

    // Data
    Mat mat(rows, cols, type);
    fs.read((char*)mat.data, CV_ELEM_SIZE(type) * rows * cols);

    ColletorMat ojbectMat(indexFrame, found, mat);
    return ojbectMat;
}