We use fwrite
to write formated data to disk and use fread
to later read them back from disk. Different data types are very different in using fwrite/fread
. The following shows how to do for vector
(simple), Mat
, vector<Mat>
, struct
(simple), struct
(complex).
for vector
(simple):
fwrite
:1
2
3
4
5
6
7
8
9void write_vector(string file, vector<int> a)
{
// write data to file
FILE *fo;
fo = fopen(file.c_str(), "wb");
//size_element = result[0].elemSize()*result[0].total();
fwrite(&a[0], a.size(), sizeof(int), fo);
fclose(fo);
}fread
:1
2
3
4
5
6
7
8
9
10
11
12
13
14vector<int> load_vector(string file, int size)
{
vector<int> result;
result.resize(size);
// read data from file
FILE *fi;
fi = fopen(file.c_str(), "rb");
fread( &result[0], sizeof(int), result.size(), fi );
fclose(fi);
return result;
}Usage:
1
2
3
4
5
6vector<int> a;
a.push_back(0);
a.push_back(1);
a.push_back(2);
write_vector("vector.dat", a);
vector<int> b = load_vector("vector.dat", 3);
for Mat
:
fwrite
:1
2
3
4
5
6
7
8
9void write_mat(string file, Mat a)
{
// write data to file
FILE *fo;
fo = fopen(file.c_str(), "wb");
//size_element = result[0].elemSize()*result[0].total();
fwrite(a.ptr(), 1, (size_t)(a.elemSize()*a.total()), fo);
fclose(fo);
}fread
:1
2
3
4
5
6
7
8
9
10
11
12Mat load_mat(string file, int rows, int cols, int type)
{
Mat result(rows, cols, type);
// read data from file
FILE *fi;
fi = fopen(file.c_str(), "rb");
fread(result.ptr(), (size_t)(result.elemSize()*result.total()), 1, fi);
fclose(fi);
return result;
}Usage:
1
2
3Mat a = Mat::eye(3, 3, CV_32FC1);
write_mat("mat.dat", a);
Mat b = load_mat("mat.dat", 3, 3, CV_32FC1);
for vector<Mat>
(every Mat
have similar data):
fwrite
:1
2
3
4
5
6
7
8
9
10
11
12
13
14void write_all_images_content_to_disk(vector<Mat> result, string file, size_t & size_mat)
{
// write data to file
FILE *fo;
fo = fopen(file.c_str(), "wb");
for (int i=0; i<result.size(); i++)
{
Mat stub = result[i];
size_mat = (size_t)(stub.elemSize()*stub.total());
fwrite(stub.ptr(), size_mat, 1, fo);
}
fclose(fo);
}fread
:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20vector<Mat> load_images_content_from_disk(string file, size_t size_mat, int size)
{
vector<Mat> result;
result.resize(size);
// read data from file
FILE *fi;
fi = fopen(file.c_str(), "rb");
for (int i=0; i<size; i++)
{
Mat stub(WIDTH_IMAGE, HEIGHT_IMAGE, CV_8UC3);
fread(stub.ptr(), size_mat, 1, fi);
result.push_back(stub);
}
fclose(fi);
return result;
}Usage:
1
2
3
4// for vector<Mat> IMAGES_CONTENT;
size_t size_mat;
write_all_images_content_to_disk(IMAGES_CONTENT, "images.dat", size_mat);
vector<Mat> stub = load_images_content_from_disk("images.dat", size_mat, IMAGES_CONTENT.size());
for struct
(simple):
Take the following struct as an example:
1 | struct PERSON |
fwrite
:1
2
3
4
5
6
7
8void write_struct(string file, PERSON a)
{
// write data to file
FILE *fo;
fo = fopen(file.c_str(), "wb");
fwrite(&a, sizeof(struct PERSON), 1, fo);
fclose(fo);
}fread
:1
2
3
4
5
6
7
8void load_struct(string file, PERSON & result)
{
// read data from file
FILE *fi;
fi = fopen(file.c_str(), "rb");
fread(&result, sizeof(struct PERSON), 1, fi);
fclose(fi);
}Usage:
1
2
3
4
5
6
7PERSON a;
strcpy(a.name, "Tommy");
a.age = 25;
write_struct("person.dat", a);
PERSON b;
load_struct("person.dat", b);
for struct
(contains vector
and Mat
):
Take the following struct as an example:
1 | struct FEATURE_RECT |
fwrite
:1
2
3
4
5
6
7
8
9
10void write_struct(string file, FEATURE_RECT a)
{
// write data to file
FILE *fo;
fo = fopen(file.c_str(), "wb");
fwrite(&a, sizeof(struct FEATURE_RECT), 1, fo);
fwrite(&a.color_hist[0], sizeof(float), a.color_hist.size(), fo);
fwrite(&a.hog[0], sizeof(float), a.hog.size(), fo);
fclose(fo);
}fread
:1
2
3
4
5
6
7
8
9
10
11void load_struct(string file, FEATURE_RECT & result)
{
// read data from file
FILE *fi;
fi = fopen(file.c_str(), "rb");
fread(&result, sizeof(struct FEATURE_RECT ), 1, fi);
fread(&result.color_hist[0], sizeof(float), result.color_hist.size(), fi);
fread(&result.hog[0], sizeof(float), result.hog.size(), fi);
fclose(fi);
}Usage:
1
2
3
4
5
6
7
8
9FEATURE_RECT test;
test.rect = Rect(1,2,100,200);
test.color_hist.push_back(0);
test.hog.push_back(1);
test.region_mat = Mat::eye(3,3,CV_32FC1);
write_struct("struct.dat", test);
FEATURE_RECT test2;
load_struct("struct.dat", test2);