Serializing OpenCV Mat_

前端 未结 7 1201
刺人心
刺人心 2020-11-30 05:29

I\'m working on a robotics research project where I need to serialize 2D matrices of 3D points: basically each pixel is a 3-vector of floats. These pixels are saved in an Op

7条回答
  •  死守一世寂寞
    2020-11-30 05:56

    I wrote this code:

    /*
    Will save in the file:
    cols\n
    rows\n
    elemSize\n
    type\n
    DATA
    */
    void serializeMatbin(Mat& mat, std::string filename){
        if (!mat.isContinuous()) {
            cout << "Not implemented yet" << endl;
            exit(1);
        }
        int elemSizeInBytes = (int)mat.elemSize();
        int elemType        = (int)mat.type();
        int dataSize        = (int)(mat.cols * mat.rows * mat.elemSize());
    
        FILE* FP = fopen(filename.c_str(), "wb");
        int sizeImg[4] = {mat.cols, mat.rows, elemSizeInBytes, elemType };
        fwrite(/*buffer*/ sizeImg, /*howmanyelements*/ 4, /* size of each element */ sizeof(int), /*file*/ FP);
        fwrite(mat.data, mat.cols * mat.rows, elemSizeInBytes, FP);
        fclose(FP);
    }
    
    Mat deserializeMatbin(std::string filename){
        FILE* fp = fopen(filename.c_str(), "r");
        int header[4];
        fread(header, sizeof(int), 4, fp);
        int cols = header[0]; 
        int rows = header[1];
        int elemSizeInBytes = header[2];
        int elemType = header[3];
    
        Mat outputMat = Mat(rows, cols, elemType);
    
        fread(outputMat.data, elemSizeInBytes, cols * rows, fp);
        fclose(fp);
        return outputMat;
    }
    
    void testSerializeMatbin(){
        Mat a = Mat::ones(/*cols*/ 10, /* rows */ 5, CV_8U) * 2;
        std::string filename = "test.matbin";
        serializeMatbin(a, filename);
        Mat b = deserializeMatbin(filename);
        cout << "Rows: " << b.rows << " Cols: " << b.cols << " type: " << b.type()<< endl;
    }
    

提交回复
热议问题