licornea_tools
feature_slopes.cc
Go to the documentation of this file.
1 #include "feature_slopes.h"
2 #include "../../../lib/random_color.h"
3 #include "../../../lib/assert.h"
4 #include "../../../lib/string.h"
5 #include <vector>
6 
7 namespace tlz {
8 
10  Assert(! fpoints.is_distorted, "feature_slopes can only be created for undistorted feature points");
11 }
12 
13 
15  feature_points fpoints = decode_feature_points(j_fslopes);
16  feature_slopes fslopes(fpoints);
17  const json& j_fslopes_feat = j_fslopes["points"];
18  for(auto it = j_fslopes_feat.begin(); it != j_fslopes_feat.end(); ++it) {
19  const std::string& feature_name = it.key();
20  const json& j_fslope = it.value();
21  feature_slope& fslope = fslopes.slopes[feature_name];
22  fslope.horizontal = j_fslope["slope_horizontal"];
23  fslope.vertical = j_fslope["slope_vertical"];
24  }
25  return fslopes;
26 }
27 
28 
29 bool has_feature_slopes(const json& j_fslopes) {
30  return has(j_fslopes["points"].begin().value(), "slope_horizontal");
31 }
32 
33 
35  json j_fslopes = encode_feature_points(fslopes);
36  json& j_fslopes_feat = j_fslopes["points"];
37  for(const auto& kv : fslopes.slopes) {
38  const std::string& feature_name = kv.first;
39  const feature_slope& fslope = kv.second;
40  json& j_fslope = j_fslopes_feat[feature_name];
41  j_fslope["slope_horizontal"] = fslope.horizontal;
42  j_fslope["slope_vertical"] = fslope.vertical;
43  }
44  return j_fslopes;
45 }
46 
47 
48 real model_horizontal_slope(const vec2& undistorted_point, const mat33& K, const mat33& R) {
49  real ix = undistorted_point[0], iy = undistorted_point[1];
50  real fx = K(0, 0), fy = K(1, 1), cx = K(0, 2), cy = K(1, 2);
51  real r11 = R(0, 0), r21 = R(1, 0), r31 = R(2, 0);
52  return (fy*r21 + cy*r31 - iy*r31) / (fx*r11 + cx*r31 - ix*r31);
53 }
54 
55 
56 real model_vertical_slope(const vec2& undistorted_point, const mat33& K, const mat33& R) {
57  real ix = undistorted_point[0], iy = undistorted_point[1];
58  real fx = K(0, 0), fy = K(1, 1), cx = K(0, 2), cy = K(1, 2);
59  real r12 = R(0, 1), r22 = R(1, 1), r32 = R(2, 1);
60  return (fx*r12 + cx*r32 - ix*r32) / (fy*r22 + cy*r32 - iy*r32);
61 }
62 
63 
64 cv::Mat_<cv::Vec3b> visualize_feature_slopes(const feature_slopes& fslopes, const cv::Mat_<cv::Vec3b>& back_img, int width, real exaggeration, int thickness, const border& bord) {
65  cv::Mat_<cv::Vec3b> out_img;
66  back_img.copyTo(out_img);
67 
68  int radius = width / 2;
69 
70  for(const auto& kv : fslopes.slopes) {
71  const std::string& feature_name = kv.first;
72  const feature_point& fpoint = fslopes.points.at(feature_name);
73  const feature_slope& fslope = kv.second;
74 
75  cv::Point center_point = vec2_to_point(fpoint.position);
76  center_point.x += bord.left;
77  center_point.y += bord.top;
78  cv::Vec3b col = random_color(string_hash(feature_name));
79 
80  real hslope = fslope.horizontal * exaggeration;
81  real vslope = fslope.vertical * exaggeration;
82 
83  // draw near-horizontal line
84  {
85  std::vector<cv::Point> end_points(2);
86  end_points[0] = cv::Point(center_point.x - radius, center_point.y - radius*hslope);
87  end_points[1] = cv::Point(center_point.x + radius, center_point.y + radius*hslope);
88  std::vector<std::vector<cv::Point>> polylines { end_points };
89  cv::polylines(out_img, polylines, false, cv::Scalar(col), thickness);
90  }
91 
92 
93  // draw near-vertical line
94  {
95  std::vector<cv::Point> end_points(2);
96  end_points[0] = cv::Point(center_point.x - radius*vslope, center_point.y - radius);
97  end_points[1] = cv::Point(center_point.x + radius*vslope, center_point.y + radius);
98  std::vector<std::vector<cv::Point>> polylines { end_points };
99  cv::polylines(out_img, polylines, false, cv::Scalar(col), thickness);
100  }
101  }
102 
103  return out_img;
104 }
105 
106 
108  Assert(! a.is_distorted);
109  Assert(! b.is_distorted);
110 
111  feature_slopes ab;
112  ab.points.insert(a.points.begin(), a.points.end());
113  ab.slopes.insert(a.slopes.begin(), a.slopes.end());
114  ab.points.insert(b.points.begin(), b.points.end());
115  ab.slopes.insert(b.slopes.begin(), b.slopes.end());
116  return ab;
117 }
118 
119 
121  std::cout << "loading feature slopes" << std::endl;
123 }
124 
125 
126 
127 }
json encode_feature_slopes(const feature_slopes &fslopes)
feature_slopes()=default
bool has(const json &j, const std::string &key)
Definition: json.h:24
bool has_feature_slopes(const json &j_fslopes)
real model_vertical_slope(const vec2 &undistorted_point, const mat33 &K, const mat33 &R)
real model_horizontal_slope(const vec2 &undistorted_point, const mat33 &K, const mat33 &R)
cv::Vec3b random_color(int i)
Definition: random_color.cc:8
Points of different features, on one view.
int left
Definition: border.h:10
feature_slopes feature_slopes_arg()
cv::Vec< real, 2 > vec2
Definition: common.h:22
cv::Mat_< cv::Vec3b > visualize_feature_slopes(const feature_slopes &fslopes, const cv::Mat_< cv::Vec3b > &back_img, int width, real exaggeration, int thickness, const border &bord)
std::map< std::string, feature_point > points
json json_arg()
Definition: json.h:34
json encode_feature_points(const feature_points &fpoints)
cv::Matx< real, 3, 3 > mat33
Definition: common.h:26
feature_points decode_feature_points(const json &j_fpoints)
double real
Definition: common.h:16
int string_hash(const std::string &str)
Definition: string.cc:75
int top
Definition: border.h:9
feature_slopes decode_feature_slopes(const json &j_fslopes)
#define Assert
Definition: assert.h:40
std::map< std::string, feature_slope > slopes
feature_slopes merge_multiview_feature_slopes(const feature_slopes &a, const feature_slopes &b)
nlohmann::json json
Definition: json.h:11