licornea_tools
checkerboard_depth_samples.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/intrinsics.h"
5 #include "../lib/misc.h"
6 #include "../lib/obj_img_correspondence.h"
7 #include "../lib/viewer.h"
8 #include "lib/live/grabber.h"
10 #include <string>
11 #include <cassert>
12 #include <fstream>
13 
14 using namespace tlz;
15 
17 
18 int main(int argc, const char* argv[]) {
19  get_args(argc, argv, "cols rows out_chk_samples.json");
20  int cols = int_arg();
21  int rows = int_arg();
22  std::string out_chk_samples_filename = out_filename_arg();
23 
25 
26  viewer view(512+512, 424+30);
27  auto& min_ir = view.add_int_slider("ir min", 0, 0x0000, 0xffff);
28  auto& max_ir = view.add_int_slider("ir max", 0xffff, 0x0000, 0xffff);
29  auto& min_d = view.add_int_slider("depth min ", 0, 0, 20000);
30  auto& max_d = view.add_int_slider("depth max", 6000, 0, 20000);
31  auto& granularity = view.add_int_slider("granularity", 2, 1, 30);
32 
33  bool autocollect = false;
34  struct checkerboard_sample {
35  std::vector<vec2> corners;
36  std::vector<checkerboard_pixel_depth_sample> pixel_samples;
37  };
38  std::vector<checkerboard_sample> samples;
39  int total_pixels = 0;
40 
41  checkerboard_sample current_sample;
42  bool has_sample;
43 
44  std::cout << "running viewer... (enter to capture samples, esc to end)" << std::endl;
45  bool running = true;
46  while(running) {
47  grab.grab();
48  view.clear();
49 
50  cv::Mat_<float> depth = grab.get_depth_frame();
51  cv::Mat_<uchar> ir = grab.get_ir_frame(min_ir.value(), max_ir.value());
52 
53  has_sample = false;
54 
55  checkerboard ir_chk = detect_ir_checkerboard(ir, cols, rows, 0.0);
56  if(ir_chk && granularity.value() > 0) {
57  current_sample = checkerboard_sample();
58  current_sample.corners = checkerboard_image_corners(ir_chk);
59  current_sample.pixel_samples = checkerboard_pixel_depth_samples(ir_chk, depth, granularity.value());
60  has_sample = true;
61  }
62 
63  view.draw(cv::Rect(0, 0, 512, 424), visualize_checkerboard(ir, ir_chk));
64  if(has_sample) view.draw(cv::Rect(512, 0, 512, 424), visualize_checkerboard_pixel_samples(view.visualize_depth(depth, min_d.value(), max_d.value()), current_sample.pixel_samples));
65  else view.draw_depth(cv::Rect(512, 0, 512, 424), depth, min_d.value(), max_d.value());
66 
67  view.draw_text(cv::Rect(20, 424, 512+512-20, 30), std::string("(a) autocollect is ") + (autocollect ? "ON" : "off"), viewer::left);
68  view.draw_text(cv::Rect(0, 424, 512+512-20, 30), std::to_string(samples.size()) + " checkerboard samples (" + std::to_string(total_pixels) + " pixels total)", viewer::right);
69 
70  grab.release();
71 
72  int keycode = 0;
73  running = view.show(keycode);
74 
75  bool collect = false;
76 
77  if(keycode == enter_keycode && ir_chk) collect = true;
78  else if(keycode == 'a') autocollect = !autocollect;
79 
80  if(autocollect) collect = true;
81 
82  if(collect && has_sample) {
83  samples.push_back(current_sample);
84  total_pixels += current_sample.pixel_samples.size();
85  }
86  }
87 
88 
89  std::cout << "saving collected checkerboard samples" << std::endl;
90  {
91  json j_chk_samples = json::array();
92  for(const checkerboard_sample& chk : samples) {
93  json j_chk_samp = json::object();
94 
95  json j_corners = json::array();
96  for(const vec2& corner : chk.corners) {
97  json j_corner = json::object();
98  j_corner["x"] = corner[0];
99  j_corner["y"] = corner[1];
100  j_corners.push_back(j_corner);
101  }
102  j_chk_samp["corners"] = j_corners;
103 
104  json j_pixels = json::array();
105  for(const checkerboard_pixel_depth_sample& pix : chk.pixel_samples) {
106  json j_pixel = json::object();
107  j_pixel["x"] = pix.coordinates[0];
108  j_pixel["y"] = pix.coordinates[1];
109  j_pixel["d"] = pix.measured_depth;
110  j_pixels.push_back(j_pixel);
111  }
112  j_chk_samp["pixels"] = j_pixels;
113 
114  j_chk_samples.push_back(j_chk_samp);
115  }
116  export_json_file(j_chk_samples, out_chk_samples_filename);
117  }
118 
119  std::cout << "done" << std::endl;
120 }
cv::Mat_< uchar > get_ir_frame(float min_ir=0, float max_ir=0xffff, bool undistorted=false)
const real reprojection_error_max_threshold
static cv::Mat_< uchar > visualize_depth(const cv::Mat &, float min_d, float max_d)
Definition: viewer.cc:95
std::vector< vec2 > checkerboard_image_corners(const checkerboard &chk)
int rows
void draw(const cv::Mat_< cv::Vec3b > &, real blend=1.0)
Definition: viewer.cc:119
cv::Mat_< cv::Vec3b > visualize_checkerboard(const cv::Mat_< cv::Vec3b > &img, const checkerboard &chk, const checkerboard_visualization_parameters &param)
int main(int argc, const char *argv[])
cv::Vec< real, 2 > vec2
Definition: common.h:22
cv::Mat_< float > get_depth_frame(bool undistorted=false)
bool show(int &keycode)
Definition: viewer.cc:218
cv::Mat_< cv::Vec3b > visualize_checkerboard_pixel_samples(const cv::Mat_< cv::Vec3b > &img, const std::vector< checkerboard_pixel_depth_sample > &pixels, int rad)
double real
Definition: common.h:16
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
long int_arg()
Definition: args.cc:138
constexpr int enter_keycode
Definition: common.h:73
void export_json_file(const json &j, const std::string &filename, bool compact)
Definition: json.cc:9
int cols
std::string to_string(const T &)
std::string out_filename_arg()
Definition: args.cc:104
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
checkerboard detect_ir_checkerboard(cv::Mat_< uchar > &img, int cols, int rows, real square_width)
Definition: checkerboard.cc:62
void clear(int width, int height)
Definition: viewer.cc:76
nlohmann::json json
Definition: json.h:11
std::vector< checkerboard_pixel_depth_sample > checkerboard_pixel_depth_samples(const checkerboard &chk, const cv::Mat_< float > &depth_image, int granularity)
void get_args(int argc, const char *argv[], const std::string &usage)
Definition: args.cc:49