superviseddescent  0.4.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
mat_cerealisation.hpp
1 /*
2  * superviseddescent: A C++11 implementation of the supervised descent
3  * optimisation method
4  * File: superviseddescent/matcerealisation.hpp
5  *
6  * Copyright 2015 Patrik Huber
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 #pragma once
21 
22 #ifndef MATCEREALISATION_HPP_
23 #define MATCEREALISATION_HPP_
24 
25 #include "opencv2/core/core.hpp"
26 
32 namespace cv {
33 
42 template<class Archive>
43 void save(Archive& ar, const cv::Mat& mat)
44 {
45  int rows, cols, type;
46  bool continuous;
47 
48  rows = mat.rows;
49  cols = mat.cols;
50  type = mat.type();
51  continuous = mat.isContinuous();
52 
53  ar & rows & cols & type & continuous;
54 
55  if (continuous) {
56  const int data_size = rows * cols * static_cast<int>(mat.elemSize());
57  auto mat_data = cereal::binary_data(mat.ptr(), data_size);
58  ar & mat_data;
59  }
60  else {
61  const int row_size = cols * static_cast<int>(mat.elemSize());
62  for (int i = 0; i < rows; i++) {
63  auto row_data = cereal::binary_data(mat.ptr(i), row_size);
64  ar & row_data;
65  }
66  }
67 };
68 
77 template<class Archive>
78 void load(Archive& ar, cv::Mat& mat)
79 {
80  int rows, cols, type;
81  bool continuous;
82 
83  ar & rows & cols & type & continuous;
84 
85  if (continuous) {
86  mat.create(rows, cols, type);
87  const int data_size = rows * cols * static_cast<int>(mat.elemSize());
88  auto mat_data = cereal::binary_data(mat.ptr(), data_size);
89  ar & mat_data;
90  }
91  else {
92  mat.create(rows, cols, type);
93  const int row_size = cols * static_cast<int>(mat.elemSize());
94  for (int i = 0; i < rows; i++) {
95  auto row_data = cereal::binary_data(mat.ptr(i), row_size);
96  ar & row_data;
97  }
98  }
99 };
100 
101 }
102 
103 #endif /* MATCEREALISATION_HPP_ */
void save(Archive &ar, const cv::Mat &mat)
Definition: mat_cerealisation.hpp:43
void load(Archive &ar, cv::Mat &mat)
Definition: mat_cerealisation.hpp:78