licornea_tools
parallel_wall.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/misc.h"
6 #include "../lib/viewer.h"
7 #include "lib/common.h"
8 #include "lib/live/grabber.h"
12 #include <string>
13 #include <cmath>
14 
15 using namespace tlz;
16 
22 };
23 computation_result compute_depth_slopes(const cv::Mat_<real>& depth) {
24  std::size_t x_count = depth.cols, y_count = depth.rows;
25 
26  real min_d = INFINITY;
27  real max_d = 0.0;
28 
29  std::vector<real> depth_samples;
30  depth_samples.reserve(x_count * y_count);
31  std::vector<real> hmeans(x_count, 0.0), vmeans(y_count, 0.0);
32  std::vector<std::size_t> hcounts(x_count, 0), vcounts(y_count, 0);
33  for(std::ptrdiff_t y = 0; y < y_count; ++y)
34  for(std::ptrdiff_t x = 0; x < x_count; ++x) {
35  real d = depth(y, x);
36  if(d == 0.0) continue;
37 
38  hmeans[x] += d;
39  hcounts[x]++;
40  vmeans[y] += d;
41  vcounts[y]++;
42 
43  if(d < min_d) min_d = d;
44  if(d > max_d) max_d = d;
45  }
46 
47  for(std::ptrdiff_t y = 0; y < y_count; ++y) vmeans[y] /= vcounts[y];
48  for(std::ptrdiff_t x = 0; x < x_count; ++x) hmeans[x] /= hcounts[x];
49 
50  real hslope, vslope;
51  {
52  std::vector<cv::Vec2f> points; points.reserve(x_count);
53  for(std::ptrdiff_t x = 0; x < x_count; ++x)
54  if(! std::isnan(hmeans[x])) points.emplace_back(x, hmeans[x]);
55  cv::Vec4f params;
56  cv::fitLine(points, params, CV_DIST_L2, 0, 0.01, 0.01);
57  hslope = params[1] / params[0];
58  }
59 
60  {
61  std::vector<cv::Vec2f> points; points.reserve(y_count);
62  for(std::ptrdiff_t y = 0; y < y_count; ++y)
63  if(! std::isnan(vmeans[y])) points.emplace_back(y, vmeans[y]);
64  cv::Vec4f params;
65  cv::fitLine(points, params, CV_DIST_L2, 0, 0.01, 0.01);
66  vslope = params[1] / params[0];
67  }
68 
69  return {
70  hslope,
71  vslope,
72  min_d,
73  max_d
74  };
75 }
76 
77 
78 int main(int argc, const char* argv[]) {
79  get_args(argc, argv, "reprojection.json");
80  std::string reprojection_parameters_filename = in_filename_opt_arg("reprojection.json");
81 
82  std::cout << "loading reprojection parameters" << std::endl;
83  kinect_reprojection_parameters reprojection_parameters = decode_kinect_reprojection_parameters(import_json_file(reprojection_parameters_filename));
84  kinect_reprojection reprojection(reprojection_parameters);
85 
87 
88  viewer view("Parallel to Wall", 754+754, 424);
89  view.indicator_color = cv::Vec3b(0, 0, 255);
90  auto& border_x_slider = view.add_int_slider("border X (px)", 280, 250, texture_width/2 - 50);
91  auto& border_y_slider = view.add_int_slider("border Y (px)", 100, 0, texture_height/2 - 50);
92  auto& indicator_precision_slider = view.add_real_slider("precision", 1.0, 0.1, 2.0);
93 
94  auto densifier = make_depth_densify("fast");
95  cv::Mat_<real> reprojected_depth;
96  cv::Mat_<cv::Vec3b> shown_reprojected_depth;
97 
98  bool running = true;
99  while(running) {
100  try {
101  grab.grab();
102  view.clear();
103 
104  cv::Mat_<cv::Vec3b> color = grab.get_color_frame();
105  cv::Mat_<real> depth = grab.get_depth_frame();
106 
107  auto samples = reprojection.reproject_ir_to_color_samples(depth, depth, true);
108  densifier->densify(samples, reprojected_depth);
109 
110  grab.release();
111 
112  // get ROI & compute depth slopes
113  int min_x = border_x_slider, max_x = texture_width - border_x_slider;
114  int min_y = border_y_slider, max_y = texture_height - border_y_slider;
115 
116  cv::Rect roi(min_x, min_y, max_x - min_x, max_y - min_y);
117  cv::Mat_<real> depth_roi = reprojected_depth(roi);
118 
119  computation_result res = compute_depth_slopes(depth_roi);
120 
121  // draw rects
122  cv::Mat_<uchar> shown_reprojected_depth = viewer::visualize_depth(reprojected_depth, res.min_d, res.max_d);
123  cv::Mat_<cv::Vec3b> shown_reprojected_depth_col;
124  cv::cvtColor(shown_reprojected_depth, shown_reprojected_depth_col, CV_GRAY2BGR);
125  cv::rectangle(color, roi, cv::Scalar(view.indicator_color), 4);
126  cv::rectangle(shown_reprojected_depth_col, roi, cv::Scalar(view.indicator_color), 4);
127 
128  // draw depth & color
129  view.draw(cv::Rect(0, 0, 754, 424), color);
130  cv::Rect depth_rect(754, 0, 754, 424);
131  view.draw(depth_rect, shown_reprojected_depth_col);
132 
133  // draw depth indicator
134  view.draw_2d_cross_indicator(depth_rect, res.hslope, res.vslope, indicator_precision_slider);
135  } catch(...) { }
136 
137  running = view.show();
138  }
139 }
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
std::unique_ptr< depth_densify_base > make_depth_densify(const std::string &method)
cv::Mat_< cv::Vec3b > get_color_frame()
constexpr std::size_t texture_height
Definition: common.h:13
kinect_reprojection_parameters decode_kinect_reprojection_parameters(const json &j_parameters)
static cv::Mat_< uchar > visualize_depth(const cv::Mat &, float min_d, float max_d)
Definition: viewer.cc:95
void draw_2d_cross_indicator(cv::Rect rect, real value_x, real value_y, real max_abs_value)
Definition: viewer.cc:186
void draw(const cv::Mat_< cv::Vec3b > &, real blend=1.0)
Definition: viewer.cc:119
int main(int argc, const char *argv[])
cv::Vec3b indicator_color
Definition: viewer.h:36
cv::Mat_< float > get_depth_frame(bool undistorted=false)
constexpr std::size_t texture_width
Definition: common.h:12
bool show(int &keycode)
Definition: viewer.cc:218
computation_result compute_depth_slopes(const cv::Mat_< real > &depth)
double real
Definition: common.h:16
void release()
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 in_filename_opt_arg(const std::string &def="")
Definition: args.h:40
void get_args(int argc, const char *argv[], const std::string &usage)
Definition: args.cc:49