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