licornea_tools
checkerboard_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/image_io.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 
12 using namespace tlz;
13 
14 std::vector<checkerboard> color_chks, ir_chks;
15 std::string mode;
16 int cols;
17 int rows;
20 
21 
23  json j_cors_set;
24  std::vector<vec3> world_points = checkerboard_world_corners(cols, rows, square_width);
25  if(mode == "color") {
27  for(const checkerboard& chk : color_chks)
28  set.push_back(checkerboard_obj_img_correspondences(chk));
29  j_cors_set = encode_obj_img_correspondences_set(set);
30 
31  } else if(mode == "ir") {
33  for(const checkerboard& chk : ir_chks)
34  set.push_back(checkerboard_obj_img_correspondences(chk));
35  j_cors_set = encode_obj_img_correspondences_set(set);
36 
37  } else if(mode == "both") {
39  for(auto it1 = color_chks.begin(), it2 = ir_chks.begin(); it1 != color_chks.end(); ++it1, ++it2)
40  set.push_back(checkerboard_obj_2img_correspondences(*it1, *it2));
41  j_cors_set = encode_obj_img_correspondences_set(set);
42  }
43 
45 }
46 
47 
48 int main(int argc, const char* argv[]) {
49  get_args(argc, argv, "color/ir/both cols rows square_width out_cors_set.json [out_images_dir/] [restore_from_images]");
50  mode = enum_arg({ "color", "ir", "both" });
51  cols = int_arg();
52  rows = int_arg();
55  std::string out_images_dirname = out_dirname_opt_arg();
56  bool restore_from_images = bool_opt_arg("restore_from_images", false);
57 
58  bool autosave = false;
59 
60  if(restore_from_images) {
61 
62  int count = 0;
63  std::cout << "restoring checkerboards from images" << std::endl;
64  for(int i = 0; ; ++i) {
65  checkerboard color_chk, ir_chk;
66  if(mode == "color" || mode == "both") {
67  std::string color_filename = out_images_dirname + "/color_" + std::to_string(i) + ".png";
68  cv::Mat_<cv::Vec3b> color = cv::imread(color_filename, CV_LOAD_IMAGE_COLOR);
69  if(color.empty()) break;
70  color_chk = detect_color_checkerboard(color, cols, rows, square_width);
71 
72  //auto viz = visualize_checkerboard(color, color_chk);
73  //cv::imshow("Color", viz);
74  //cv::waitKey(1);
75  }
76  if(mode == "ir" || mode == "both") {
77  std::string ir_filename = out_images_dirname + "/ir_" + std::to_string(i) + ".png";
78  cv::Mat_<ushort> ir = cv::imread(ir_filename, CV_LOAD_IMAGE_ANYDEPTH);
79  if(ir.empty()) break;
80  ir_chk = detect_ir_checkerboard(ir, cols, rows, square_width);
81 
82  //auto viz = visualize_checkerboard(ir, ir_chk);
83  //cv::imshow("IR", viz);
84  //cv::waitKey(1);
85  }
86 
87  if(mode == "color" && color_chk) {
88  color_chks.push_back(color_chk); ++count;
89  std::cout << "got color checkerboard " << count << std::endl;
90  } else if(mode == "ir" && ir_chk) {
91  ir_chks.push_back(ir_chk); ++count;
92  std::cout << "got ir checkerboard " << count << std::endl;
93  } else if(mode == "both" && color_chk && ir_chk) {
94  color_chks.push_back(color_chk);
95  ir_chks.push_back(ir_chk);
96  ++count;
97  std::cout << "got color+ir checkerboards " << count << std::endl;
98  }
99  }
100 
101  std::cout << "restored " << count << " checkerboards, saving correspondences" << std::endl;
103 
104  } else {
105 
107 
108  viewer view(754+512, 424+30);
109  auto& min_ir = view.add_int_slider("ir min", 0, 0x0000, 0xffff);
110  auto& max_ir = view.add_int_slider("ir max", 0xffff, 0x0000, 0xffff);
111 
112 
113  int count = 0;
114 
115  std::cout << "running viewer... (enter to capture checkerboard, esc to end)" << std::endl;
116  bool running = true;
117  while(running) {
118  grab.grab();
119  view.clear();
120 
121  checkerboard color_chk, ir_chk;
122 
123  cv::Mat_<cv::Vec3b> color = grab.get_color_frame();
124  cv::Mat_<uchar> ir = grab.get_ir_frame(min_ir.value(), max_ir.value());
125  cv::Mat_<ushort> ir_orig = grab.get_original_ir_frame();
126 
127  if(mode == "color" || mode == "both") color_chk = detect_color_checkerboard(color, cols, rows, square_width);
128  if(mode == "ir" || mode == "both") ir_chk = detect_ir_checkerboard(ir, cols, rows, square_width);
129 
130  view.draw(cv::Rect(0, 0, 754, 424), visualize_checkerboard(color, color_chk));
131  view.draw(cv::Rect(754, 0, 512, 424), visualize_checkerboard(ir, ir_chk));
132  grab.release();
133 
134 
135  view.draw_text(cv::Rect(0, 424, 754+512-10, 30), std::to_string(count) + " samples collected", viewer::right);
136 
137  int keycode = 0;
138  running = view.show(keycode);
139 
140  if(keycode == enter_keycode) {
141  if(! out_images_dirname.empty()) {
142  std::string color_filename = out_images_dirname + "/color_" + std::to_string(count) + ".png";
143  std::string ir_filename = out_images_dirname + "/ir_" + std::to_string(count) + ".png";
144  std::vector<int> params = { CV_IMWRITE_PNG_COMPRESSION, 0 };
145  if(mode == "color" || mode == "both") cv::imwrite(color_filename, color, params);
146  if(mode == "ir" || mode == "both") cv::imwrite(ir_filename, ir_orig, params);
147  }
148 
149  if(mode == "color" && color_chk) {
150  color_chks.push_back(color_chk); ++count;
151  std::cout << "recorded color checkerboard " << count << std::endl;
152  } else if(mode == "ir" && ir_chk) {
153  ir_chks.push_back(ir_chk); ++count;
154  std::cout << "recorded ir checkerboard " << count << std::endl;
155  } else if(mode == "both" && color_chk && ir_chk) {
156  color_chks.push_back(color_chk);
157  ir_chks.push_back(ir_chk);
158  ++count;
159  std::cout << "recorded color+ir checkerboards " << count << std::endl;
160  }
161 
162  if(autosave) {
163  std::cout << "autosaving correspondences" << std::endl;
165  }
166  }
167  }
168 
169  if(! autosave) {
170  std::cout << "now saving correspondences" << std::endl;
172  }
173 
174  }
175 
176  std::cout << "done" << std::endl;
177 }
cv::Mat_< uchar > get_ir_frame(float min_ir=0, float max_ir=0xffff, bool undistorted=false)
cv::Mat_< cv::Vec3b > get_color_frame()
std::vector< checkerboard > ir_chks
obj_img_correspondences< 1, 1 > checkerboard_obj_img_correspondences(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)
std::string enum_arg(const std::vector< std::string > &options)
Definition: args.cc:154
cv::Mat_< ushort > get_original_ir_frame(bool undistorted=false)
checkerboard detect_color_checkerboard(cv::Mat_< cv::Vec3b > &img, int cols, int rows, real square_width)
Definition: checkerboard.cc:46
bool show(int &keycode)
Definition: viewer.cc:218
double real
Definition: common.h:16
int main(int argc, const char *argv[])
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
double real_arg()
Definition: args.cc:146
std::string out_dirname_opt_arg(const std::string &def)
Definition: args.cc:127
real square_width
std::vector< vec3 > checkerboard_world_corners(int cols, int rows, real square_width)
int cols
std::string to_string(const T &)
std::string out_filename_arg()
Definition: args.cc:104
void release()
std::string mode
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
obj_img_correspondences< 1, 2 > checkerboard_obj_2img_correspondences(const checkerboard &chk1, const checkerboard &chk2)
checkerboard detect_ir_checkerboard(cv::Mat_< uchar > &img, int cols, int rows, real square_width)
Definition: checkerboard.cc:62
void save_correspondences_set()
json encode_obj_img_correspondences_set(const obj_img_correspondences_set< Obj_count, Img_count > &)
std::vector< checkerboard > color_chks
bool bool_opt_arg(const std::string &expected, bool def=false)
Definition: args.h:62
void clear(int width, int height)
Definition: viewer.cc:76
nlohmann::json json
Definition: json.h:11
std::string out_cors_set_filename
std::vector< obj_img_correspondences< Obj_count, Img_count >> obj_img_correspondences_set
void get_args(int argc, const char *argv[], const std::string &usage)
Definition: args.cc:49