licornea_tools
view_depth.cc
Go to the documentation of this file.
1 #include "../lib/common.h"
2 #include "../lib/args.h"
3 #include "../lib/viewer.h"
4 #include "../lib/opencv.h"
5 #include <cstdlib>
6 #include <iostream>
7 #include <string>
8 
9 using namespace tlz;
10 
11 
12 int main(int argc, const char* argv[]) {
13  get_args(argc, argv, "depth.png [min_d max_d] [out.png]");
14  std::string depth_filename = in_filename_arg();
15  int z_near_init = int_opt_arg(-1);
16  int z_far_init = int_opt_arg(-1);
17  std::string out_depth_filename = out_filename_opt_arg();
18 
19  cv::Mat_<ushort> depth = cv::imread(depth_filename, CV_LOAD_IMAGE_ANYDEPTH);
20  bool is16bit = (depth.depth() == CV_16U);
21 
22  // scale if too large (not when saving to file)
23  int max_cols = 1000;
24  if(out_depth_filename.empty() && depth.cols > max_cols) {
25  int new_cols = max_cols;
26  int new_rows = new_cols * depth.rows / depth.cols;
27  cv::resize(depth, depth, cv::Size(new_cols, new_rows));
28  }
29 
30  // get min/max value in image (excluding 0 pixels)
31  ushort min_value = (is16bit ? 0xffff : 0xff), max_value = 0;
32  for(ushort value : depth) {
33  if(value == 0) continue;
34  else if(value > max_value) max_value = value;
35  else if(value < min_value) min_value = value;
36  }
37 
38 
39  if(out_depth_filename.empty()) {
40  viewer view("Depth map");
41  const int slider_steps = 200;
42  auto& z_near_slider = view.add_real_slider("z near", (z_near_init == -1 ? min_value : z_near_init), min_value, max_value, slider_steps);
43  auto& z_far_slider = view.add_real_slider("z far", (z_far_init == -1 ? max_value : z_near_init), min_value, max_value, slider_steps);
44 
45  auto update = [&]() {
46  real min_d = z_near_slider.value();
47  real max_d = z_far_slider.value();
48 
49  std::cout << "min_d=" << min_d << ", max_d=" << max_d << std::endl;
50 
51  cv::Mat_<uchar> img = viewer::visualize_depth(depth, min_d, max_d);
52  view.clear(img.size());
53  view.draw(img);
54  };
55  view.update_callback = update;
56  view.show_modal();
57 
58  } else {
59  cv::Mat_<uchar> img = viewer::visualize_depth(depth, z_near_init, z_far_init);
60  cv::imwrite(out_depth_filename, img);
61  std::cout << "saved to " << out_depth_filename << std::endl;
62  }
63 
64 }
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
long int_opt_arg(long def)
Definition: args.h:50
static cv::Mat_< uchar > visualize_depth(const cv::Mat &, float min_d, float max_d)
Definition: viewer.cc:95
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 value() const
Definition: viewer.cc:311
std::string out_filename_opt_arg(const std::string &def)
Definition: args.cc:110
double real
Definition: common.h:16
void clear(int width, int height)
Definition: viewer.cc:76
int main(int argc, const char *argv[])
Definition: view_depth.cc:12
const int slider_steps
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