1 | initial version |
I wrote this short 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; }