1 #include "../lib/common.h" 2 #include "../lib/args.h" 3 #include "../lib/opencv.h" 4 #include "../lib/json.h" 5 #include "../lib/obj_img_correspondence.h" 6 #include "../lib/intrinsics.h" 7 #include "../lib/misc.h" 8 #include "../lib/viewer.h" 14 #include <libfreenect2/registration.h> 20 int main(
int argc,
const char* argv[]) {
21 get_args(argc, argv,
"internal.json reprojection.json");
25 std::cout <<
"loading internal&reprojection parameters" << std::endl;
29 std::pair<freenect2_color_params, freenect2_ir_params> freenect2_internal =
to_freenect2(internal_parameters);
30 libfreenect2::Registration registration(freenect2_internal.second, freenect2_internal.first);
40 auto& superimpose = view.
add_int_slider(
"superimpose (%)", 0, 0, 100);
41 auto& offset = view.
add_int_slider(
"depth offset (-200 + ..) (mm)", 0, -200, 200);
43 cv::Mat_<uchar> homography_mapping(h, w);
44 cv::Mat_<uchar> freenect2_mapping(h, w);
45 cv::Mat_<vec2> freenect2_mapping_coordinates(424, 512);
46 cv::Mat_<vec2> homography_mapping_coordinates(424, 512);
47 cv::Mat_<float> z_buffer(h, w);
50 int z_offset = offset.value();
59 cv::Mat_<uchar> undistorted_ir = grab.
get_ir_frame(
true);
63 z_buffer.setTo(INFINITY);
64 freenect2_mapping.setTo(0);
65 freenect2_mapping_coordinates.setTo(
vec2(0,0));
66 for(
int dy = 0; dy < 424; ++dy)
for(
int dx = 0; dx < 512; ++dx) {
68 float dz = undistorted_depth(dy, dx);
69 if(dz < 0.001)
continue;
70 uchar value = undistorted_ir(dy, dx);
71 registration.apply(dx, dy, dz, cx, cy);
73 int scx = cx * scale, scy = cy * scale;
74 if(scx < 0 || scx >= w || scy < 0 || scy >= h)
continue;
76 float& old_dz = z_buffer(scy, scx);
77 if(dz > old_dz)
continue;
78 freenect2_mapping(scy, scx) = value;
79 freenect2_mapping_coordinates(dy, dx) =
vec2(cx, cy);
85 homography_mapping.setTo(0);
86 homography_mapping_coordinates.setTo(
vec2(0,0));
87 z_buffer.setTo(INFINITY);
89 cv::Mat_<float> depth_off = depth + z_offset;
90 depth_off.setTo(0.0, (depth == 0.0));
92 for(
auto& samp : color_samples) {
93 real cx = samp.color_coordinates[0], cy = samp.color_coordinates[1];
94 int scx = cx*scale, scy = cy*scale;
95 if(scx < 0 || scx >= w || scy < 0 || scy >= h)
continue;
96 float dz = samp.ir_depth;
98 float& old_dz = z_buffer(scy, scx);
99 if(dz > old_dz)
continue;
100 homography_mapping(scy, scx) = samp.value;
101 int dx = samp.ir_coordinates[0], dy = samp.ir_coordinates[1];
102 homography_mapping_coordinates(dy, dx) = samp.color_coordinates;
107 view.
draw(cv::Rect(0, 20, w, h), freenect2_mapping);
108 view.
draw(cv::Rect(w, 20, w, h), color);
109 view.
draw(cv::Rect(2*w, 20, w, h), homography_mapping);
110 if(superimpose.value() > 0) {
111 float blend = superimpose.value() / 100.0;
112 view.
draw(cv::Rect(0, 20, w, h), color, blend);
113 view.
draw(cv::Rect(2*w, 20, w, h), color, blend);
122 for(
int dy = 0; dy < 424; ++dy)
for(
int dx = 0; dx < 512; ++dx) {
123 vec2 freenect2_mapping_coord = freenect2_mapping_coordinates(dy, dx);
124 vec2 homography_mapping_coord = homography_mapping_coordinates(dy, dx);
125 if(freenect2_mapping_coord ==
vec2(0,0) || homography_mapping_coord ==
vec2(0,0))
continue;
127 vec2 diff = freenect2_mapping_coord - homography_mapping_coord;
128 sum +=
sq(diff[0]) +
sq(diff[1]);
131 real err = std::sqrt(sum / count);
137 }
while(view.
show());
Numeric sq(Numeric n)
Compute square of a number.
int main(int argc, const char *argv[])
cv::Mat_< uchar > get_ir_frame(float min_ir=0, float max_ir=0xffff, bool undistorted=false)
cv::Mat_< cv::Vec3b > get_color_frame()
kinect_reprojection_parameters decode_kinect_reprojection_parameters(const json &j_parameters)
std::string in_filename_arg()
void draw(const cv::Mat_< cv::Vec3b > &, real blend=1.0)
kinect_internal_parameters decode_kinect_internal_parameters(const json &j)
cv::Mat_< float > get_depth_frame(bool undistorted=false)
std::pair< freenect2_color_params, freenect2_ir_params > to_freenect2(const kinect_internal_parameters &)
std::string to_string(const T &)
void draw_text(cv::Rect rect, const std::string &text, text_alignment=left)
int_slider & add_int_slider(const std::string &caption, int default_val, int min_val, int max_val, int step=1)
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)
json import_json_file(const std::string &filename)
void get_args(int argc, const char *argv[], const std::string &usage)