licornea_tools
cg_slopes_viewer.cc
Go to the documentation of this file.
1 #include "../lib/args.h"
2 #include "../lib/opencv.h"
3 #include "../lib/intrinsics.h"
4 #include "../lib/dataset.h"
5 #include "../lib/misc.h"
6 #include "../lib/viewer.h"
7 #include "../lib/assert.h"
8 #include "lib/feature_points.h"
10 #include <cstdlib>
11 #include <iostream>
12 #include <string>
13 
14 using namespace tlz;
15 
17  mat33 Rx(
18  1.0, 0.0, 0.0,
19  0.0, std::cos(x), -std::sin(x),
20  0.0, std::sin(x), std::cos(x)
21  );
22  mat33 Ry(
23  std::cos(y), 0.0, std::sin(y),
24  0.0, 1.0, 0.0,
25  -std::sin(y), 0.0, std::cos(y)
26  );
27  mat33 Rz(
28  std::cos(z), -std::sin(z), 0.0,
29  std::sin(z), std::cos(z), 0.0,
30  0.0, 0.0, 1.0
31  );
32  mat33 R = Rz * Ry * Rx;
33  return R.t();
34 }
35 
36 
37 
38 int main(int argc, const char* argv[]) {
39  get_args(argc, argv, "dataset_parameters.json intrinsics.json points.json/measured_slopes.json");
40  dataset datas = dataset_arg();
41  intrinsics intr = intrinsics_arg();
42  std::string point_or_slopes_filename = in_filename_arg();
43  std::string dataset_group_name = ""; // TODO include with feature_points
44 
45  Assert(intr.distortion.is_none(), "must have not distortion");
46  dataset_group datag = datas.group(dataset_group_name);
47 
48  std::cout << "loading feature points or slopes" << std::endl;
49  feature_points fpoints;
50  feature_slopes measured_fslopes;
51  bool has_measured_slopes;
52  {
53  json j = import_json_file(point_or_slopes_filename);
54  has_measured_slopes = has_feature_slopes(j);
55  if(has_measured_slopes) fpoints = measured_fslopes = decode_feature_slopes(j);
56  else fpoints = decode_feature_points(j);
57  }
58 
59  std::cout << "preparing background image" << std::endl;
60  const cv::Vec3b background_color(0, 0, 0);
61  cv::Size image_size = datag.image_size_with_border();
62  cv::Mat_<cv::Vec3b> back_image(image_size, background_color);
63  if(fpoints.view_idx) {
64  view_index view_index = fpoints.view_idx;
65  std::string image_filename = datas.view(view_index).image_filename();
66  back_image = cv::imread(image_filename, CV_LOAD_IMAGE_COLOR);
67  image_size = back_image.size();
68  }
69 
70  viewer view("Slopes Viewer", image_size.width, image_size.height+20+(has_measured_slopes ? 20 : 0), true);
71  const real max_abs_angle = 10.0_deg;
72  auto& x_slider = view.add_real_slider("X", 0.0, -max_abs_angle, +max_abs_angle, 1000);
73  auto& y_slider = view.add_real_slider("Y", 0.0, -max_abs_angle, +max_abs_angle, 1000);
74  auto& z_slider = view.add_real_slider("Z", 0.0, -max_abs_angle, +max_abs_angle, 1000);
75  auto& measured_width_slider = view.add_int_slider("measured width", 0, 0, 400);
76  auto& model_width_slider = view.add_int_slider("model width", 200, 0, 400);
77  auto& exaggeration_slider = view.add_real_slider("exaggeration", 1.0, 1.0, 100.0);
78 
79 
80  view.update_callback = [&]() {
81  // parameters
82  real x = x_slider.value(), y = y_slider.value(), z = z_slider.value();
83  real exaggeration = exaggeration_slider.value();
84 
85  cv::Mat_<cv::Vec3b> viz_image(image_size);
86 
87  // draw background
88  back_image.copyTo(viz_image);
89 
90  // draw measured slopes
91  if(has_measured_slopes && measured_width_slider.value() > 0)
92  viz_image = visualize_feature_slopes(measured_fslopes, viz_image, measured_width_slider.value(), exaggeration, 2);
93 
94  // make grayscale
95  {
96  cv::Mat interm;
97  cv::cvtColor(viz_image, interm, CV_BGR2GRAY);
98  cv::cvtColor(interm, viz_image, CV_GRAY2BGR);
99  }
100 
101  // draw points
102  //viz_image = visualize_feature_points(fpoints, viz_image);
103 
104  // compute and draw model slopes
105  feature_slopes model_fslopes(fpoints);
106  mat33 R = to_rotation_matrix(x, y, z);
107  for(const auto& kv : fpoints.points) {
108  const std::string& feature_name = kv.first;
109  const feature_point& fpoint = kv.second;
110  feature_slope& fslope = model_fslopes.slopes[feature_name];
111  fslope.horizontal = model_horizontal_slope(fpoint.position, intr.K, R);
112  fslope.vertical = model_vertical_slope(fpoint.position, intr.K, R);
113  }
114  if(model_width_slider > 0)
115  viz_image = visualize_feature_slopes(model_fslopes, viz_image, model_width_slider, exaggeration, 1);
116 
117  view.clear();
118  view.draw(cv::Point(0, 20), viz_image);
119 
120  real herror = NAN;
121  real verror = NAN;
122  real error = NAN;
123  if(has_measured_slopes) {
124  herror = 0;
125  verror = 0;
126  for(const auto& kv : fpoints.points) {
127  const std::string& feature_name = kv.first;
128  const feature_slope& measured_fslope = measured_fslopes.slopes.at(feature_name);
129  const feature_slope& model_fslope = model_fslopes.slopes.at(feature_name);
130 
131  herror += sq(measured_fslope.horizontal - model_fslope.horizontal);
132  verror += sq(measured_fslope.vertical - model_fslope.vertical);
133  }
134  int n = fpoints.points.size();
135  real error_coeff = 100.0;
136  herror /= n;
137  verror /= n;
138  error = herror + verror;
139  error *= error_coeff;
140  herror *= error_coeff;
141  verror *= error_coeff;
142  }
143 
144  // draw label
145  std::string label = "x=" + std::to_string(x * deg_per_rad) + " y=" + std::to_string(y * deg_per_rad) + " z=" + std::to_string(z * deg_per_rad);
146  view.draw_text(cv::Rect(10, 0, image_size.width-20, 20), label, viewer::left);
147 
148  if(has_measured_slopes) {
149  label = "herror=" + std::to_string(herror) + " verror=" + std::to_string(verror) + " error=" + std::to_string(error);
150  view.draw_text(cv::Rect(10, 20+image_size.height, image_size.width-20, 20), label, viewer::left);
151  }
152  };
153 
154  view.show_modal();
155 }
real_slider & add_real_slider(const std::string &caption, real default_val, real min_val, real max_val, int steps=100)
Definition: viewer.cc:273
Numeric sq(Numeric n)
Compute square of a number.
Definition: misc.h:17
constexpr real deg_per_rad
Definition: common.h:78
bool has_feature_slopes(const json &j_fslopes)
std::string image_filename() const
Definition: dataset.cc:64
std::string in_filename_arg()
Definition: args.cc:98
void draw(const cv::Mat_< cv::Vec3b > &, real blend=1.0)
Definition: viewer.cc:119
void show_modal()
Definition: viewer.cc:230
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)
Points of different features, on one view.
const cv::Vec3b background_color(0, 0, 0)
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
dataset_view view(int x) const
Definition: dataset.cc:243
intrinsics intrinsics_arg()
Definition: intrinsics.cc:119
distortion_parameters distortion
Definition: intrinsics.h:30
dataset dataset_arg()
Definition: dataset.cc:297
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
mat33 to_rotation_matrix(const vec3 &euler)
Definition: rotation.cc:17
feature_slopes decode_feature_slopes(const json &j_fslopes)
#define Assert
Definition: assert.h:40
std::string to_string(const T &)
std::map< std::string, feature_slope > slopes
void draw_text(cv::Rect rect, const std::string &text, text_alignment=left)
Definition: viewer.cc:182
int_slider & add_int_slider(const std::string &caption, int default_val, int min_val, int max_val, int step=1)
Definition: viewer.cc:263
void clear(int width, int height)
Definition: viewer.cc:76
nlohmann::json json
Definition: json.h:11
json import_json_file(const std::string &filename)
Definition: json.cc:24
int main(int argc, const char *argv[])
dataset_group group(const std::string &grp) const
Definition: dataset.cc:265
std::function< void()> update_callback
Definition: viewer.h:40
void get_args(int argc, const char *argv[], const std::string &usage)
Definition: args.cc:49