Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 1 | #include "frc971/vision/vision_util_lib.h" |
| 2 | |
Jim Ostrowski | 3320898 | 2024-03-02 15:01:45 -0800 | [diff] [blame^] | 3 | #include "absl/strings/str_format.h" |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 4 | #include "glog/logging.h" |
| 5 | |
| 6 | namespace frc971::vision { |
| 7 | |
| 8 | std::optional<cv::Mat> CameraExtrinsics( |
| 9 | const frc971::vision::calibration::CameraCalibration *camera_calibration) { |
| 10 | CHECK(!camera_calibration->has_turret_extrinsics()) |
| 11 | << "Turret not currently supported"; |
| 12 | |
| 13 | if (!camera_calibration->has_fixed_extrinsics()) { |
| 14 | return std::nullopt; |
| 15 | } |
| 16 | CHECK(camera_calibration->fixed_extrinsics()->has_data()); |
| 17 | cv::Mat result(4, 4, CV_32F, |
| 18 | const_cast<void *>(static_cast<const void *>( |
| 19 | camera_calibration->fixed_extrinsics()->data()->data()))); |
| 20 | result.convertTo(result, CV_64F); |
| 21 | CHECK_EQ(result.total(), |
| 22 | camera_calibration->fixed_extrinsics()->data()->size()); |
| 23 | |
| 24 | return result; |
| 25 | } |
| 26 | |
| 27 | cv::Mat CameraIntrinsics( |
| 28 | const frc971::vision::calibration::CameraCalibration *camera_calibration) { |
| 29 | cv::Mat result(3, 3, CV_32F, |
| 30 | const_cast<void *>(static_cast<const void *>( |
| 31 | camera_calibration->intrinsics()->data()))); |
| 32 | result.convertTo(result, CV_64F); |
| 33 | CHECK_EQ(result.total(), camera_calibration->intrinsics()->size()); |
| 34 | |
| 35 | return result; |
| 36 | } |
| 37 | |
| 38 | cv::Mat CameraDistCoeffs( |
| 39 | const frc971::vision::calibration::CameraCalibration *camera_calibration) { |
| 40 | const cv::Mat result(5, 1, CV_32F, |
| 41 | const_cast<void *>(static_cast<const void *>( |
| 42 | camera_calibration->dist_coeffs()->data()))); |
| 43 | CHECK_EQ(result.total(), camera_calibration->dist_coeffs()->size()); |
| 44 | return result; |
| 45 | } |
| 46 | |
Jim Ostrowski | b974cca | 2024-01-28 15:07:50 -0800 | [diff] [blame] | 47 | std::optional<uint16_t> CameraNumberFromChannel(std::string camera_channel) { |
| 48 | if (camera_channel.find("/camera") == std::string::npos) { |
| 49 | return std::nullopt; |
| 50 | } |
| 51 | // If the string doesn't end in /camera#, return nullopt |
| 52 | uint16_t cam_len = std::string("/camera").length(); |
| 53 | if (camera_channel.length() != camera_channel.find("/camera") + cam_len + 1) { |
| 54 | return std::nullopt; |
| 55 | } |
| 56 | |
| 57 | uint16_t camera_number = std::stoi( |
| 58 | camera_channel.substr(camera_channel.find("/camera") + cam_len, 1)); |
| 59 | return camera_number; |
| 60 | } |
| 61 | |
Jim Ostrowski | 3320898 | 2024-03-02 15:01:45 -0800 | [diff] [blame^] | 62 | std::string CalibrationFilename(std::string calibration_folder, |
| 63 | std::string node_name, int team_number, |
| 64 | int camera_number, std::string camera_id, |
| 65 | std::string timestamp) { |
| 66 | // Get rid of any fractional seconds-- we shouldn't need those and it makes |
| 67 | // the string unnecessarily longer |
| 68 | timestamp = timestamp.substr(0, timestamp.find(".")); |
| 69 | std::string calibration_filename = |
| 70 | calibration_folder + |
| 71 | absl::StrFormat("/calibration_%s-%d-%d_cam-%s_%s.json", node_name.c_str(), |
| 72 | team_number, camera_number, camera_id.c_str(), |
| 73 | timestamp.c_str()); |
| 74 | return calibration_filename; |
| 75 | } |
| 76 | |
Jim Ostrowski | cb8b408 | 2024-01-21 02:23:46 -0800 | [diff] [blame] | 77 | } // namespace frc971::vision |