22 void to(
const std::string &fileName,
const Mat &m)
24 FILE *pFile = fopen(fileName.c_str(),
"wb");
28 int depth = m.depth();
29 int channels = m.channels();
31 fwrite(&height,
sizeof(
int), 1, pFile);
32 fwrite(&width,
sizeof(
int), 1, pFile);
33 fwrite(&depth,
sizeof(
int), 1, pFile);
34 fwrite(&channels,
sizeof(
int), 1, pFile);
38 case CV_8U: elementSize = 1;
break;
39 case CV_8S: elementSize = 1;
break;
40 case CV_16U: elementSize = 2;
break;
41 case CV_16S: elementSize = 2;
break;
42 case CV_32S: elementSize = 4;
break;
43 case CV_32F: elementSize = 4;
break;
44 case CV_64F: elementSize = 8;
break;
46 DGM_WARNING(
"Custom matrix type is not supported");
51 fwrite(m.data, elementSize, height * width * channels, pFile);
59 Mat
from(
const std::string &fileName)
61 FILE *pFile = fopen(fileName.c_str(),
"rb");
62 if (!pFile)
return Mat();
65 int height, width, depth, channels;
67 fread(&height,
sizeof(
int), 1, pFile);
68 fread(&width,
sizeof(
int), 1, pFile);
69 fread(&depth,
sizeof(
int), 1, pFile);
70 fread(&channels,
sizeof(
int), 1, pFile);
74 case CV_8U: elementSize = 1;
break;
75 case CV_8S: elementSize = 1;
break;
76 case CV_16U: elementSize = 2;
break;
77 case CV_16S: elementSize = 2;
break;
78 case CV_32S: elementSize = 4;
break;
79 case CV_32F: elementSize = 4;
break;
80 case CV_64F: elementSize = 8;
break;
82 DGM_WARNING(
"Custom matrix type is not supported");
87 Mat res(height, width, CV_MAKETYPE(depth, channels));
88 fread(res.data, elementSize, height * width * channels, pFile);
void to(const std::string &fileName, const Mat &m)
Saves matrix m into the file fileName.
Mat from(const std::string &fileName)
Loads the matrix from file filename.