licornea_tools
view_homography.cc
Go to the documentation of this file.
1 #include "view_homography.h"
2 #include "opencv.h"
3 #include <cmath>
4 #include <vector>
5 #include <algorithm>
6 #include <climits>
7 
8 namespace tlz {
9 
11  json j_hom = json::object();
12  j_hom["mat"] = encode_mat(hom.mat);
13  if(! std::isnan(hom.err)) j_hom["err"] = hom.err;
14  return j_hom;
15 }
16 
17 
19  view_homography hom;
20  hom.mat = decode_mat(j_hom["mat"]);
21  hom.err = get_or(j_hom, "err", NAN);
22  return hom;
23 }
24 
25 
27  json j_homs = json::object();
28  for(const auto& kv : homs) {
29  const view_index& idx = kv.first;
30  const view_homography& hom = kv.second;
31  j_homs[encode_view_index(idx)] = encode_view_homography(hom);
32  }
33  return j_homs;
34 }
35 
36 
38  view_homographies homs;
39  for(auto it = j_homs.begin(); it != j_homs.end(); ++it) {
40  view_index idx = decode_view_index(it.key());
41  const json& j_hom = it.value();
42  homs[idx] = decode_view_homography(j_hom);
43  }
44  return homs;
45 }
46 
47 
49  return has(j, "mat");
50 }
51 
52 
54  real err = 0.0;
55  for(const auto& kv : homs) err += kv.second.err;
56  err /= homs.size();
57  return err;
58 }
59 
60 
61 std::vector<vec2> quadrilateral(const view_homography& hom, real width, real height) {
62  std::vector<vec2> src {
63  vec2(0, 0),
64  vec2(width, 0),
65  vec2(width, height),
66  vec2(0, height)
67  };
68 
69  std::vector<vec2> dst;
70  cv::perspectiveTransform(src, dst, hom.mat);
71  return dst;
72 }
73 
74 
75 border maximal_border(const view_homography& hom, real width, real height) {
76  std::vector<vec2> dst = quadrilateral(hom, width, height);
77 
78  real max_x = std::max({ dst[0][0], dst[1][0], dst[2][0], dst[3][0] });
79  real max_y = std::max({ dst[0][1], dst[1][1], dst[2][1], dst[3][1] });
80  real min_x = std::min({ dst[0][0], dst[1][0], dst[2][0], dst[3][0] });
81  real min_y = std::min({ dst[0][1], dst[1][1], dst[2][1], dst[3][1] });
82 
83  border bord;
84  bord.top = -min_y;
85  bord.left = -min_x;
86  bord.bottom = max_y - height;
87  bord.right = max_x - width;
88  return bord;
89 }
90 
91 
92 border maximal_border(const view_homographies& homs, real width, real height) {
93  border max_bord;
94  max_bord.top = INT_MIN;
95  max_bord.left = INT_MIN;
96  max_bord.bottom = INT_MIN;
97  max_bord.right = INT_MIN;
98 
99  for(const auto& kv : homs) {
100  border bord = maximal_border(kv.second, width, height);
101  max_bord.top = std::max({ max_bord.top, bord.top });
102  max_bord.left = std::max({ max_bord.left, bord.left });
103  max_bord.bottom = std::max({ max_bord.bottom, bord.bottom });
104  max_bord.right = std::max({ max_bord.right, bord.right });
105  }
106 
107  return max_bord;
108 }
109 
110 
112  std::cout << "loading view homography" << std::endl;
114 }
115 
117  std::cout << "loading view homographies" << std::endl;
119 }
120 
121 
122 
123 }
view_homography decode_view_homography(const json &j_hom)
std::vector< vec2 > quadrilateral(const view_homography &hom, real width, real height)
bool has(const json &j, const std::string &key)
Definition: json.h:24
std::string encode_view_index(view_index idx)
Definition: dataset.cc:275
view_homographies homographies_arg()
bool is_single_view_homography(const json &j)
int left
Definition: border.h:10
int bottom
Definition: border.h:12
cv::Vec< real, 2 > vec2
Definition: common.h:22
std::map< view_index, view_homography > view_homographies
cv::Mat_< real > decode_mat(const json &j)
Definition: json.cc:32
json encode_view_homography(const view_homography &hom)
json json_arg()
Definition: json.h:34
border maximal_border(const view_homography &hom, real width, real height)
json encode_mat(const Mat &mat)
Definition: json.h:19
double real
Definition: common.h:16
view_homography homography_arg()
int top
Definition: border.h:9
int right
Definition: border.h:11
view_index decode_view_index(const std::string &key)
Definition: dataset.cc:281
real view_homographies_error(const view_homographies &homs)
json encode_view_homographies(const view_homographies &homs)
view_homographies decode_view_homographies(const json &j_homs)
nlohmann::json json
Definition: json.h:11
T get_or(const json &j, const std::string &key, const T &default_value)
Definition: json.h:28