Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 1 | #ifndef FRC971_VISION_VISION_UTIL_LIB_H_ |
| 2 | #define FRC971_VISION_VISION_UTIL_LIB_H_ |
| 3 | #include <optional> |
| 4 | #include <string_view> |
| 5 | |
Jim Ostrowski | 6d242d5 | 2024-04-03 20:39:03 -0700 | [diff] [blame] | 6 | #include "Eigen/Dense" |
| 7 | #include "Eigen/Geometry" |
| 8 | #include "Eigen/SVD" |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 9 | #include "opencv2/imgproc.hpp" |
| 10 | |
| 11 | #include "frc971/vision/calibration_generated.h" |
Jim Ostrowski | 6d242d5 | 2024-04-03 20:39:03 -0700 | [diff] [blame] | 12 | #include "frc971/vision/ceres/types.h" |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 13 | |
Jim Ostrowski | b974cca | 2024-01-28 15:07:50 -0800 | [diff] [blame] | 14 | // Extract the CameraExtrinsics from a CameraCalibration struct |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 15 | namespace frc971::vision { |
| 16 | std::optional<cv::Mat> CameraExtrinsics( |
| 17 | const frc971::vision::calibration::CameraCalibration *camera_calibration); |
| 18 | |
Jim Ostrowski | b974cca | 2024-01-28 15:07:50 -0800 | [diff] [blame] | 19 | // Extract the CameraIntrinsics from a CameraCalibration struct |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 20 | cv::Mat CameraIntrinsics( |
| 21 | const frc971::vision::calibration::CameraCalibration *camera_calibration); |
| 22 | |
Jim Ostrowski | b974cca | 2024-01-28 15:07:50 -0800 | [diff] [blame] | 23 | // Extract the CameraDistCoeffs from a CameraCalibration struct |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 24 | cv::Mat CameraDistCoeffs( |
| 25 | const frc971::vision::calibration::CameraCalibration *camera_calibration); |
| 26 | |
Jim Ostrowski | b974cca | 2024-01-28 15:07:50 -0800 | [diff] [blame] | 27 | // Get the camera number from a camera channel name, e.g., return 2 from |
| 28 | // "/camera2". Returns nullopt if string doesn't start with "/camera" or does |
| 29 | // not have a number |
| 30 | std::optional<uint16_t> CameraNumberFromChannel(std::string camera_channel); |
| 31 | |
Jim Ostrowski | 3320898 | 2024-03-02 15:01:45 -0800 | [diff] [blame] | 32 | // Return a calibration filename to save to based on the given data |
| 33 | std::string CalibrationFilename(std::string calibration_folder, |
| 34 | std::string node_name, int team_number, |
| 35 | int camera_number, std::string camera_id, |
| 36 | std::string timestamp); |
| 37 | |
Jim Ostrowski | 6d242d5 | 2024-04-03 20:39:03 -0700 | [diff] [blame] | 38 | // Utility functions for dealing with ceres::examples::Pose3d structs |
| 39 | class PoseUtils { |
| 40 | public: |
| 41 | // Embeds a 3d pose into an affine transformation |
| 42 | static Eigen::Affine3d Pose3dToAffine3d( |
| 43 | const ceres::examples::Pose3d &pose3d); |
| 44 | // Inverse of above function |
| 45 | static ceres::examples::Pose3d Affine3dToPose3d(const Eigen::Affine3d &H); |
| 46 | |
| 47 | // Computes pose_2 relative to pose_1. This is equivalent to (pose_1^-1 * |
| 48 | // pose_2) |
| 49 | static ceres::examples::Pose3d ComputeRelativePose( |
| 50 | const ceres::examples::Pose3d &pose_1, |
| 51 | const ceres::examples::Pose3d &pose_2); |
| 52 | |
| 53 | // Computes pose_2 given a pose_1 and pose_2 relative to pose_1. This is |
| 54 | // equivalent to (pose_1 * pose_2_relative) |
| 55 | static ceres::examples::Pose3d ComputeOffsetPose( |
| 56 | const ceres::examples::Pose3d &pose_1, |
| 57 | const ceres::examples::Pose3d &pose_2_relative); |
| 58 | |
| 59 | // Converts a rotation with roll, pitch, and yaw into a quaternion |
| 60 | static Eigen::Quaterniond EulerAnglesToQuaternion(const Eigen::Vector3d &rpy); |
| 61 | // Inverse of above function |
| 62 | static Eigen::Vector3d QuaternionToEulerAngles(const Eigen::Quaterniond &q); |
| 63 | // Converts a 3d rotation matrix into a rotation with roll, pitch, and yaw |
| 64 | static Eigen::Vector3d RotationMatrixToEulerAngles(const Eigen::Matrix3d &R); |
| 65 | }; |
| 66 | |
| 67 | // Compute the average of a set of quaternions |
| 68 | Eigen::Vector4d QuaternionAverage(std::vector<Eigen::Vector4d> quaternions); |
| 69 | |
| 70 | // Compute the average pose given a list of translations (as |
| 71 | // Eigen::Vector3d's) and rotations (as Eigen::Vector4d quaternions) |
| 72 | // Returned as an Eigen::Affine3d pose |
| 73 | // Also, compute the variance of each of these list of vectors |
| 74 | Eigen::Affine3d ComputeAveragePose( |
| 75 | std::vector<Eigen::Vector3d> &translation_list, |
| 76 | std::vector<Eigen::Vector4d> &rotation_list, |
| 77 | Eigen::Vector3d *translation_variance = nullptr, |
| 78 | Eigen::Vector3d *rotation_variance = nullptr); |
| 79 | |
| 80 | // Helper function to compute average pose when supplied with list |
| 81 | // of Eigen::Affine3d's |
| 82 | Eigen::Affine3d ComputeAveragePose( |
| 83 | std::vector<Eigen::Affine3d> &pose_list, |
| 84 | Eigen::Vector3d *translation_variance = nullptr, |
| 85 | Eigen::Vector3d *rotation_variance = nullptr); |
| 86 | |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 87 | } // namespace frc971::vision |
| 88 | |
| 89 | #endif // FRC971_VISION_VISION_UTIL_LIB_H_ |