The open source OpenXR runtime

a/tracking: Merge ctors for NormalizedCoordsCache

Replaces the individual constructors with one constructor that mimics
the API used by opencv, and takes a distortion_model parameter to
allow use with fisheye distortion models as well.

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2519>

authored by

Joel-Valenciano and committed by
Beyley Cardellio
6c193b61 4a6a143f

+24 -91
+15 -43
src/xrt/auxiliary/tracking/t_calibration.cpp
··· 22 22 #include <opencv2/opencv.hpp> 23 23 #include <sys/stat.h> 24 24 #include <utility> 25 + #include <stdexcept> 25 26 26 27 #if CV_MAJOR_VERSION >= 4 27 28 #define SB_CHEESBOARD_CORNERS_SUPPORTED ··· 1418 1419 } 1419 1420 1420 1421 NormalizedCoordsCache::NormalizedCoordsCache(cv::Size size, // NOLINT // small, pass by value 1421 - const cv::Matx33d &intrinsics, 1422 - const cv::Matx<double, 5, 1> &distortion) 1422 + t_camera_distortion_model distortion_model, 1423 + cv::InputArray intrinsics, 1424 + cv::InputArray distortion, 1425 + cv::InputArray rectification, 1426 + cv::InputArray new_camera_or_projection_matrix) 1423 1427 { 1424 1428 std::vector<cv::Vec2f> outputCoords; 1425 1429 std::vector<cv::Vec2f> inputCoords = generateInputCoordsAndReserveOutputCoords(size, outputCoords); 1426 - // Undistort/reproject those coordinates in one call, to make use of 1427 - // cached internal/intermediate computations. 1428 - cv::undistortPoints(inputCoords, outputCoords, intrinsics, distortion); 1429 1430 1430 - populateCacheMats(size, inputCoords, outputCoords, cacheX_, cacheY_); 1431 - } 1432 - NormalizedCoordsCache::NormalizedCoordsCache(cv::Size size, // NOLINT // small, pass by value 1433 - const cv::Matx33d &intrinsics, 1434 - const cv::Matx<double, 5, 1> &distortion, 1435 - const cv::Matx33d &rectification, 1436 - const cv::Matx33d &new_camera_matrix) 1437 - { 1438 - std::vector<cv::Vec2f> outputCoords; 1439 - std::vector<cv::Vec2f> inputCoords = generateInputCoordsAndReserveOutputCoords(size, outputCoords); 1440 1431 // Undistort/reproject those coordinates in one call, to make use of 1441 1432 // cached internal/intermediate computations. 1442 - cv::undistortPoints(inputCoords, outputCoords, intrinsics, distortion, rectification, new_camera_matrix); 1443 - 1444 - populateCacheMats(size, inputCoords, outputCoords, cacheX_, cacheY_); 1445 - } 1446 - 1447 - NormalizedCoordsCache::NormalizedCoordsCache(cv::Size size, // NOLINT // small, pass by value 1448 - const cv::Matx33d &intrinsics, 1449 - const cv::Matx<double, 5, 1> &distortion, 1450 - const cv::Matx33d &rectification, 1451 - const cv::Matx<double, 3, 4> &new_projection_matrix) 1452 - { 1453 - std::vector<cv::Vec2f> outputCoords; 1454 - std::vector<cv::Vec2f> inputCoords = generateInputCoordsAndReserveOutputCoords(size, outputCoords); 1455 - // Undistort/reproject those coordinates in one call, to make use of 1456 - // cached internal/intermediate computations. 1457 - cv::undistortPoints(inputCoords, outputCoords, intrinsics, distortion, rectification, new_projection_matrix); 1458 - 1459 - populateCacheMats(size, inputCoords, outputCoords, cacheX_, cacheY_); 1460 - } 1461 - NormalizedCoordsCache::NormalizedCoordsCache(cv::Size size, // NOLINT // small, pass by value 1462 - const cv::Mat &intrinsics, 1463 - const cv::Mat &distortion) 1464 - { 1465 - std::vector<cv::Vec2f> outputCoords; 1466 - std::vector<cv::Vec2f> inputCoords = generateInputCoordsAndReserveOutputCoords(size, outputCoords); 1467 - // Undistort/reproject those coordinates in one call, to make use of 1468 - // cached internal/intermediate computations. 1469 - cv::undistortPoints(inputCoords, outputCoords, intrinsics, distortion); 1433 + if (t_camera_distortion_model_is_opencv_fisheye(distortion_model)) { 1434 + cv::fisheye::undistortPoints(inputCoords, outputCoords, intrinsics, distortion, rectification, 1435 + new_camera_or_projection_matrix); 1436 + } else if (t_camera_distortion_model_is_opencv_non_fisheye(distortion_model)) { 1437 + cv::undistortPoints(inputCoords, outputCoords, intrinsics, distortion, rectification, 1438 + new_camera_or_projection_matrix); 1439 + } else { 1440 + throw std::invalid_argument("unsupported distortion model"); 1441 + } 1470 1442 1471 1443 populateCacheMats(size, inputCoords, outputCoords, cacheX_, cacheY_); 1472 1444 }
+9 -48
src/xrt/auxiliary/tracking/t_calibration_opencv.hpp
··· 257 257 * @brief Set up the precomputed cache for a given camera. 258 258 * 259 259 * @param size Size of the image in pixels 260 - * @param intrinsics Camera intrinsics matrix 261 - * @param distortion Distortion coefficients 262 - * 263 - * This overload applies no rectification (`R`) and uses a 264 - * normalized/identity new camera matrix (`P`). 265 - */ 266 - NormalizedCoordsCache(cv::Size size, const cv::Matx33d &intrinsics, const cv::Matx<double, 5, 1> &distortion); 267 - /*! 268 - * @brief Set up the precomputed cache for a given camera (overload for 269 - * rectification and new camera matrix) 270 - * 271 - * @param size Size of the image in pixels 260 + * @param distortion_model Model used with `distortion` 272 261 * @param intrinsics Camera intrinsics matrix 273 262 * @param distortion Distortion coefficients 274 263 * @param rectification Rectification matrix - corresponds to parameter 275 264 * `R` to cv::undistortPoints(). 276 - * @param new_camera_matrix A 3x3 new camera matrix - corresponds to 277 - * parameter `P` to cv::undistortPoints(). 265 + * @param new_camera_or_projection_matrix A new 3x3 camera matrix or 266 + * 3x4 projection matrix - corresponds to parameter `P` to 267 + * cv::undistortPoints(). 278 268 */ 279 269 NormalizedCoordsCache(cv::Size size, 280 - const cv::Matx33d &intrinsics, 281 - const cv::Matx<double, 5, 1> &distortion, 282 - const cv::Matx33d &rectification, 283 - const cv::Matx33d &new_camera_matrix); 284 - 285 - /*! 286 - * @brief Set up the precomputed cache for a given camera. (overload for 287 - * rectification and new projection matrix) 288 - * 289 - * @param size Size of the image in pixels 290 - * @param intrinsics Camera intrinsics matrix 291 - * @param distortion Distortion coefficients 292 - * @param rectification Rectification matrix - corresponds to parameter 293 - * `R` to cv::undistortPoints(). 294 - * @param new_projection_matrix A 3x4 new projection matrix - 295 - * corresponds to parameter `P` to cv::undistortPoints(). 296 - */ 297 - NormalizedCoordsCache(cv::Size size, 298 - const cv::Matx33d &intrinsics, 299 - const cv::Matx<double, 5, 1> &distortion, 300 - const cv::Matx33d &rectification, 301 - const cv::Matx<double, 3, 4> &new_projection_matrix); 302 - 303 - /*! 304 - * @brief Set up the precomputed cache for a given camera. 305 - * 306 - * Less-strongly-typed overload. 307 - * 308 - * @overload 309 - * 310 - * This overload applies no rectification (`R`) and uses a 311 - * normalized/identity new camera matrix (`P`). 312 - */ 313 - NormalizedCoordsCache(cv::Size size, const cv::Mat &intrinsics, const cv::Mat &distortion); 270 + t_camera_distortion_model distortion_model, 271 + cv::InputArray intrinsics, 272 + cv::InputArray distortion, 273 + cv::InputArray rectification = cv::noArray(), 274 + cv::InputArray new_camera_or_projection_matrix = cv::noArray()); 314 275 315 276 /*! 316 277 * @brief Get normalized, undistorted coordinates from a point in the