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