Volume Cartographer 2.27.0
Json.hpp
Go to the documentation of this file.
1#pragma once
2
8#include <nlohmann/json.hpp>
9#include <opencv2/core.hpp>
10
11NLOHMANN_JSON_NAMESPACE_BEGIN
12/* cv::Vec */
13template <typename T, int Cn>
14struct adl_serializer<cv::Vec<T, Cn>> {
15 template <class JsonT>
16 // NOLINTNEXTLINE(readability-identifier-naming): Must be exact signature
17 static void to_json(JsonT& j, const cv::Vec<T, Cn>& v)
18 {
19 for (int i = 0; i < Cn; i++) {
20 j.push_back(v[i]);
21 }
22 }
23
24 template <class JsonT>
25 // NOLINTNEXTLINE(readability-identifier-naming): Must be exact signature
26 static void from_json(const JsonT& j, cv::Vec<T, Cn>& v)
27 {
28 for (int i = 0; i < Cn; i++) {
29 v[i] = j.at(i).template get<T>();
30 }
31 }
32};
33
34/* cv::Mat_ */
35template <typename T>
36struct adl_serializer<cv::Mat_<T>> {
37 template <class JsonT>
38 // NOLINTNEXTLINE(readability-identifier-naming): Must be exact signature
39 static void to_json(JsonT& j, const cv::Mat_<T>& m)
40 {
41 for (int r = 0; r < m.rows; r++) {
42 json row;
43 for (int c = 0; c < m.cols; c++) {
44 row.push_back(m(r, c));
45 }
46 j.push_back(row);
47 }
48 }
49
50 template <class JsonT>
51 // NOLINTNEXTLINE(readability-identifier-naming): Must be exact signature
52 static void from_json(const JsonT& j, cv::Mat_<T>& m)
53 {
54 auto rows = j.size();
55 auto cols = j[0].size();
56 m = cv::Mat_<T>(rows, cols);
57 for (int r = 0; r < m.rows; r++) {
58 for (int c = 0; c < m.cols; c++) {
59 m(r, c) = j[r][c];
60 }
61 }
62 }
63};
64
65/* cv::Matx */
66template <typename T, int M, int N>
67struct adl_serializer<cv::Matx<T, M, N>> {
68 template <class JsonT>
69 // NOLINTNEXTLINE(readability-identifier-naming): Must be exact signature
70 static void to_json(JsonT& j, const cv::Matx<T, M, N>& mat)
71 {
72 for (int r = 0; r < M; r++) {
73 JsonT row;
74 for (int c = 0; c < N; c++) {
75 row.push_back(mat(r, c));
76 }
77 j.push_back(row);
78 }
79 }
80
81 template <class JsonT>
82 // NOLINTNEXTLINE(readability-identifier-naming): Must be exact signature
83 static void from_json(const JsonT& j, cv::Matx<T, M, N>& mat)
84 {
85 for (int r = 0; r < M; r++) {
86 for (int c = 0; c < N; c++) {
87 mat(r, c) = j[r][c];
88 }
89 }
90 }
91};
92NLOHMANN_JSON_NAMESPACE_END