从opencv3.4.9中摘取。
1 static void icvWriteMat( CvFileStorage* fs, const char* name, const void* struct_ptr, CvAttrList /*attr*/ ) 2 { 3 const CvMat* mat = (const CvMat*)struct_ptr; 4 char dt[16]; 5 cv::Size size; 6 int y; 7 8 assert( CV_IS_MAT_HDR_Z(mat) ); 9 10 cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_MAT ); 11 cvWriteInt( fs, "rows", mat->rows ); 12 cvWriteInt( fs, "cols", mat->cols ); 13 cvWriteString( fs, "dt", icvEncodeFormat( CV_MAT_TYPE(mat->type), dt ), 0 ); 14 cvStartWriteStruct( fs, "data", CV_NODE_SEQ + CV_NODE_FLOW ); 15 16 size = cvGetSize(mat); 17 if( size.height > 0 && size.width > 0 && mat->data.ptr ) 18 { 19 if( CV_IS_MAT_CONT(mat->type) ) 20 { 21 size.width *= size.height; 22 size.height = 1; 23 } 24 25 for( y = 0; y < size.height; y++ ) 26 cvWriteRawData( fs, mat->data.ptr + (size_t)y*mat->step, size.width, dt ); 27 } 28 cvEndWriteStruct( fs ); 29 cvEndWriteStruct( fs ); 30 }