licornea_tools
reprojection_viewer.cc
Go to the documentation of this file.
1 #include "../lib/common.h"
2 #include "../lib/args.h"
3 #include "../lib/opencv.h"
4 #include "../lib/json.h"
5 #include "../lib/intrinsics.h"
6 #include "../lib/misc.h"
7 #include "../lib/viewer.h"
8 #include "lib/live/grabber.h"
13 #include <string>
14 #include <cmath>
15 
16 using namespace tlz;
17 
18 enum class depth_mode {
19  original,
22 };
23 
24 int main(int argc, const char* argv[]) {
25  get_args(argc, argv, "reprojection.json [densify_mode=fast]");
26  std::string reprojection_parameters_filename = in_filename_arg();
27  std::string densify_mode = string_opt_arg("fast");
28 
29  std::cout << "loading reprojection parameters" << std::endl;
30  kinect_reprojection_parameters reprojection_parameters = decode_kinect_reprojection_parameters(import_json_file(reprojection_parameters_filename));
31  kinect_reprojection reprojection(reprojection_parameters);
32 
34 
35  viewer view(754+754, 424+30);
36  auto& min_d = view.add_int_slider("depth min ", 0, 0, 20000);
37  auto& max_d = view.add_int_slider("depth max", 6000, 0, 20000);
38  auto& diff_range = view.add_int_slider("difference range", 100, 0, 500);
39  auto& superimpose = view.add_int_slider("superimpose (%)", 0, 0, 100);
40 
41  depth_mode used_depth_mode = depth_mode::reprojected;
42 
43  auto densifier = make_depth_densify(densify_mode);
44  cv::Mat_<real> reprojected_depth;
45 
46  bool running = true;
47  while(running) {
48  grab.grab();
49  view.clear();
50 
51  cv::Mat_<cv::Vec3b> color = grab.get_color_frame();
52  cv::Mat_<real> depth = grab.get_depth_frame();
53 
54  auto samples = reprojection.reproject_ir_to_color_samples(depth, depth, true);
55  if(used_depth_mode == depth_mode::original)
56  for(auto& samp : samples) samp.color_depth = samp.ir_depth;
57  else if(used_depth_mode == depth_mode::difference)
58  for(auto& samp : samples) samp.color_depth = samp.ir_depth - samp.color_depth;
59 
60  densifier->densify(samples, reprojected_depth);
61 
62  view.draw(cv::Rect(0, 0, 754, 424), color);
63  cv::Rect depth_rect(754, 0, 754, 424);
64  if(used_depth_mode == depth_mode::difference) view.draw_depth(depth_rect, reprojected_depth, -diff_range.value()/2, +diff_range.value()/2);
65  else view.draw_depth(depth_rect, reprojected_depth, min_d.value(), max_d.value());
66  if(superimpose.value() > 0) {
67  float blend = superimpose.value() / 100.0;
68  view.draw(depth_rect, color, blend);
69  }
70 
71 
72  int label_w = 250;
73  cv::Vec3b selected_mode_color(0, 255, 200);
74  auto col = [&](depth_mode md) { return (md == used_depth_mode ? selected_mode_color : view.text_color); };
75  view.draw_text(cv::Rect(0, 424, 754, 30), "color image", viewer::center);
76  view.draw_text(cv::Rect(754+40, 424, label_w, 30), "(o) original depth", viewer::left, col(depth_mode::original));
77  view.draw_text(cv::Rect(754+40+label_w, 424, label_w, 30), "(r) reprojected depth", viewer::left, col(depth_mode::reprojected));
78  view.draw_text(cv::Rect(754+40+2*label_w, 424, label_w, 30), "(d) difference", viewer::left, col(depth_mode::difference));
79 
80 
81  grab.release();
82 
83  int keycode;
84  running = view.show(keycode);
85 
86  if(keycode == 'o') used_depth_mode = depth_mode::original;
87  else if(keycode == 'r') used_depth_mode = depth_mode::reprojected;
88  else if(keycode == 'd') used_depth_mode = depth_mode::difference;
89  }
90 }
std::unique_ptr< depth_densify_base > make_depth_densify(const std::string &method)
cv::Mat_< cv::Vec3b > get_color_frame()
kinect_reprojection_parameters decode_kinect_reprojection_parameters(const json &j_parameters)
std::string in_filename_arg()
Definition: args.cc:98
void draw(const cv::Mat_< cv::Vec3b > &, real blend=1.0)
Definition: viewer.cc:119
int main(int argc, const char *argv[])
cv::Mat_< float > get_depth_frame(bool undistorted=false)
bool show(int &keycode)
Definition: viewer.cc:218
void draw_depth(cv::Rect rect, const cv::Mat_< float > &depth_img, float min_d, float max_d, real blend=1.0)
Definition: viewer.cc:161
cv::Vec3b text_color
Definition: viewer.h:38
void release()
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
std::vector< sample< Value > > reproject_ir_to_color_samples(const cv::Mat_< Value > &distorted_ir_values, const cv::Mat_< Depth > &distorted_ir_z, bool distort_color=true) const
void clear(int width, int height)
Definition: viewer.cc:76
json import_json_file(const std::string &filename)
Definition: json.cc:24
std::string string_opt_arg(const std::string &def="")
Definition: args.h:36
void get_args(int argc, const char *argv[], const std::string &usage)
Definition: args.cc:49