/* * File: matserialisation.hpp * * Copyright 2014, 2015 Patrik Huber * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #ifndef MATSERIALISATION_HPP_ #define MATSERIALISATION_HPP_ #include "opencv2/core/core.hpp" #include "boost/serialization/serialization.hpp" #include "boost/serialization/binary_object.hpp" /** * Serialisation for the OpenCV cv::Mat class. * * Based on the answer from: http://stackoverflow.com/questions/4170745/serializing-opencv-mat-vec3f * And the blog post on: http://cheind.wordpress.com/2011/12/06/serialization-of-cvmat-objects-using-boost/ */ namespace boost { namespace serialization { /** * Serialize a cv::Mat using boost::serialization. * * Supports all types of matrices as well as non-contiguous ones. * * @param[in] ar The archive to serialise to (or to serialise from). * @param[in] mat The matrix to serialise (or deserialise). * @param[in] version An optional version argument. */ template void serialize(Archive& ar, cv::Mat& mat, const unsigned int /*version*/) { int rows, cols, type; bool continuous; if (Archive::is_saving::value) { rows = mat.rows; cols = mat.cols; type = mat.type(); continuous = mat.isContinuous(); } ar & BOOST_SERIALIZATION_NVP(rows) & BOOST_SERIALIZATION_NVP(cols) & BOOST_SERIALIZATION_NVP(type) & BOOST_SERIALIZATION_NVP(continuous); if (Archive::is_loading::value) mat.create(rows, cols, type); if (continuous) { const int data_size = rows * cols * static_cast(mat.elemSize()); boost::serialization::binary_object mat_data(mat.data, data_size); ar & BOOST_SERIALIZATION_NVP(mat_data); } else { const int row_size = cols * static_cast(mat.elemSize()); for (int i = 0; i < rows; i++) { boost::serialization::binary_object row_data(mat.ptr(i), row_size); std::string row_name("mat_data_row_" + std::to_string(i)); ar & make_nvp(row_name.c_str(), row_data); } } }; } /* namespace serialization */ } /* namespace boost */ #endif /* MATSERIALISATION_HPP_ */