22#ifndef MATCEREALISATION_HPP_
23#define MATCEREALISATION_HPP_
25#include "cereal/cereal.hpp"
27#include "opencv2/core/core.hpp"
48template<class Archive, cereal::traits::DisableIf<cereal::traits::is_text_archive<Archive>::value>
49 = cereal::traits::sfinae>
50void save(Archive& ar,
const cv::Mat& mat)
58 continuous = mat.isContinuous();
60 ar & rows & cols & type & continuous;
63 const int data_size = rows * cols *
static_cast<int>(mat.elemSize());
64 auto mat_data = cereal::binary_data(mat.ptr(), data_size);
68 const int row_size = cols *
static_cast<int>(mat.elemSize());
69 for (
int i = 0; i < rows; i++) {
70 auto row_data = cereal::binary_data(mat.ptr(i), row_size);
84template<class Archive, cereal::traits::DisableIf<cereal::traits::is_text_archive<Archive>::value>
85 = cereal::traits::sfinae>
86void load(Archive& ar, cv::Mat& mat)
91 ar & rows & cols & type & continuous;
94 mat.create(rows, cols, type);
95 const int data_size = rows * cols *
static_cast<int>(mat.elemSize());
96 auto mat_data = cereal::binary_data(mat.ptr(), data_size);
100 mat.create(rows, cols, type);
101 const int row_size = cols *
static_cast<int>(mat.elemSize());
102 for (
int i = 0; i < rows; i++) {
103 auto row_data = cereal::binary_data(mat.ptr(i), row_size);
126template <
class Archive,
127 cereal::traits::EnableIf<cereal::traits::is_text_archive<Archive>::value>
128 = cereal::traits::sfinae>
129 void save(Archive& ar,
const cv::Mat& mat)
131 int rows, cols, type;
137 continuous = mat.isContinuous();
140 assert(mat.dims == 2);
147 std::vector<std::vector<float>> mat_data;
148 for (
int i = 0; i < rows; i++) {
149 Mat this_row = mat.row(i);
150 mat_data.push_back(std::vector<float>(this_row.begin<
float>(), this_row.end<
float>()));
152 ar & cereal::make_nvp(
"data", mat_data);
155 const int row_size = cols *
static_cast<int>(mat.elemSize());
156 for (
int i = 0; i < rows; i++) {
175template <
class Archive,
176 cereal::traits::EnableIf<cereal::traits::is_text_archive<Archive>::value>
177 = cereal::traits::sfinae>
178 void load(Archive& ar, cv::Mat& mat)
185 std::vector<std::vector<float>> mat_data;
187 assert(mat_data.size() > 0);
188 int rows =
static_cast<int>(mat_data.size());
189 int cols =
static_cast<int>(mat_data[0].size());
190 mat.create(rows, cols, CV_32FC1);
191 for (
int r = 0; r < rows; ++r) {
192 for (
int c = 0; c < cols; ++c) {
193 mat.at<
float>(r, c) = mat_data[r][c];
217template <
class Archive>
Serialisation of OpenCV cv::Mat matrices for the serialisation library cereal (http://uscilab....
Definition: mat_cerealisation.hpp:38
void serialize(Archive &ar, cv::Vec2f &vec)
Serialisation of a cv::Vec2f using cereal.
Definition: mat_cerealisation.hpp:218
void save(Archive &ar, const cv::Mat &mat)
Serialise a cv::Mat using cereal.
Definition: mat_cerealisation.hpp:50
void load(Archive &ar, cv::Mat &mat)
De-serialise a cv::Mat using cereal.
Definition: mat_cerealisation.hpp:86