2019-09-25 00:49:21 +00:00
|
|
|
// Copyright 2019, Collabora, Ltd.
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
/*!
|
|
|
|
* @file
|
|
|
|
* @brief OpenCV calibration helpers.
|
|
|
|
* @author Pete Black <pblack@collabora.com>
|
2019-10-12 11:24:19 +00:00
|
|
|
* @ingroup aux_tracking
|
2019-09-25 00:49:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-09-29 14:30:11 +00:00
|
|
|
#include "tracking/t_tracking.h"
|
|
|
|
|
2019-09-25 00:49:21 +00:00
|
|
|
#include <opencv2/opencv.hpp>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-09-29 14:30:11 +00:00
|
|
|
/*!
|
|
|
|
* What calibration data that is saved down to file.
|
|
|
|
*/
|
|
|
|
struct CalibrationRawData : t_calibration_raw_data
|
2019-09-25 00:49:21 +00:00
|
|
|
{
|
2019-09-27 11:28:37 +00:00
|
|
|
cv::Mat l_intrinsics = {};
|
|
|
|
cv::Mat l_distortion = {};
|
|
|
|
cv::Mat l_distortion_fisheye = {};
|
|
|
|
cv::Mat l_translation = {};
|
|
|
|
cv::Mat l_rotation = {};
|
|
|
|
cv::Mat l_projection = {};
|
|
|
|
cv::Mat r_intrinsics = {};
|
|
|
|
cv::Mat r_distortion = {};
|
|
|
|
cv::Mat r_distortion_fisheye = {};
|
|
|
|
cv::Mat r_translation = {};
|
|
|
|
cv::Mat r_rotation = {};
|
|
|
|
cv::Mat r_projection = {};
|
|
|
|
cv::Mat disparity_to_depth = {};
|
2019-09-25 00:49:21 +00:00
|
|
|
};
|
|
|
|
|
2019-09-29 14:30:11 +00:00
|
|
|
struct CalibrationData : t_calibration_data
|
2019-09-25 00:49:21 +00:00
|
|
|
{
|
2019-09-29 14:30:11 +00:00
|
|
|
cv::Mat l_undistort_map_x = {};
|
|
|
|
cv::Mat l_undistort_map_y = {};
|
|
|
|
cv::Mat l_rectify_map_x = {};
|
|
|
|
cv::Mat l_rectify_map_y = {};
|
|
|
|
cv::Mat r_undistort_map_x = {};
|
|
|
|
cv::Mat r_undistort_map_y = {};
|
|
|
|
cv::Mat r_rectify_map_x = {};
|
|
|
|
cv::Mat r_rectify_map_y = {};
|
|
|
|
cv::Mat disparity_to_depth = {};
|
|
|
|
};
|
2019-09-27 11:28:37 +00:00
|
|
|
|
2019-09-29 14:30:11 +00:00
|
|
|
extern "C" bool
|
|
|
|
t_file_load_stereo_calibration_v1_hack(struct t_calibration_data **out_data);
|
2019-09-27 11:28:37 +00:00
|
|
|
|
2019-09-29 14:30:11 +00:00
|
|
|
extern "C" bool
|
|
|
|
t_file_save_raw_data_hack(struct t_calibration_raw_data *raw_data);
|
2019-09-25 00:49:21 +00:00
|
|
|
|
|
|
|
XRT_MAYBE_UNUSED static bool
|
2019-09-29 10:43:08 +00:00
|
|
|
calibration_get_stereo(const char *configuration_filename,
|
2019-09-25 00:49:21 +00:00
|
|
|
uint32_t frame_w,
|
|
|
|
uint32_t frame_h,
|
|
|
|
bool use_fisheye,
|
2019-09-29 10:43:08 +00:00
|
|
|
cv::Mat *l_undistort_map_x,
|
|
|
|
cv::Mat *l_undistort_map_y,
|
|
|
|
cv::Mat *l_rectify_map_x,
|
|
|
|
cv::Mat *l_rectify_map_y,
|
|
|
|
cv::Mat *r_undistort_map_x,
|
|
|
|
cv::Mat *r_undistort_map_y,
|
|
|
|
cv::Mat *r_rectify_map_x,
|
|
|
|
cv::Mat *r_rectify_map_y,
|
|
|
|
cv::Mat *disparity_to_depth)
|
2019-09-25 00:49:21 +00:00
|
|
|
{
|
2019-09-29 14:30:11 +00:00
|
|
|
t_calibration_data *data_c;
|
|
|
|
bool ok = t_file_load_stereo_calibration_v1_hack(&data_c);
|
2019-09-25 00:49:21 +00:00
|
|
|
|
2019-09-29 14:30:11 +00:00
|
|
|
if (!ok) {
|
2019-09-25 00:49:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-29 14:30:11 +00:00
|
|
|
CalibrationData *data = (CalibrationData *)data_c;
|
2019-09-25 00:49:21 +00:00
|
|
|
|
2019-09-29 14:30:11 +00:00
|
|
|
*l_undistort_map_x = data->l_undistort_map_x;
|
|
|
|
*l_undistort_map_y = data->l_undistort_map_y;
|
|
|
|
*l_rectify_map_x = data->l_rectify_map_x;
|
|
|
|
*l_rectify_map_y = data->l_rectify_map_y;
|
|
|
|
*r_undistort_map_x = data->r_undistort_map_x;
|
|
|
|
*r_undistort_map_y = data->r_undistort_map_y;
|
|
|
|
*r_rectify_map_x = data->r_rectify_map_x;
|
|
|
|
*r_rectify_map_y = data->r_rectify_map_y;
|
|
|
|
*disparity_to_depth = data->disparity_to_depth;
|
2019-09-25 00:49:21 +00:00
|
|
|
|
2019-09-29 14:30:11 +00:00
|
|
|
t_calibration_data_free(data_c);
|
2019-09-25 00:49:21 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! @todo Move this as it is a generic helper
|
2019-09-27 11:28:37 +00:00
|
|
|
XRT_MAYBE_UNUSED static int
|
2019-09-29 10:43:08 +00:00
|
|
|
mkpath(char *path)
|
2019-09-25 00:49:21 +00:00
|
|
|
{
|
|
|
|
char tmp[PATH_MAX]; //!< @todo PATH_MAX probably not strictly correct
|
2019-09-29 10:43:08 +00:00
|
|
|
char *p = nullptr;
|
2019-09-25 00:49:21 +00:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
snprintf(tmp, sizeof(tmp), "%s", path);
|
|
|
|
len = strlen(tmp) - 1;
|
|
|
|
if (tmp[len] == '/') {
|
|
|
|
tmp[len] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = tmp + 1; *p; p++) {
|
|
|
|
if (*p == '/') {
|
|
|
|
*p = 0;
|
|
|
|
if (mkdir(tmp, S_IRWXU) < 0 && errno != EEXIST)
|
|
|
|
return -1;
|
|
|
|
*p = '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mkdir(tmp, S_IRWXU) < 0 && errno != EEXIST) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|