Jim Ostrowski | a3fd387 | 2021-10-09 19:44:18 -0700 | [diff] [blame] | 1 | #include <math.h> |
| 2 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 3 | #include <opencv2/calib3d.hpp> |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 4 | #include <opencv2/features2d.hpp> |
| 5 | #include <opencv2/imgproc.hpp> |
| 6 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 7 | #include "aos/events/shm_event_loop.h" |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 8 | #include "aos/flatbuffer_merge.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 9 | #include "aos/init.h" |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 10 | #include "aos/network/team_number.h" |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 11 | #include "y2020/vision/sift/sift971.h" |
| 12 | #include "y2020/vision/sift/sift_generated.h" |
| 13 | #include "y2020/vision/sift/sift_training_generated.h" |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 14 | #include "y2020/vision/tools/python_code/sift_training_data.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 15 | #include "y2020/vision/v4l2_reader.h" |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 16 | #include "y2020/vision/vision_generated.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 17 | |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 18 | // config used to allow running camera_reader independently. E.g., |
| 19 | // bazel run //y2020/vision:camera_reader -- --config y2020/config.json |
| 20 | // --override_hostname pi-7971-1 --ignore_timestamps true |
| 21 | DEFINE_string(config, "config.json", "Path to the config file to use."); |
Austin Schuh | 7256aea | 2020-03-28 18:06:46 -0700 | [diff] [blame] | 22 | DEFINE_bool(skip_sift, false, |
| 23 | "If true don't run any feature extraction. Just forward images."); |
Jim Ostrowski | 39b4f0a | 2021-08-28 15:43:01 -0700 | [diff] [blame] | 24 | DEFINE_bool(ransac_pose, false, |
| 25 | "If true, do pose estimate with RANSAC; else, use ITERATIVE mode."); |
Jim Ostrowski | 3a19348 | 2021-09-11 13:21:42 -0700 | [diff] [blame] | 26 | DEFINE_bool(use_prev_pose, true, |
| 27 | "If true, use previous pose estimate as seed for next estimate."); |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 28 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 29 | namespace frc971 { |
| 30 | namespace vision { |
| 31 | namespace { |
| 32 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 33 | class CameraReader { |
| 34 | public: |
| 35 | CameraReader(aos::EventLoop *event_loop, |
| 36 | const sift::TrainingData *training_data, V4L2Reader *reader, |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 37 | const cv::Ptr<cv::flann::IndexParams> &index_params, |
| 38 | const cv::Ptr<cv::flann::SearchParams> &search_params) |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 39 | : event_loop_(event_loop), |
| 40 | training_data_(training_data), |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 41 | camera_calibration_(FindCameraCalibration()), |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 42 | reader_(reader), |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 43 | image_sender_(event_loop->MakeSender<CameraImage>("/camera")), |
| 44 | result_sender_( |
| 45 | event_loop->MakeSender<sift::ImageMatchResult>("/camera")), |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 46 | detailed_result_sender_( |
| 47 | event_loop->MakeSender<sift::ImageMatchResult>("/camera/detailed")), |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame^] | 48 | read_image_timer_(event_loop->AddTimer([this]() { ReadImage(); })) { |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 49 | for (int ii = 0; ii < number_training_images(); ++ii) { |
| 50 | matchers_.push_back(cv::FlannBasedMatcher(index_params, search_params)); |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame^] | 51 | prev_camera_field_R_vec_list_.push_back(cv::Mat::zeros(3, 1, CV_32F)); |
| 52 | prev_camera_field_T_list_.push_back(cv::Mat::zeros(3, 1, CV_32F)); |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 53 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 54 | CopyTrainingFeatures(); |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 55 | |
| 56 | for (auto &matcher : matchers_) { |
| 57 | matcher.train(); |
| 58 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 59 | |
| 60 | event_loop->OnRun( |
| 61 | [this]() { read_image_timer_->Setup(event_loop_->monotonic_now()); }); |
| 62 | } |
| 63 | |
| 64 | private: |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 65 | const sift::CameraCalibration *FindCameraCalibration() const; |
| 66 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 67 | // Copies the information from training_data_ into matcher_. |
| 68 | void CopyTrainingFeatures(); |
| 69 | // Processes an image (including sending the results). |
| 70 | void ProcessImage(const CameraImage &image); |
| 71 | // Reads an image, and then performs all of our processing on it. |
| 72 | void ReadImage(); |
| 73 | |
| 74 | flatbuffers::Offset< |
| 75 | flatbuffers::Vector<flatbuffers::Offset<sift::ImageMatch>>> |
| 76 | PackImageMatches(flatbuffers::FlatBufferBuilder *fbb, |
| 77 | const std::vector<std::vector<cv::DMatch>> &matches); |
| 78 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::Feature>>> |
| 79 | PackFeatures(flatbuffers::FlatBufferBuilder *fbb, |
| 80 | const std::vector<cv::KeyPoint> &keypoints, |
| 81 | const cv::Mat &descriptors); |
| 82 | |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 83 | void SendImageMatchResult(const CameraImage &image, |
| 84 | const std::vector<cv::KeyPoint> &keypoints, |
| 85 | const cv::Mat &descriptors, |
| 86 | const std::vector<std::vector<cv::DMatch>> &matches, |
| 87 | const std::vector<cv::Mat> &camera_target_list, |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 88 | const std::vector<cv::Mat> &field_camera_list, |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 89 | const std::vector<cv::Point2f> &target_point_vector, |
| 90 | const std::vector<float> &target_radius_vector, |
James Kuszmaul | fac4ca6 | 2021-10-02 18:02:04 -0700 | [diff] [blame] | 91 | const std::vector<int> &training_image_indices, |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 92 | const std::vector<int> &homography_feature_counts, |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 93 | aos::Sender<sift::ImageMatchResult> *result_sender, |
| 94 | bool send_details); |
| 95 | |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 96 | // Returns the 2D (image) location for the specified training feature. |
| 97 | cv::Point2f Training2dPoint(int training_image_index, |
| 98 | int feature_index) const { |
| 99 | const float x = training_data_->images() |
| 100 | ->Get(training_image_index) |
| 101 | ->features() |
| 102 | ->Get(feature_index) |
| 103 | ->x(); |
| 104 | const float y = training_data_->images() |
| 105 | ->Get(training_image_index) |
| 106 | ->features() |
| 107 | ->Get(feature_index) |
| 108 | ->y(); |
| 109 | return cv::Point2f(x, y); |
| 110 | } |
| 111 | |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 112 | // Returns the 3D location for the specified training feature. |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 113 | cv::Point3f Training3dPoint(int training_image_index, |
| 114 | int feature_index) const { |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 115 | const sift::KeypointFieldLocation *const location = |
| 116 | training_data_->images() |
| 117 | ->Get(training_image_index) |
| 118 | ->features() |
| 119 | ->Get(feature_index) |
| 120 | ->field_location(); |
| 121 | return cv::Point3f(location->x(), location->y(), location->z()); |
| 122 | } |
| 123 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 124 | const sift::TransformationMatrix *FieldToTarget(int training_image_index) { |
| 125 | return training_data_->images() |
| 126 | ->Get(training_image_index) |
| 127 | ->field_to_target(); |
| 128 | } |
| 129 | |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 130 | void TargetLocation(int training_image_index, cv::Point2f &target_location, |
| 131 | float &target_radius) { |
| 132 | target_location.x = |
| 133 | training_data_->images()->Get(training_image_index)->target_point_x(); |
| 134 | target_location.y = |
| 135 | training_data_->images()->Get(training_image_index)->target_point_y(); |
| 136 | target_radius = training_data_->images() |
| 137 | ->Get(training_image_index) |
| 138 | ->target_point_radius(); |
| 139 | } |
| 140 | |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 141 | int number_training_images() const { |
| 142 | return training_data_->images()->size(); |
| 143 | } |
| 144 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 145 | cv::Mat CameraIntrinsics() const { |
| 146 | const cv::Mat result(3, 3, CV_32F, |
| 147 | const_cast<void *>(static_cast<const void *>( |
| 148 | camera_calibration_->intrinsics()->data()))); |
| 149 | CHECK_EQ(result.total(), camera_calibration_->intrinsics()->size()); |
| 150 | return result; |
| 151 | } |
| 152 | |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 153 | cv::Mat CameraDistCoeffs() const { |
| 154 | const cv::Mat result(5, 1, CV_32F, |
| 155 | const_cast<void *>(static_cast<const void *>( |
| 156 | camera_calibration_->dist_coeffs()->data()))); |
| 157 | CHECK_EQ(result.total(), camera_calibration_->dist_coeffs()->size()); |
| 158 | return result; |
| 159 | } |
| 160 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 161 | aos::EventLoop *const event_loop_; |
| 162 | const sift::TrainingData *const training_data_; |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 163 | const sift::CameraCalibration *const camera_calibration_; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 164 | V4L2Reader *const reader_; |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 165 | std::vector<cv::FlannBasedMatcher> matchers_; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 166 | aos::Sender<CameraImage> image_sender_; |
| 167 | aos::Sender<sift::ImageMatchResult> result_sender_; |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 168 | aos::Sender<sift::ImageMatchResult> detailed_result_sender_; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 169 | // We schedule this immediately to read an image. Having it on a timer means |
| 170 | // other things can run on the event loop in between. |
| 171 | aos::TimerHandler *const read_image_timer_; |
| 172 | |
Jim Ostrowski | 3a19348 | 2021-09-11 13:21:42 -0700 | [diff] [blame] | 173 | // Storage for when we want to use the previous estimates of pose |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame^] | 174 | std::vector<cv::Mat> prev_camera_field_R_vec_list_; |
| 175 | std::vector<cv::Mat> prev_camera_field_T_list_; |
Jim Ostrowski | 3a19348 | 2021-09-11 13:21:42 -0700 | [diff] [blame] | 176 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 177 | const std::unique_ptr<frc971::vision::SIFT971_Impl> sift_{ |
| 178 | new frc971::vision::SIFT971_Impl()}; |
| 179 | }; |
| 180 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 181 | const sift::CameraCalibration *CameraReader::FindCameraCalibration() const { |
| 182 | const std::string_view node_name = event_loop_->node()->name()->string_view(); |
| 183 | const int team_number = aos::network::GetTeamNumber(); |
| 184 | for (const sift::CameraCalibration *candidate : |
| 185 | *training_data_->camera_calibrations()) { |
| 186 | if (candidate->node_name()->string_view() != node_name) { |
| 187 | continue; |
| 188 | } |
| 189 | if (candidate->team_number() != team_number) { |
| 190 | continue; |
| 191 | } |
| 192 | return candidate; |
| 193 | } |
| 194 | LOG(FATAL) << ": Failed to find camera calibration for " << node_name |
| 195 | << " on " << team_number; |
| 196 | } |
| 197 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 198 | void CameraReader::CopyTrainingFeatures() { |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 199 | int training_image_index = 0; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 200 | for (const sift::TrainingImage *training_image : *training_data_->images()) { |
| 201 | cv::Mat features(training_image->features()->size(), 128, CV_32F); |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 202 | for (size_t i = 0; i < training_image->features()->size(); ++i) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 203 | const sift::Feature *feature_table = training_image->features()->Get(i); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 204 | |
| 205 | // We don't need this information right now, but make sure it's here to |
| 206 | // avoid crashes that only occur when specific features are matched. |
| 207 | CHECK(feature_table->has_field_location()); |
| 208 | |
Brian Silverman | f64b9bd | 2020-02-29 12:51:33 -0800 | [diff] [blame] | 209 | const flatbuffers::Vector<uint8_t> *const descriptor = |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 210 | feature_table->descriptor(); |
| 211 | CHECK_EQ(descriptor->size(), 128u) << ": Unsupported feature size"; |
Brian Silverman | f64b9bd | 2020-02-29 12:51:33 -0800 | [diff] [blame] | 212 | const auto in_mat = cv::Mat( |
| 213 | 1, descriptor->size(), CV_8U, |
| 214 | const_cast<void *>(static_cast<const void *>(descriptor->data()))); |
| 215 | const auto out_mat = features(cv::Range(i, i + 1), cv::Range(0, 128)); |
| 216 | in_mat.convertTo(out_mat, CV_32F); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 217 | } |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 218 | matchers_[training_image_index].add(features); |
| 219 | ++training_image_index; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 223 | void CameraReader::SendImageMatchResult( |
| 224 | const CameraImage &image, const std::vector<cv::KeyPoint> &keypoints, |
| 225 | const cv::Mat &descriptors, |
| 226 | const std::vector<std::vector<cv::DMatch>> &matches, |
| 227 | const std::vector<cv::Mat> &camera_target_list, |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 228 | const std::vector<cv::Mat> &field_camera_list, |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 229 | const std::vector<cv::Point2f> &target_point_vector, |
| 230 | const std::vector<float> &target_radius_vector, |
James Kuszmaul | fac4ca6 | 2021-10-02 18:02:04 -0700 | [diff] [blame] | 231 | const std::vector<int> &training_image_indices, |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 232 | const std::vector<int> &homography_feature_counts, |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 233 | aos::Sender<sift::ImageMatchResult> *result_sender, bool send_details) { |
| 234 | auto builder = result_sender->MakeBuilder(); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 235 | const auto camera_calibration_offset = |
Austin Schuh | fa5ee95 | 2020-11-21 17:26:00 -0800 | [diff] [blame] | 236 | aos::RecursiveCopyFlatBuffer(camera_calibration_, builder.fbb()); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 237 | |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 238 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::Feature>>> |
| 239 | features_offset; |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 240 | flatbuffers::Offset< |
| 241 | flatbuffers::Vector<flatbuffers::Offset<sift::ImageMatch>>> |
| 242 | image_matches_offset; |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 243 | if (send_details) { |
| 244 | features_offset = PackFeatures(builder.fbb(), keypoints, descriptors); |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 245 | image_matches_offset = PackImageMatches(builder.fbb(), matches); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 248 | std::vector<flatbuffers::Offset<sift::CameraPose>> camera_poses; |
| 249 | |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 250 | CHECK_EQ(camera_target_list.size(), field_camera_list.size()); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 251 | for (size_t i = 0; i < camera_target_list.size(); ++i) { |
| 252 | cv::Mat camera_target = camera_target_list[i]; |
| 253 | CHECK(camera_target.isContinuous()); |
| 254 | const auto data_offset = builder.fbb()->CreateVector<float>( |
| 255 | reinterpret_cast<float *>(camera_target.data), camera_target.total()); |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 256 | const flatbuffers::Offset<sift::TransformationMatrix> transform_offset = |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 257 | sift::CreateTransformationMatrix(*builder.fbb(), data_offset); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 258 | |
| 259 | cv::Mat field_camera = field_camera_list[i]; |
| 260 | CHECK(field_camera.isContinuous()); |
| 261 | const auto fc_data_offset = builder.fbb()->CreateVector<float>( |
| 262 | reinterpret_cast<float *>(field_camera.data), field_camera.total()); |
| 263 | const flatbuffers::Offset<sift::TransformationMatrix> fc_transform_offset = |
| 264 | sift::CreateTransformationMatrix(*builder.fbb(), fc_data_offset); |
| 265 | |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 266 | const flatbuffers::Offset<sift::TransformationMatrix> |
James Kuszmaul | fac4ca6 | 2021-10-02 18:02:04 -0700 | [diff] [blame] | 267 | field_to_target_offset = aos::RecursiveCopyFlatBuffer( |
| 268 | FieldToTarget(training_image_indices[i]), builder.fbb()); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 269 | |
| 270 | sift::CameraPose::Builder pose_builder(*builder.fbb()); |
| 271 | pose_builder.add_camera_to_target(transform_offset); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 272 | pose_builder.add_field_to_camera(fc_transform_offset); |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 273 | pose_builder.add_field_to_target(field_to_target_offset); |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 274 | pose_builder.add_query_target_point_x(target_point_vector[i].x); |
| 275 | pose_builder.add_query_target_point_y(target_point_vector[i].y); |
| 276 | pose_builder.add_query_target_point_radius(target_radius_vector[i]); |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 277 | pose_builder.add_homography_feature_count(homography_feature_counts[i]); |
| 278 | pose_builder.add_training_image_index(training_image_indices[i]); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 279 | camera_poses.emplace_back(pose_builder.Finish()); |
| 280 | } |
| 281 | const auto camera_poses_offset = builder.fbb()->CreateVector(camera_poses); |
| 282 | |
| 283 | sift::ImageMatchResult::Builder result_builder(*builder.fbb()); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 284 | result_builder.add_camera_poses(camera_poses_offset); |
| 285 | if (send_details) { |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 286 | result_builder.add_image_matches(image_matches_offset); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 287 | result_builder.add_features(features_offset); |
| 288 | } |
| 289 | result_builder.add_image_monotonic_timestamp_ns( |
| 290 | image.monotonic_timestamp_ns()); |
| 291 | result_builder.add_camera_calibration(camera_calibration_offset); |
| 292 | |
| 293 | // TODO<Jim>: Need to add target point computed from matches and |
| 294 | // mapped by homography |
| 295 | builder.Send(result_builder.Finish()); |
| 296 | } |
| 297 | |
| 298 | void CameraReader::ProcessImage(const CameraImage &image) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 299 | // First, we need to extract the brightness information. This can't really be |
| 300 | // fused into the beginning of the SIFT algorithm because the algorithm needs |
| 301 | // to look at the base image directly. It also only takes 2ms on our images. |
| 302 | // This is converting from YUYV to a grayscale image. |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 303 | cv::Mat image_mat(image.rows(), image.cols(), CV_8U); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 304 | CHECK(image_mat.isContinuous()); |
| 305 | const int number_pixels = image.rows() * image.cols(); |
| 306 | for (int i = 0; i < number_pixels; ++i) { |
| 307 | reinterpret_cast<uint8_t *>(image_mat.data)[i] = |
| 308 | image.data()->data()[i * 2]; |
| 309 | } |
| 310 | |
| 311 | // Next, grab the features from the image. |
| 312 | std::vector<cv::KeyPoint> keypoints; |
Austin Schuh | 7256aea | 2020-03-28 18:06:46 -0700 | [diff] [blame] | 313 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 314 | cv::Mat descriptors; |
Austin Schuh | 7256aea | 2020-03-28 18:06:46 -0700 | [diff] [blame] | 315 | if (!FLAGS_skip_sift) { |
| 316 | sift_->detectAndCompute(image_mat, cv::noArray(), keypoints, descriptors); |
| 317 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 318 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 319 | struct PerImageMatches { |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 320 | std::vector<std::vector<cv::DMatch>> matches; |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 321 | std::vector<cv::Point3f> training_points_3d; |
| 322 | std::vector<cv::Point2f> query_points; |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 323 | std::vector<cv::Point2f> training_points; |
| 324 | cv::Mat homography; |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 325 | }; |
| 326 | std::vector<PerImageMatches> per_image_matches(number_training_images()); |
| 327 | |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 328 | for (int image_idx = 0; image_idx < number_training_images(); ++image_idx) { |
| 329 | // Then, match those features against our training data. |
| 330 | std::vector<std::vector<cv::DMatch>> matches; |
| 331 | if (!FLAGS_skip_sift) { |
| 332 | matchers_[image_idx].knnMatch(/* queryDescriptors */ descriptors, matches, |
| 333 | /* k */ 2); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 334 | } |
| 335 | |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 336 | // Pull out the good matches which we want for each image. |
| 337 | // Discard the bad matches per Lowe's ratio test. |
| 338 | // (Lowe originally proposed 0.7 ratio, but 0.75 was later proposed as a |
| 339 | // better option. We'll go with the more conservative (fewer, better |
| 340 | // matches) for now). |
| 341 | for (const std::vector<cv::DMatch> &match : matches) { |
| 342 | CHECK_EQ(2u, match.size()); |
| 343 | CHECK_LE(match[0].distance, match[1].distance); |
| 344 | CHECK_EQ(match[0].imgIdx, 0); |
| 345 | CHECK_EQ(match[1].imgIdx, 0); |
| 346 | CHECK_EQ(match[0].queryIdx, match[1].queryIdx); |
| 347 | if (!(match[0].distance < 0.7 * match[1].distance)) { |
| 348 | continue; |
| 349 | } |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 350 | |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 351 | const int training_image = image_idx; |
| 352 | CHECK_LT(training_image, static_cast<int>(per_image_matches.size())); |
| 353 | PerImageMatches *const per_image = &per_image_matches[training_image]; |
| 354 | per_image->matches.push_back(match); |
| 355 | per_image->matches.back()[0].imgIdx = image_idx; |
| 356 | per_image->matches.back()[1].imgIdx = image_idx; |
| 357 | per_image->training_points.push_back( |
| 358 | Training2dPoint(training_image, match[0].trainIdx)); |
| 359 | per_image->training_points_3d.push_back( |
| 360 | Training3dPoint(training_image, match[0].trainIdx)); |
| 361 | |
| 362 | const cv::KeyPoint &keypoint = keypoints[match[0].queryIdx]; |
| 363 | per_image->query_points.push_back(keypoint.pt); |
| 364 | } |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // The minimum number of matches in a training image for us to use it. |
| 368 | static constexpr int kMinimumMatchCount = 10; |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 369 | std::vector<cv::Mat> camera_target_list; |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 370 | std::vector<cv::Mat> field_camera_list; |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 371 | |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 372 | // Rebuild the matches and store them here |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 373 | std::vector<std::vector<cv::DMatch>> all_good_matches; |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 374 | // Build list of target point and radius for each good match |
| 375 | std::vector<cv::Point2f> target_point_vector; |
| 376 | std::vector<float> target_radius_vector; |
James Kuszmaul | fac4ca6 | 2021-10-02 18:02:04 -0700 | [diff] [blame] | 377 | std::vector<int> training_image_indices; |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 378 | std::vector<int> homography_feature_counts; |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 379 | |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 380 | // Iterate through matches for each training image |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 381 | for (size_t i = 0; i < per_image_matches.size(); ++i) { |
| 382 | const PerImageMatches &per_image = per_image_matches[i]; |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 383 | |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame^] | 384 | VLOG(2) << "Number of matches to start for training image: " << i |
| 385 | << " is: " << per_image.matches.size() << "\n"; |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 386 | // If we don't have enough matches to start, skip this set of matches |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 387 | if (per_image.matches.size() < kMinimumMatchCount) { |
| 388 | continue; |
| 389 | } |
| 390 | |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 391 | // Use homography to determine which matches make sense physically |
| 392 | cv::Mat mask; |
| 393 | cv::Mat homography = |
| 394 | cv::findHomography(per_image.training_points, per_image.query_points, |
| 395 | CV_RANSAC, 3.0, mask); |
| 396 | |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 397 | const int homography_feature_count = cv::countNonZero(mask); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 398 | // If mask doesn't have enough leftover matches, skip these matches |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 399 | if (homography_feature_count < kMinimumMatchCount) { |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 400 | continue; |
| 401 | } |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 402 | homography_feature_counts.push_back(homography_feature_count); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 403 | |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame^] | 404 | VLOG(2) << "Number of matches after homography for training image: " << i |
| 405 | << " is " << cv::countNonZero(mask) << "\n"; |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 406 | |
| 407 | // Fill our match info for each good match based on homography result |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 408 | PerImageMatches per_image_good_match; |
| 409 | CHECK_EQ(per_image.training_points.size(), |
| 410 | (unsigned long)mask.size().height); |
| 411 | for (size_t j = 0; j < per_image.matches.size(); j++) { |
| 412 | // Skip if we masked out by homography |
| 413 | if (mask.at<uchar>(0, j) != 1) { |
| 414 | continue; |
| 415 | } |
| 416 | |
| 417 | // Add this to our collection of all matches that passed our criteria |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 418 | all_good_matches.push_back(per_image.matches[j]); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 419 | |
| 420 | // Fill out the data for matches per image that made it past |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 421 | // homography check, for later use |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 422 | per_image_good_match.matches.push_back(per_image.matches[j]); |
| 423 | per_image_good_match.training_points.push_back( |
| 424 | per_image.training_points[j]); |
| 425 | per_image_good_match.training_points_3d.push_back( |
| 426 | per_image.training_points_3d[j]); |
| 427 | per_image_good_match.query_points.push_back(per_image.query_points[j]); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 428 | } |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 429 | |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 430 | // Returns from opencv are doubles (CV_64F), which don't play well |
| 431 | // with our floats |
| 432 | homography.convertTo(homography, CV_32F); |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame^] | 433 | per_image_good_match.homography = homography.clone(); |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 434 | |
| 435 | CHECK_GT(per_image_good_match.matches.size(), 0u); |
| 436 | |
| 437 | // Collect training target location, so we can map it to matched image |
| 438 | cv::Point2f target_point; |
| 439 | float target_radius; |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 440 | TargetLocation(i, target_point, target_radius); |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 441 | |
| 442 | // Store target_point in vector for use by perspectiveTransform |
| 443 | std::vector<cv::Point2f> src_target_pt; |
| 444 | src_target_pt.push_back(target_point); |
| 445 | std::vector<cv::Point2f> query_target_pt; |
| 446 | |
| 447 | cv::perspectiveTransform(src_target_pt, query_target_pt, homography); |
| 448 | |
| 449 | float query_target_radius = |
| 450 | target_radius * |
| 451 | abs(homography.at<float>(0, 0) + homography.at<float>(1, 1)) / 2.; |
| 452 | |
| 453 | CHECK_EQ(query_target_pt.size(), 1u); |
| 454 | target_point_vector.push_back(query_target_pt[0]); |
| 455 | target_radius_vector.push_back(query_target_radius); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 456 | |
| 457 | // Pose transformations (rotations and translations) for various |
| 458 | // coordinate frames. R_X_Y_vec is the Rodrigues (angle-axis) |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 459 | // representation of the 3x3 rotation R_X_Y from frame X to frame Y |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 460 | |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 461 | // Tranform from camera to target frame |
Jim Ostrowski | 295f2a1 | 2020-02-26 22:22:44 -0800 | [diff] [blame] | 462 | cv::Mat R_camera_target_vec, R_camera_target, T_camera_target; |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 463 | // Tranform from camera to field origin (global) reference frame |
| 464 | cv::Mat R_camera_field_vec, R_camera_field, T_camera_field; |
| 465 | // Inverse of camera to field-- defines location of camera in |
| 466 | // global (field) reference frame |
| 467 | cv::Mat R_field_camera_vec, R_field_camera, T_field_camera; |
| 468 | |
Jim Ostrowski | 3a19348 | 2021-09-11 13:21:42 -0700 | [diff] [blame] | 469 | // Using the previous pose helps to stabilize the estimate, since |
| 470 | // it's sometimes bouncing between two possible poses. Putting it |
| 471 | // near the previous pose helps it converge to the previous pose |
| 472 | // estimate (assuming it's valid). |
| 473 | if (FLAGS_use_prev_pose) { |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame^] | 474 | R_camera_field_vec = prev_camera_field_R_vec_list_[i].clone(); |
| 475 | T_camera_field = prev_camera_field_T_list_[i].clone(); |
| 476 | VLOG(2) << "Using previous match for training image " << i |
| 477 | << " with T of : " << T_camera_field; |
Jim Ostrowski | 3a19348 | 2021-09-11 13:21:42 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Jim Ostrowski | 295f2a1 | 2020-02-26 22:22:44 -0800 | [diff] [blame] | 480 | // Compute the pose of the camera (global origin relative to camera) |
Jim Ostrowski | 39b4f0a | 2021-08-28 15:43:01 -0700 | [diff] [blame] | 481 | if (FLAGS_ransac_pose) { |
| 482 | // RANSAC computation is designed to be more robust to outliers. |
| 483 | // But, we found it bounces around a lot, even with identical points |
| 484 | cv::solvePnPRansac(per_image_good_match.training_points_3d, |
| 485 | per_image_good_match.query_points, CameraIntrinsics(), |
Jim Ostrowski | 3a19348 | 2021-09-11 13:21:42 -0700 | [diff] [blame] | 486 | CameraDistCoeffs(), R_camera_field_vec, T_camera_field, |
| 487 | FLAGS_use_prev_pose); |
Jim Ostrowski | 39b4f0a | 2021-08-28 15:43:01 -0700 | [diff] [blame] | 488 | } else { |
| 489 | // ITERATIVE mode is potentially less robust to outliers, but we |
| 490 | // found it to be more stable |
| 491 | // |
Jim Ostrowski | 39b4f0a | 2021-08-28 15:43:01 -0700 | [diff] [blame] | 492 | cv::solvePnP(per_image_good_match.training_points_3d, |
| 493 | per_image_good_match.query_points, CameraIntrinsics(), |
| 494 | CameraDistCoeffs(), R_camera_field_vec, T_camera_field, |
Jim Ostrowski | 3a19348 | 2021-09-11 13:21:42 -0700 | [diff] [blame] | 495 | FLAGS_use_prev_pose, CV_ITERATIVE); |
Jim Ostrowski | 39b4f0a | 2021-08-28 15:43:01 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Jim Ostrowski | a3fd387 | 2021-10-09 19:44:18 -0700 | [diff] [blame] | 498 | // We are occasionally seeing NaN in the prior estimate, so checking for |
| 499 | // this If we sit, just bail the pose estimate |
| 500 | if (isnan(T_camera_field.at<double>(0, 0))) { |
| 501 | LOG(ERROR) |
| 502 | << "NAN ERROR in solving for Pose (SolvePnP). Pose returned as: T: " |
| 503 | << T_camera_field << "\nR: " << R_camera_field_vec |
| 504 | << "\nNumber of matches is: " |
| 505 | << per_image_good_match.query_points.size(); |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame^] | 506 | VLOG(2) << "Resetting previous values to zero, from: R_prev: " |
| 507 | << prev_camera_field_R_vec_list_[i] |
| 508 | << ", T_prev: " << prev_camera_field_T_list_[i]; |
| 509 | prev_camera_field_R_vec_list_[i] = cv::Mat::zeros(3, 1, CV_32F); |
| 510 | prev_camera_field_T_list_[i] = cv::Mat::zeros(3, 1, CV_32F); |
Jim Ostrowski | a3fd387 | 2021-10-09 19:44:18 -0700 | [diff] [blame] | 511 | |
Jim Ostrowski | 2e67a1b | 2021-10-13 20:24:43 -0700 | [diff] [blame] | 512 | continue; |
Jim Ostrowski | a3fd387 | 2021-10-09 19:44:18 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 515 | CHECK_EQ(cv::Size(1, 3), T_camera_field.size()); |
| 516 | |
| 517 | // Convert to float32's (from float64) to be compatible with the rest |
| 518 | R_camera_field_vec.convertTo(R_camera_field_vec, CV_32F); |
| 519 | T_camera_field.convertTo(T_camera_field, CV_32F); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 520 | |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 521 | // Get matrix version of R_camera_field |
| 522 | cv::Rodrigues(R_camera_field_vec, R_camera_field); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 523 | CHECK_EQ(cv::Size(3, 3), R_camera_field.size()); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 524 | |
| 525 | // Compute H_field_camera = H_camera_field^-1 |
| 526 | R_field_camera = R_camera_field.t(); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 527 | T_field_camera = -R_field_camera * (T_camera_field); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 528 | |
| 529 | // Extract the field_target transformation |
| 530 | const cv::Mat H_field_target(4, 4, CV_32F, |
| 531 | const_cast<void *>(static_cast<const void *>( |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 532 | FieldToTarget(i)->data()->data()))); |
| 533 | |
James Kuszmaul | fac4ca6 | 2021-10-02 18:02:04 -0700 | [diff] [blame] | 534 | training_image_indices.push_back(i); |
| 535 | |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 536 | const cv::Mat R_field_target = |
| 537 | H_field_target(cv::Range(0, 3), cv::Range(0, 3)); |
| 538 | const cv::Mat T_field_target = |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 539 | H_field_target(cv::Range(0, 3), cv::Range(3, 4)); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 540 | |
| 541 | // Use it to get the relative pose from camera to target |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 542 | R_camera_target = R_camera_field * (R_field_target); |
| 543 | T_camera_target = R_camera_field * (T_field_target) + T_camera_field; |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 544 | |
| 545 | // Set H_camera_target |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 546 | { |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 547 | CHECK_EQ(cv::Size(3, 3), R_camera_target.size()); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 548 | CHECK_EQ(cv::Size(1, 3), T_camera_target.size()); |
| 549 | cv::Mat H_camera_target = cv::Mat::zeros(4, 4, CV_32F); |
| 550 | R_camera_target.copyTo(H_camera_target(cv::Range(0, 3), cv::Range(0, 3))); |
| 551 | T_camera_target.copyTo(H_camera_target(cv::Range(0, 3), cv::Range(3, 4))); |
| 552 | H_camera_target.at<float>(3, 3) = 1; |
| 553 | CHECK(H_camera_target.isContinuous()); |
| 554 | camera_target_list.push_back(H_camera_target.clone()); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | // Set H_field_camera |
| 558 | { |
| 559 | CHECK_EQ(cv::Size(3, 3), R_field_camera.size()); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 560 | CHECK_EQ(cv::Size(1, 3), T_field_camera.size()); |
| 561 | cv::Mat H_field_camera = cv::Mat::zeros(4, 4, CV_32F); |
| 562 | R_field_camera.copyTo(H_field_camera(cv::Range(0, 3), cv::Range(0, 3))); |
| 563 | T_field_camera.copyTo(H_field_camera(cv::Range(0, 3), cv::Range(3, 4))); |
| 564 | H_field_camera.at<float>(3, 3) = 1; |
| 565 | CHECK(H_field_camera.isContinuous()); |
| 566 | field_camera_list.push_back(H_field_camera.clone()); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 567 | } |
Jim Ostrowski | 2e67a1b | 2021-10-13 20:24:43 -0700 | [diff] [blame] | 568 | |
| 569 | // We also sometimes see estimates where the target is behind the camera |
| 570 | // or where we have very large pose estimates. |
| 571 | // This will generally lead to an estimate that is off the field, and also |
| 572 | // will mess up the |
| 573 | if (T_camera_target.at<float>(0, 2) < 0.0 || |
| 574 | T_camera_target.at<float>(0, 2) > 100.0) { |
| 575 | LOG(ERROR) << "Pose returned non-physical pose with camera to target z. " |
| 576 | "T_camera_target = " |
| 577 | << T_camera_target |
| 578 | << "\nAnd T_field_camera = " << T_field_camera; |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame^] | 579 | VLOG(2) << "Resetting previous values to zero, from: R_prev: " |
| 580 | << prev_camera_field_R_vec_list_[i] |
| 581 | << ", T_prev: " << prev_camera_field_T_list_[i]; |
| 582 | prev_camera_field_R_vec_list_[i] = cv::Mat::zeros(3, 1, CV_32F); |
| 583 | prev_camera_field_T_list_[i] = cv::Mat::zeros(3, 1, CV_32F); |
Jim Ostrowski | 2e67a1b | 2021-10-13 20:24:43 -0700 | [diff] [blame] | 584 | continue; |
| 585 | } |
| 586 | |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame^] | 587 | prev_camera_field_R_vec_list_[i] = R_camera_field_vec.clone(); |
| 588 | prev_camera_field_T_list_[i] = T_camera_field.clone(); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 589 | } |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 590 | // Now, send our two messages-- one large, with details for remote |
| 591 | // debugging(features), and one smaller |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 592 | SendImageMatchResult(image, keypoints, descriptors, all_good_matches, |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 593 | camera_target_list, field_camera_list, |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 594 | target_point_vector, target_radius_vector, |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 595 | training_image_indices, homography_feature_counts, |
| 596 | &detailed_result_sender_, true); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 597 | SendImageMatchResult(image, keypoints, descriptors, all_good_matches, |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 598 | camera_target_list, field_camera_list, |
| 599 | target_point_vector, target_radius_vector, |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 600 | training_image_indices, homography_feature_counts, |
| 601 | &result_sender_, false); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | void CameraReader::ReadImage() { |
| 605 | if (!reader_->ReadLatestImage()) { |
Austin Schuh | 7256aea | 2020-03-28 18:06:46 -0700 | [diff] [blame] | 606 | if (!FLAGS_skip_sift) { |
| 607 | LOG(INFO) << "No image, sleeping"; |
| 608 | } |
| 609 | read_image_timer_->Setup(event_loop_->monotonic_now() + |
| 610 | std::chrono::milliseconds(10)); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 611 | return; |
| 612 | } |
| 613 | |
| 614 | ProcessImage(reader_->LatestImage()); |
| 615 | |
| 616 | reader_->SendLatestImage(); |
Austin Schuh | 7256aea | 2020-03-28 18:06:46 -0700 | [diff] [blame] | 617 | read_image_timer_->Setup(event_loop_->monotonic_now()); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::ImageMatch>>> |
| 621 | CameraReader::PackImageMatches( |
| 622 | flatbuffers::FlatBufferBuilder *fbb, |
| 623 | const std::vector<std::vector<cv::DMatch>> &matches) { |
| 624 | // First, we need to pull out all the matches for each image. Might as well |
| 625 | // build up the Match tables at the same time. |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 626 | std::vector<std::vector<sift::Match>> per_image_matches( |
| 627 | number_training_images()); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 628 | for (const std::vector<cv::DMatch> &image_matches : matches) { |
Jim Ostrowski | ce02ebe | 2020-03-07 20:39:02 -0800 | [diff] [blame] | 629 | CHECK_GT(image_matches.size(), 0u); |
| 630 | // We're only using the first of the two matches |
| 631 | const cv::DMatch &image_match = image_matches[0]; |
| 632 | CHECK_LT(image_match.imgIdx, number_training_images()); |
| 633 | per_image_matches[image_match.imgIdx].emplace_back(); |
| 634 | sift::Match *const match = &per_image_matches[image_match.imgIdx].back(); |
| 635 | match->mutate_query_feature(image_match.queryIdx); |
| 636 | match->mutate_train_feature(image_match.trainIdx); |
| 637 | match->mutate_distance(image_match.distance); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | // Then, we need to build up each ImageMatch table. |
| 641 | std::vector<flatbuffers::Offset<sift::ImageMatch>> image_match_tables; |
| 642 | for (size_t i = 0; i < per_image_matches.size(); ++i) { |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 643 | const std::vector<sift::Match> &this_image_matches = per_image_matches[i]; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 644 | if (this_image_matches.empty()) { |
| 645 | continue; |
| 646 | } |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 647 | const auto vector_offset = fbb->CreateVectorOfStructs(this_image_matches); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 648 | sift::ImageMatch::Builder image_builder(*fbb); |
| 649 | image_builder.add_train_image(i); |
| 650 | image_builder.add_matches(vector_offset); |
| 651 | image_match_tables.emplace_back(image_builder.Finish()); |
| 652 | } |
| 653 | |
| 654 | return fbb->CreateVector(image_match_tables); |
| 655 | } |
| 656 | |
| 657 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::Feature>>> |
| 658 | CameraReader::PackFeatures(flatbuffers::FlatBufferBuilder *fbb, |
| 659 | const std::vector<cv::KeyPoint> &keypoints, |
| 660 | const cv::Mat &descriptors) { |
| 661 | const int number_features = keypoints.size(); |
| 662 | CHECK_EQ(descriptors.rows, number_features); |
Austin Schuh | 7256aea | 2020-03-28 18:06:46 -0700 | [diff] [blame] | 663 | if (number_features != 0) { |
| 664 | CHECK_EQ(descriptors.cols, 128); |
| 665 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 666 | std::vector<flatbuffers::Offset<sift::Feature>> features_vector( |
| 667 | number_features); |
| 668 | for (int i = 0; i < number_features; ++i) { |
Brian Silverman | f64b9bd | 2020-02-29 12:51:33 -0800 | [diff] [blame] | 669 | const auto submat = |
| 670 | descriptors(cv::Range(i, i + 1), cv::Range(0, descriptors.cols)); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 671 | CHECK(submat.isContinuous()); |
Brian Silverman | f64b9bd | 2020-02-29 12:51:33 -0800 | [diff] [blame] | 672 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> descriptor_offset; |
| 673 | { |
| 674 | uint8_t *data; |
| 675 | descriptor_offset = fbb->CreateUninitializedVector(128, &data); |
| 676 | submat.convertTo( |
| 677 | cv::Mat(1, descriptors.cols, CV_8U, static_cast<void *>(data)), |
| 678 | CV_8U); |
| 679 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 680 | sift::Feature::Builder feature_builder(*fbb); |
| 681 | feature_builder.add_descriptor(descriptor_offset); |
| 682 | feature_builder.add_x(keypoints[i].pt.x); |
| 683 | feature_builder.add_y(keypoints[i].pt.y); |
| 684 | feature_builder.add_size(keypoints[i].size); |
| 685 | feature_builder.add_angle(keypoints[i].angle); |
| 686 | feature_builder.add_response(keypoints[i].response); |
| 687 | feature_builder.add_octave(keypoints[i].octave); |
| 688 | CHECK_EQ(-1, keypoints[i].class_id) |
| 689 | << ": Not sure what to do with a class id"; |
| 690 | features_vector[i] = feature_builder.Finish(); |
| 691 | } |
| 692 | return fbb->CreateVector(features_vector); |
| 693 | } |
| 694 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 695 | void CameraReaderMain() { |
| 696 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 697 | aos::configuration::ReadConfig(FLAGS_config); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 698 | |
Brian Silverman | 36c7f34 | 2021-06-11 15:21:41 -0700 | [diff] [blame] | 699 | const aos::FlatbufferSpan<sift::TrainingData> training_data( |
| 700 | SiftTrainingData()); |
| 701 | CHECK(training_data.Verify()); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 702 | |
| 703 | const auto index_params = cv::makePtr<cv::flann::IndexParams>(); |
| 704 | index_params->setAlgorithm(cvflann::FLANN_INDEX_KDTREE); |
| 705 | index_params->setInt("trees", 5); |
| 706 | const auto search_params = |
| 707 | cv::makePtr<cv::flann::SearchParams>(/* checks */ 50); |
| 708 | cv::FlannBasedMatcher matcher(index_params, search_params); |
| 709 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 710 | aos::ShmEventLoop event_loop(&config.message()); |
Brian Silverman | 62956e7 | 2020-02-26 21:04:05 -0800 | [diff] [blame] | 711 | |
| 712 | // First, log the data for future reference. |
| 713 | { |
| 714 | aos::Sender<sift::TrainingData> training_data_sender = |
| 715 | event_loop.MakeSender<sift::TrainingData>("/camera"); |
Brian Silverman | 36c7f34 | 2021-06-11 15:21:41 -0700 | [diff] [blame] | 716 | training_data_sender.Send(training_data); |
Brian Silverman | 62956e7 | 2020-02-26 21:04:05 -0800 | [diff] [blame] | 717 | } |
| 718 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 719 | V4L2Reader v4l2_reader(&event_loop, "/dev/video0"); |
Brian Silverman | 36c7f34 | 2021-06-11 15:21:41 -0700 | [diff] [blame] | 720 | CameraReader camera_reader(&event_loop, &training_data.message(), |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 721 | &v4l2_reader, index_params, search_params); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 722 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 723 | event_loop.Run(); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | } // namespace |
| 727 | } // namespace vision |
| 728 | } // namespace frc971 |
| 729 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 730 | int main(int argc, char **argv) { |
| 731 | aos::InitGoogle(&argc, &argv); |
| 732 | frc971::vision::CameraReaderMain(); |
| 733 | } |