Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 1 | #include "frc971/vision/vision_util_lib.h" |
| 2 | |
| 3 | #include "glog/logging.h" |
| 4 | |
| 5 | namespace frc971::vision { |
| 6 | |
| 7 | std::optional<cv::Mat> CameraExtrinsics( |
| 8 | const frc971::vision::calibration::CameraCalibration *camera_calibration) { |
| 9 | CHECK(!camera_calibration->has_turret_extrinsics()) |
| 10 | << "Turret not currently supported"; |
| 11 | |
| 12 | if (!camera_calibration->has_fixed_extrinsics()) { |
| 13 | return std::nullopt; |
| 14 | } |
| 15 | CHECK(camera_calibration->fixed_extrinsics()->has_data()); |
| 16 | cv::Mat result(4, 4, CV_32F, |
| 17 | const_cast<void *>(static_cast<const void *>( |
| 18 | camera_calibration->fixed_extrinsics()->data()->data()))); |
| 19 | result.convertTo(result, CV_64F); |
| 20 | CHECK_EQ(result.total(), |
| 21 | camera_calibration->fixed_extrinsics()->data()->size()); |
| 22 | |
| 23 | return result; |
| 24 | } |
| 25 | |
| 26 | cv::Mat CameraIntrinsics( |
| 27 | const frc971::vision::calibration::CameraCalibration *camera_calibration) { |
| 28 | cv::Mat result(3, 3, CV_32F, |
| 29 | const_cast<void *>(static_cast<const void *>( |
| 30 | camera_calibration->intrinsics()->data()))); |
| 31 | result.convertTo(result, CV_64F); |
| 32 | CHECK_EQ(result.total(), camera_calibration->intrinsics()->size()); |
| 33 | |
| 34 | return result; |
| 35 | } |
| 36 | |
| 37 | cv::Mat CameraDistCoeffs( |
| 38 | const frc971::vision::calibration::CameraCalibration *camera_calibration) { |
| 39 | const cv::Mat result(5, 1, CV_32F, |
| 40 | const_cast<void *>(static_cast<const void *>( |
| 41 | camera_calibration->dist_coeffs()->data()))); |
| 42 | CHECK_EQ(result.total(), camera_calibration->dist_coeffs()->size()); |
| 43 | return result; |
| 44 | } |
| 45 | |
Jim Ostrowski | b974cca | 2024-01-28 15:07:50 -0800 | [diff] [blame] | 46 | std::optional<uint16_t> CameraNumberFromChannel(std::string camera_channel) { |
| 47 | if (camera_channel.find("/camera") == std::string::npos) { |
| 48 | return std::nullopt; |
| 49 | } |
| 50 | // If the string doesn't end in /camera#, return nullopt |
| 51 | uint16_t cam_len = std::string("/camera").length(); |
| 52 | if (camera_channel.length() != camera_channel.find("/camera") + cam_len + 1) { |
| 53 | return std::nullopt; |
| 54 | } |
| 55 | |
| 56 | uint16_t camera_number = std::stoi( |
| 57 | camera_channel.substr(camera_channel.find("/camera") + cam_len, 1)); |
| 58 | return camera_number; |
| 59 | } |
| 60 | |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 61 | } // namespace frc971::vision |