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