From 9b60bd7c772425e492d410003abacd9571531f9c Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Fri, 22 Nov 2019 14:20:19 +0000 Subject: [PATCH] t/calib: Add support to load images --- src/xrt/auxiliary/tracking/t_calibration.cpp | 87 +++++++++++++++++-- src/xrt/auxiliary/tracking/t_tracking.h | 10 +++ .../state_trackers/gui/gui_scene_calibrate.c | 6 ++ 3 files changed, 95 insertions(+), 8 deletions(-) diff --git a/src/xrt/auxiliary/tracking/t_calibration.cpp b/src/xrt/auxiliary/tracking/t_calibration.cpp index 5a12e5a76..50263f831 100644 --- a/src/xrt/auxiliary/tracking/t_calibration.cpp +++ b/src/xrt/auxiliary/tracking/t_calibration.cpp @@ -148,6 +148,12 @@ public: uint32_t collected_of_part = 0; } state; + struct + { + bool enabled = false; + uint32_t num_images = 20; + } load; + //! Should we use subpixel enhancing for checkerboard. bool subpixel_enable = true; //! What subpixel range for checkerboard enhancement. @@ -887,6 +893,18 @@ make_calibration_frame_sbs(class Calibration &c) send_rgb_frame(c); } +static void +make_calibration_frame(class Calibration &c, struct xrt_frame *xf) +{ + switch (xf->stereo_format) { + case XRT_STEREO_FORMAT_SBS: make_calibration_frame_sbs(c); break; + case XRT_STEREO_FORMAT_NONE: make_calibration_frame_mono(c); break; + default: + P("ERROR: Unknown stereo format! '%i'", xf->stereo_format); + make_gui_str(c); + return; + } +} /* * @@ -927,6 +945,60 @@ process_frame_yuyv(class Calibration &c, struct xrt_frame *xf) cv::cvtColor(data_full, c.grey, cv::COLOR_YUV2GRAY_YUYV); } +XRT_NO_INLINE static void +process_load_image(class Calibration &c, struct xrt_frame *xf) +{ + char buf[512]; + + // We need to change the settings for frames to make it work. + uint32_t num_collect_restart = 1; + uint32_t num_cooldown_frames = 0; + uint32_t num_wait_for = 0; + + std::swap(c.num_collect_restart, num_collect_restart); + std::swap(c.num_cooldown_frames, num_cooldown_frames); + std::swap(c.num_wait_for, num_wait_for); + + for (uint32_t i = 0; i < c.load.num_images; i++) { + // Early out if the user requeted less images. + if (c.state.calibrated) { + break; + } + + snprintf(buf, 512, "grey_%03i.png", i); + c.grey = cv::imread(buf, cv::IMREAD_GRAYSCALE); + + if (c.grey.rows == 0 || c.grey.cols == 0) { + fprintf(stderr, "Could not find image '%s'!\n", buf); + continue; + } + + if (c.grey.rows != (int)xf->height || + c.grey.cols != (int)xf->width) { + fprintf(stderr, + "Image size does not match frame size! Image: " + "(%ix%i) Frame: (%ux%u)\n", + c.grey.cols, c.grey.rows, xf->width, + xf->height); + continue; + } + + // Create a new RGB image and then copy the grey data to it. + refresh_gui_frame(c, c.grey.rows, c.grey.cols); + cv::cvtColor(c.grey, c.gui.rgb, cv::COLOR_GRAY2RGB); + + // Call the normal frame processing now. + make_calibration_frame(c, xf); + } + + // Restore settings. + c.num_collect_restart = num_collect_restart; + c.num_cooldown_frames = num_cooldown_frames; + c.num_wait_for = num_wait_for; + + c.load.enabled = false; +} + /* * @@ -939,6 +1011,10 @@ t_calibration_frame(struct xrt_frame_sink *xsink, struct xrt_frame *xf) { auto &c = *(class Calibration *)xsink; + if (c.load.enabled) { + process_load_image(c, xf); + } + // Fill both c.gui.rgb and c.grey with the data we got. switch (xf->format) { case XRT_FORMAT_YUV888: process_frame_yuv(c, xf); break; @@ -967,14 +1043,7 @@ t_calibration_frame(struct xrt_frame_sink *xsink, struct xrt_frame *xf) cv::Scalar(0, 0, 0), -1, 0); } - switch (xf->stereo_format) { - case XRT_STEREO_FORMAT_SBS: make_calibration_frame_sbs(c); break; - case XRT_STEREO_FORMAT_NONE: make_calibration_frame_mono(c); break; - default: - P("ERROR: Unknown stereo format! '%i'", xf->stereo_format); - make_gui_str(c); - return; - } + make_calibration_frame(c, xf); } @@ -1031,6 +1100,8 @@ t_calibration_stereo_create(struct xrt_frame_context *xfctx, c.num_wait_for = params->num_wait_for; c.num_collect_total = params->num_collect_total; c.num_collect_restart = params->num_collect_restart; + c.load.enabled = params->load.enabled; + c.load.num_images = params->load.num_images; c.mirror_rgb_image = params->mirror_rgb_image; c.save_images = params->save_images; diff --git a/src/xrt/auxiliary/tracking/t_tracking.h b/src/xrt/auxiliary/tracking/t_tracking.h index 20ffa072a..e9e66baf8 100644 --- a/src/xrt/auxiliary/tracking/t_tracking.h +++ b/src/xrt/auxiliary/tracking/t_tracking.h @@ -343,6 +343,10 @@ t_psvr_create(struct xrt_frame_context *xfctx, 17, \ 0.02f, \ }, \ + { \ + false, \ + 20, \ + }, \ 20, 5, 20, 1, false, true, \ } @@ -388,6 +392,12 @@ struct t_calibration_params float diagonal_distance_meters; } asymmetric_circles; + struct + { + bool enabled; + int num_images; + } load; + int num_cooldown_frames; int num_wait_for; int num_collect_total; diff --git a/src/xrt/state_trackers/gui/gui_scene_calibrate.c b/src/xrt/state_trackers/gui/gui_scene_calibrate.c index 45fa55260..56493d7d2 100644 --- a/src/xrt/state_trackers/gui/gui_scene_calibrate.c +++ b/src/xrt/state_trackers/gui/gui_scene_calibrate.c @@ -116,6 +116,12 @@ scene_render_select(struct gui_scene *scene, struct gui_program *p) igCheckbox("Mirror (no calibration effect)", &cs->params.mirror_rgb_image); igCheckbox("Save images (mono only)", &cs->params.save_images); + igSeparator(); + igCheckbox("Load images (kinda mono only)", &cs->params.load.enabled); + if (cs->params.load.enabled) { + igInputInt("# images", &cs->params.load.num_images, 1, 5, 0); + } + igSeparator(); igInputInt("Cooldown for # frames", &cs->params.num_cooldown_frames, 1, 5, 0); igInputInt("Wait for # frames (steady)", &cs->params.num_wait_for, 1, 5, 0);