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