licornea_tools
feature_points.cc
Go to the documentation of this file.
1 #include "feature_points.h"
2 #include "image_correspondence.h"
3 #include "../../lib/random_color.h"
4 #include "../../lib/json.h"
5 #include "../../lib/string.h"
6 
7 namespace tlz {
8 
10  feature_points fpoints;
11  if(has(j_fpoints, "view_idx")) fpoints.view_idx = decode_view_index(j_fpoints["view_idx"]);
12  fpoints.is_distorted = get_or(j_fpoints, "is_distorted", false);
13  const json& j_fpoints_feat = j_fpoints["points"];
14  for(auto it = j_fpoints_feat.begin(); it != j_fpoints_feat.end(); ++it) {
15  const std::string& feature_name = it.key();
16  const json& j_fpoint = it.value();
17  fpoints.points[feature_name] = decode_feature_point(j_fpoint);
18  }
19  return fpoints;
20 }
21 
22 
24  json f_fpoints_feat = json::object();
25  for(const auto& kv : fpoints.points) {
26  const std::string& feature_name = kv.first;
27  const feature_point& fpoint = kv.second;
28  json j_fpoint = encode_feature_point(fpoint);
29  f_fpoints_feat[feature_name] = j_fpoint;
30  }
31 
32  json j_fpoints = json::object();
33  if(fpoints.view_idx) j_fpoints["view_idx"] = encode_view_index(fpoints.view_idx);
34  j_fpoints["points"] = f_fpoints_feat;
35  j_fpoints["is_distorted"] = fpoints.is_distorted;
36 
37  return j_fpoints;
38 }
39 
40 
42  real weights_sum = 0.0;
43  for(const auto& kv : points) weights_sum += kv.second.weight;
44  weights_sum /= points.size();
45  for(auto& kv : points) kv.second.weight /= weights_sum;
46 }
47 
48 
50  feature_points fpoints;
51  fpoints.view_idx = idx;
52  fpoints.is_distorted = is_distorted;
53 
54  for(const auto& kv : cors.features) {
55  const std::string& feature_name = kv.first;
56  const image_correspondence_feature& feature = kv.second;
57  auto point_it = feature.points.find(idx);
58  if(point_it != feature.points.end()) fpoints.points[feature_name] = point_it->second;
59  }
60 
61  return fpoints;
62 }
63 
64 
65 feature_points undistort(const feature_points& dist_fpoints, const intrinsics& intr) {
66  if(! dist_fpoints.is_distorted) return dist_fpoints;
67  if(! intr.distortion) return dist_fpoints;
68 
69  feature_points undist_fpoints;
70  undist_fpoints.is_distorted = false;
71  undist_fpoints.view_idx = dist_fpoints.view_idx;
72 
73  std::vector<vec2> distorted_points;
74  std::vector<vec2*> undistorted_point_ptrs;
75  for(const auto& kv : dist_fpoints.points) {
76  const std::string& feature_name = kv.first;
77  const feature_point& dist_fpt = kv.second;
78  feature_point& undist_fpt = undist_fpoints.points[feature_name];
79  distorted_points.push_back(dist_fpt.position);
80  undistorted_point_ptrs.push_back(&undist_fpt.position);
81  undist_fpt = dist_fpt; // copy depth, weight
82  }
83 
84  std::vector<vec2> undistorted_points = undistort_points(intr, distorted_points);
85  for(std::ptrdiff_t idx = 0; idx < distorted_points.size(); ++idx)
86  *(undistorted_point_ptrs[idx]) = undistorted_points[idx];
87 
88  return undist_fpoints;
89 }
90 
91 
93  feature_points fpoints = feature_points_for_view(cors, idx);
94  if(intr.distortion) {
95  return undistort(fpoints, intr);
96  } else {
97  fpoints.is_distorted = false;
98  return fpoints;
99  }
100 }
101 
102 
103 std::vector<vec2> positions(const feature_points& fpoints) {
104  std::vector<vec2> list;
105  for(const auto& kv : fpoints.points) {
106  const feature_point& fpoint = kv.second;
107  list.push_back(fpoint.position);
108  }
109  return list;
110 }
111 
112 
113 
114 cv::Mat_<cv::Vec3b> visualize_feature_points(const feature_points& fpoints, const cv::Mat_<cv::Vec3b>& back_img, const border& bord) {
115  cv::Mat_<cv::Vec3b> img;
116  back_img.copyTo(img);
117 
118  for(const auto& kv : fpoints.points) {
119  const std::string& feature_name = kv.first;
120  const feature_point& fpoint = kv.second;
121  cv::Vec3b col = random_color(string_hash(feature_name));
122  cv::Point2f center_point = vec2_to_point(fpoint.position);
123  center_point.x += bord.left;
124  center_point.y += bord.top;
125  cv_aa_circle(img, center_point, 10, cv::Scalar(col), 2);
126  cv::Point label_point(center_point.x + 10, center_point.y - 10);
127  cv::putText(img, feature_name, label_point, cv::FONT_HERSHEY_COMPLEX_SMALL, 1.0, cv::Scalar(col));
128  }
129  return img;
130 }
131 
132 
134  std::cout << "loading feature points" << std::endl;
136 }
137 
138 
139 
140 }
bool has(const json &j, const std::string &key)
Definition: json.h:24
std::vector< vec2 > positions(const feature_points &fpoints)
std::string encode_view_index(view_index idx)
Definition: dataset.cc:275
feature_points undistorted_feature_points_for_view(const image_correspondences &cors, view_index idx, const intrinsics &intr)
Set of features, each on set of views.
feature_point decode_feature_point(const json &j_pt)
Definition: feature_point.cc:5
cv::Vec3b random_color(int i)
Definition: random_color.cc:8
Points of different features, on one view.
int left
Definition: border.h:10
std::map< std::string, feature_point > points
feature_points feature_points_for_view(const image_correspondences &cors, view_index idx, bool is_distorted)
std::map< std::string, image_correspondence_feature > features
json json_arg()
Definition: json.h:34
distortion_parameters distortion
Definition: intrinsics.h:30
json encode_feature_point(const feature_point &pt)
json encode_feature_points(const feature_points &fpoints)
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_points undistort(const feature_points &dist_fpoints, const intrinsics &intr)
Feature on set of views. Optionally one view is "reference".
std::vector< vec2 > undistort_points(const intrinsics &intr, const std::vector< vec2 > &distorted)
Definition: intrinsics.cc:45
view_index decode_view_index(const std::string &key)
Definition: dataset.cc:281
cv::Mat_< cv::Vec3b > visualize_feature_points(const feature_points &fpoints, const cv::Mat_< cv::Vec3b > &back_img, const border &bord)
feature_points feature_points_arg()
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
std::map< view_index, feature_point > points
void cv_aa_circle(cv::Mat &mat, const cv::Point2f &center, float rad, cv::Scalar col, int thickness)
Definition: opencv.cc:5