James Kuszmaul | fe30a74 | 2021-11-13 11:31:00 -0800 | [diff] [blame] | 1 | #include "y2020/vision/camera_reader.h" |
| 2 | |
Jim Ostrowski | a3fd387 | 2021-10-09 19:44:18 -0700 | [diff] [blame] | 3 | #include <math.h> |
| 4 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 5 | #include <opencv2/calib3d.hpp> |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 6 | #include <opencv2/features2d.hpp> |
| 7 | #include <opencv2/imgproc.hpp> |
| 8 | |
James Kuszmaul | fe30a74 | 2021-11-13 11:31:00 -0800 | [diff] [blame] | 9 | #include "aos/events/event_loop.h" |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 10 | #include "aos/flatbuffer_merge.h" |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 11 | #include "aos/network/team_number.h" |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 12 | #include "y2020/vision/sift/sift971.h" |
| 13 | #include "y2020/vision/sift/sift_generated.h" |
| 14 | #include "y2020/vision/sift/sift_training_generated.h" |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 15 | #include "y2020/vision/tools/python_code/sift_training_data.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 16 | #include "y2020/vision/v4l2_reader.h" |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 17 | #include "y2020/vision/vision_generated.h" |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 18 | |
Austin Schuh | 7256aea | 2020-03-28 18:06:46 -0700 | [diff] [blame] | 19 | DEFINE_bool(skip_sift, false, |
| 20 | "If true don't run any feature extraction. Just forward images."); |
Jim Ostrowski | 39b4f0a | 2021-08-28 15:43:01 -0700 | [diff] [blame] | 21 | DEFINE_bool(ransac_pose, false, |
| 22 | "If true, do pose estimate with RANSAC; else, use ITERATIVE mode."); |
Jim Ostrowski | 3a19348 | 2021-09-11 13:21:42 -0700 | [diff] [blame] | 23 | DEFINE_bool(use_prev_pose, true, |
| 24 | "If true, use previous pose estimate as seed for next estimate."); |
Jim Ostrowski | 8565b40 | 2020-02-29 20:26:53 -0800 | [diff] [blame] | 25 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 26 | namespace frc971 { |
| 27 | namespace vision { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 28 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 29 | const sift::CameraCalibration *CameraReader::FindCameraCalibration() const { |
| 30 | const std::string_view node_name = event_loop_->node()->name()->string_view(); |
| 31 | const int team_number = aos::network::GetTeamNumber(); |
| 32 | for (const sift::CameraCalibration *candidate : |
| 33 | *training_data_->camera_calibrations()) { |
| 34 | if (candidate->node_name()->string_view() != node_name) { |
| 35 | continue; |
| 36 | } |
| 37 | if (candidate->team_number() != team_number) { |
| 38 | continue; |
| 39 | } |
| 40 | return candidate; |
| 41 | } |
| 42 | LOG(FATAL) << ": Failed to find camera calibration for " << node_name |
| 43 | << " on " << team_number; |
| 44 | } |
| 45 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 46 | void CameraReader::CopyTrainingFeatures() { |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 47 | int training_image_index = 0; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 48 | for (const sift::TrainingImage *training_image : *training_data_->images()) { |
| 49 | cv::Mat features(training_image->features()->size(), 128, CV_32F); |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 50 | for (size_t i = 0; i < training_image->features()->size(); ++i) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 51 | const sift::Feature *feature_table = training_image->features()->Get(i); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 52 | |
| 53 | // We don't need this information right now, but make sure it's here to |
| 54 | // avoid crashes that only occur when specific features are matched. |
| 55 | CHECK(feature_table->has_field_location()); |
| 56 | |
Brian Silverman | f64b9bd | 2020-02-29 12:51:33 -0800 | [diff] [blame] | 57 | const flatbuffers::Vector<uint8_t> *const descriptor = |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 58 | feature_table->descriptor(); |
| 59 | CHECK_EQ(descriptor->size(), 128u) << ": Unsupported feature size"; |
Brian Silverman | f64b9bd | 2020-02-29 12:51:33 -0800 | [diff] [blame] | 60 | const auto in_mat = cv::Mat( |
| 61 | 1, descriptor->size(), CV_8U, |
| 62 | const_cast<void *>(static_cast<const void *>(descriptor->data()))); |
| 63 | const auto out_mat = features(cv::Range(i, i + 1), cv::Range(0, 128)); |
| 64 | in_mat.convertTo(out_mat, CV_32F); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 65 | } |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 66 | matchers_[training_image_index].add(features); |
| 67 | ++training_image_index; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 71 | void CameraReader::SendImageMatchResult( |
| 72 | const CameraImage &image, const std::vector<cv::KeyPoint> &keypoints, |
| 73 | const cv::Mat &descriptors, |
| 74 | const std::vector<std::vector<cv::DMatch>> &matches, |
| 75 | const std::vector<cv::Mat> &camera_target_list, |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 76 | const std::vector<cv::Mat> &field_camera_list, |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 77 | const std::vector<cv::Point2f> &target_point_vector, |
| 78 | const std::vector<float> &target_radius_vector, |
James Kuszmaul | fac4ca6 | 2021-10-02 18:02:04 -0700 | [diff] [blame] | 79 | const std::vector<int> &training_image_indices, |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 80 | const std::vector<int> &homography_feature_counts, |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 81 | aos::Sender<sift::ImageMatchResult> *result_sender, bool send_details) { |
| 82 | auto builder = result_sender->MakeBuilder(); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 83 | const auto camera_calibration_offset = |
Austin Schuh | fa5ee95 | 2020-11-21 17:26:00 -0800 | [diff] [blame] | 84 | aos::RecursiveCopyFlatBuffer(camera_calibration_, builder.fbb()); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 85 | |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 86 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::Feature>>> |
| 87 | features_offset; |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 88 | flatbuffers::Offset< |
| 89 | flatbuffers::Vector<flatbuffers::Offset<sift::ImageMatch>>> |
| 90 | image_matches_offset; |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 91 | if (send_details) { |
| 92 | features_offset = PackFeatures(builder.fbb(), keypoints, descriptors); |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 93 | image_matches_offset = PackImageMatches(builder.fbb(), matches); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 96 | std::vector<flatbuffers::Offset<sift::CameraPose>> camera_poses; |
| 97 | |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 98 | CHECK_EQ(camera_target_list.size(), field_camera_list.size()); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 99 | for (size_t i = 0; i < camera_target_list.size(); ++i) { |
| 100 | cv::Mat camera_target = camera_target_list[i]; |
| 101 | CHECK(camera_target.isContinuous()); |
| 102 | const auto data_offset = builder.fbb()->CreateVector<float>( |
| 103 | reinterpret_cast<float *>(camera_target.data), camera_target.total()); |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 104 | const flatbuffers::Offset<sift::TransformationMatrix> transform_offset = |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 105 | sift::CreateTransformationMatrix(*builder.fbb(), data_offset); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 106 | |
| 107 | cv::Mat field_camera = field_camera_list[i]; |
| 108 | CHECK(field_camera.isContinuous()); |
| 109 | const auto fc_data_offset = builder.fbb()->CreateVector<float>( |
| 110 | reinterpret_cast<float *>(field_camera.data), field_camera.total()); |
| 111 | const flatbuffers::Offset<sift::TransformationMatrix> fc_transform_offset = |
| 112 | sift::CreateTransformationMatrix(*builder.fbb(), fc_data_offset); |
| 113 | |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 114 | const flatbuffers::Offset<sift::TransformationMatrix> |
James Kuszmaul | fac4ca6 | 2021-10-02 18:02:04 -0700 | [diff] [blame] | 115 | field_to_target_offset = aos::RecursiveCopyFlatBuffer( |
| 116 | FieldToTarget(training_image_indices[i]), builder.fbb()); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 117 | |
| 118 | sift::CameraPose::Builder pose_builder(*builder.fbb()); |
| 119 | pose_builder.add_camera_to_target(transform_offset); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 120 | pose_builder.add_field_to_camera(fc_transform_offset); |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 121 | pose_builder.add_field_to_target(field_to_target_offset); |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 122 | pose_builder.add_query_target_point_x(target_point_vector[i].x); |
| 123 | pose_builder.add_query_target_point_y(target_point_vector[i].y); |
| 124 | pose_builder.add_query_target_point_radius(target_radius_vector[i]); |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 125 | pose_builder.add_homography_feature_count(homography_feature_counts[i]); |
| 126 | pose_builder.add_training_image_index(training_image_indices[i]); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 127 | camera_poses.emplace_back(pose_builder.Finish()); |
| 128 | } |
| 129 | const auto camera_poses_offset = builder.fbb()->CreateVector(camera_poses); |
| 130 | |
| 131 | sift::ImageMatchResult::Builder result_builder(*builder.fbb()); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 132 | result_builder.add_camera_poses(camera_poses_offset); |
| 133 | if (send_details) { |
Austin Schuh | 6f3640a | 2020-02-28 22:13:36 -0800 | [diff] [blame] | 134 | result_builder.add_image_matches(image_matches_offset); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 135 | result_builder.add_features(features_offset); |
| 136 | } |
| 137 | result_builder.add_image_monotonic_timestamp_ns( |
| 138 | image.monotonic_timestamp_ns()); |
| 139 | result_builder.add_camera_calibration(camera_calibration_offset); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 140 | result_builder.add_send_failures(result_failure_counter_.failures()); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 141 | |
| 142 | // TODO<Jim>: Need to add target point computed from matches and |
| 143 | // mapped by homography |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 144 | result_failure_counter_.Count(builder.Send(result_builder.Finish())); |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void CameraReader::ProcessImage(const CameraImage &image) { |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 148 | // First, we need to extract the brightness information. This can't really be |
| 149 | // fused into the beginning of the SIFT algorithm because the algorithm needs |
| 150 | // to look at the base image directly. It also only takes 2ms on our images. |
| 151 | // This is converting from YUYV to a grayscale image. |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 152 | cv::Mat image_mat(image.rows(), image.cols(), CV_8U); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 153 | CHECK(image_mat.isContinuous()); |
| 154 | const int number_pixels = image.rows() * image.cols(); |
| 155 | for (int i = 0; i < number_pixels; ++i) { |
| 156 | reinterpret_cast<uint8_t *>(image_mat.data)[i] = |
| 157 | image.data()->data()[i * 2]; |
| 158 | } |
| 159 | |
| 160 | // Next, grab the features from the image. |
| 161 | std::vector<cv::KeyPoint> keypoints; |
Austin Schuh | 7256aea | 2020-03-28 18:06:46 -0700 | [diff] [blame] | 162 | |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 163 | cv::Mat descriptors; |
Austin Schuh | 7256aea | 2020-03-28 18:06:46 -0700 | [diff] [blame] | 164 | if (!FLAGS_skip_sift) { |
| 165 | sift_->detectAndCompute(image_mat, cv::noArray(), keypoints, descriptors); |
| 166 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 167 | |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 168 | struct PerImageMatches { |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 169 | std::vector<std::vector<cv::DMatch>> matches; |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 170 | std::vector<cv::Point3f> training_points_3d; |
| 171 | std::vector<cv::Point2f> query_points; |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 172 | std::vector<cv::Point2f> training_points; |
| 173 | cv::Mat homography; |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 174 | }; |
| 175 | std::vector<PerImageMatches> per_image_matches(number_training_images()); |
| 176 | |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 177 | for (int image_idx = 0; image_idx < number_training_images(); ++image_idx) { |
| 178 | // Then, match those features against our training data. |
| 179 | std::vector<std::vector<cv::DMatch>> matches; |
| 180 | if (!FLAGS_skip_sift) { |
| 181 | matchers_[image_idx].knnMatch(/* queryDescriptors */ descriptors, matches, |
| 182 | /* k */ 2); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 183 | } |
| 184 | |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 185 | // Pull out the good matches which we want for each image. |
| 186 | // Discard the bad matches per Lowe's ratio test. |
| 187 | // (Lowe originally proposed 0.7 ratio, but 0.75 was later proposed as a |
| 188 | // better option. We'll go with the more conservative (fewer, better |
| 189 | // matches) for now). |
| 190 | for (const std::vector<cv::DMatch> &match : matches) { |
| 191 | CHECK_EQ(2u, match.size()); |
| 192 | CHECK_LE(match[0].distance, match[1].distance); |
| 193 | CHECK_EQ(match[0].imgIdx, 0); |
| 194 | CHECK_EQ(match[1].imgIdx, 0); |
| 195 | CHECK_EQ(match[0].queryIdx, match[1].queryIdx); |
| 196 | if (!(match[0].distance < 0.7 * match[1].distance)) { |
| 197 | continue; |
| 198 | } |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 199 | |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 200 | const int training_image = image_idx; |
| 201 | CHECK_LT(training_image, static_cast<int>(per_image_matches.size())); |
| 202 | PerImageMatches *const per_image = &per_image_matches[training_image]; |
| 203 | per_image->matches.push_back(match); |
| 204 | per_image->matches.back()[0].imgIdx = image_idx; |
| 205 | per_image->matches.back()[1].imgIdx = image_idx; |
| 206 | per_image->training_points.push_back( |
| 207 | Training2dPoint(training_image, match[0].trainIdx)); |
| 208 | per_image->training_points_3d.push_back( |
| 209 | Training3dPoint(training_image, match[0].trainIdx)); |
| 210 | |
| 211 | const cv::KeyPoint &keypoint = keypoints[match[0].queryIdx]; |
| 212 | per_image->query_points.push_back(keypoint.pt); |
| 213 | } |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // The minimum number of matches in a training image for us to use it. |
| 217 | static constexpr int kMinimumMatchCount = 10; |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 218 | std::vector<cv::Mat> camera_target_list; |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 219 | std::vector<cv::Mat> field_camera_list; |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 220 | |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 221 | // Rebuild the matches and store them here |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 222 | std::vector<std::vector<cv::DMatch>> all_good_matches; |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 223 | // Build list of target point and radius for each good match |
| 224 | std::vector<cv::Point2f> target_point_vector; |
| 225 | std::vector<float> target_radius_vector; |
James Kuszmaul | fac4ca6 | 2021-10-02 18:02:04 -0700 | [diff] [blame] | 226 | std::vector<int> training_image_indices; |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 227 | std::vector<int> homography_feature_counts; |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 228 | |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 229 | // Iterate through matches for each training image |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 230 | for (size_t i = 0; i < per_image_matches.size(); ++i) { |
| 231 | const PerImageMatches &per_image = per_image_matches[i]; |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 232 | |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame] | 233 | VLOG(2) << "Number of matches to start for training image: " << i |
| 234 | << " is: " << per_image.matches.size() << "\n"; |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 235 | // 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] | 236 | if (per_image.matches.size() < kMinimumMatchCount) { |
| 237 | continue; |
| 238 | } |
| 239 | |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 240 | // Use homography to determine which matches make sense physically |
| 241 | cv::Mat mask; |
| 242 | cv::Mat homography = |
| 243 | cv::findHomography(per_image.training_points, per_image.query_points, |
Brian Silverman | 4c7235a | 2021-11-17 19:04:37 -0800 | [diff] [blame^] | 244 | cv::FM_RANSAC, 3.0, mask); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 245 | |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 246 | const int homography_feature_count = cv::countNonZero(mask); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 247 | // If mask doesn't have enough leftover matches, skip these matches |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 248 | if (homography_feature_count < kMinimumMatchCount) { |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 249 | continue; |
| 250 | } |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 251 | homography_feature_counts.push_back(homography_feature_count); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 252 | |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame] | 253 | VLOG(2) << "Number of matches after homography for training image: " << i |
| 254 | << " is " << cv::countNonZero(mask) << "\n"; |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 255 | |
| 256 | // Fill our match info for each good match based on homography result |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 257 | PerImageMatches per_image_good_match; |
| 258 | CHECK_EQ(per_image.training_points.size(), |
| 259 | (unsigned long)mask.size().height); |
| 260 | for (size_t j = 0; j < per_image.matches.size(); j++) { |
| 261 | // Skip if we masked out by homography |
| 262 | if (mask.at<uchar>(0, j) != 1) { |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | // Add this to our collection of all matches that passed our criteria |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 267 | all_good_matches.push_back(per_image.matches[j]); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 268 | |
| 269 | // Fill out the data for matches per image that made it past |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 270 | // homography check, for later use |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 271 | per_image_good_match.matches.push_back(per_image.matches[j]); |
| 272 | per_image_good_match.training_points.push_back( |
| 273 | per_image.training_points[j]); |
| 274 | per_image_good_match.training_points_3d.push_back( |
| 275 | per_image.training_points_3d[j]); |
| 276 | 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] | 277 | } |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 278 | |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 279 | // Returns from opencv are doubles (CV_64F), which don't play well |
| 280 | // with our floats |
| 281 | homography.convertTo(homography, CV_32F); |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame] | 282 | per_image_good_match.homography = homography.clone(); |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 283 | |
| 284 | CHECK_GT(per_image_good_match.matches.size(), 0u); |
| 285 | |
| 286 | // Collect training target location, so we can map it to matched image |
| 287 | cv::Point2f target_point; |
| 288 | float target_radius; |
James Kuszmaul | e3e79b0 | 2021-10-02 20:39:33 -0700 | [diff] [blame] | 289 | TargetLocation(i, target_point, target_radius); |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 290 | |
| 291 | // Store target_point in vector for use by perspectiveTransform |
| 292 | std::vector<cv::Point2f> src_target_pt; |
| 293 | src_target_pt.push_back(target_point); |
| 294 | std::vector<cv::Point2f> query_target_pt; |
| 295 | |
| 296 | cv::perspectiveTransform(src_target_pt, query_target_pt, homography); |
| 297 | |
| 298 | float query_target_radius = |
| 299 | target_radius * |
| 300 | abs(homography.at<float>(0, 0) + homography.at<float>(1, 1)) / 2.; |
| 301 | |
| 302 | CHECK_EQ(query_target_pt.size(), 1u); |
| 303 | target_point_vector.push_back(query_target_pt[0]); |
| 304 | target_radius_vector.push_back(query_target_radius); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 305 | |
| 306 | // Pose transformations (rotations and translations) for various |
| 307 | // coordinate frames. R_X_Y_vec is the Rodrigues (angle-axis) |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 308 | // 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] | 309 | |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 310 | // Tranform from camera to target frame |
Jim Ostrowski | 295f2a1 | 2020-02-26 22:22:44 -0800 | [diff] [blame] | 311 | cv::Mat R_camera_target_vec, R_camera_target, T_camera_target; |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 312 | // Tranform from camera to field origin (global) reference frame |
| 313 | cv::Mat R_camera_field_vec, R_camera_field, T_camera_field; |
| 314 | // Inverse of camera to field-- defines location of camera in |
| 315 | // global (field) reference frame |
| 316 | cv::Mat R_field_camera_vec, R_field_camera, T_field_camera; |
| 317 | |
Jim Ostrowski | 3a19348 | 2021-09-11 13:21:42 -0700 | [diff] [blame] | 318 | // Using the previous pose helps to stabilize the estimate, since |
| 319 | // it's sometimes bouncing between two possible poses. Putting it |
| 320 | // near the previous pose helps it converge to the previous pose |
| 321 | // estimate (assuming it's valid). |
| 322 | if (FLAGS_use_prev_pose) { |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame] | 323 | R_camera_field_vec = prev_camera_field_R_vec_list_[i].clone(); |
| 324 | T_camera_field = prev_camera_field_T_list_[i].clone(); |
| 325 | VLOG(2) << "Using previous match for training image " << i |
| 326 | << " with T of : " << T_camera_field; |
Jim Ostrowski | 3a19348 | 2021-09-11 13:21:42 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Jim Ostrowski | 295f2a1 | 2020-02-26 22:22:44 -0800 | [diff] [blame] | 329 | // Compute the pose of the camera (global origin relative to camera) |
Jim Ostrowski | 39b4f0a | 2021-08-28 15:43:01 -0700 | [diff] [blame] | 330 | if (FLAGS_ransac_pose) { |
| 331 | // RANSAC computation is designed to be more robust to outliers. |
| 332 | // But, we found it bounces around a lot, even with identical points |
| 333 | cv::solvePnPRansac(per_image_good_match.training_points_3d, |
| 334 | per_image_good_match.query_points, CameraIntrinsics(), |
Jim Ostrowski | 3a19348 | 2021-09-11 13:21:42 -0700 | [diff] [blame] | 335 | CameraDistCoeffs(), R_camera_field_vec, T_camera_field, |
| 336 | FLAGS_use_prev_pose); |
Jim Ostrowski | 39b4f0a | 2021-08-28 15:43:01 -0700 | [diff] [blame] | 337 | } else { |
| 338 | // ITERATIVE mode is potentially less robust to outliers, but we |
| 339 | // found it to be more stable |
| 340 | // |
Jim Ostrowski | 39b4f0a | 2021-08-28 15:43:01 -0700 | [diff] [blame] | 341 | cv::solvePnP(per_image_good_match.training_points_3d, |
| 342 | per_image_good_match.query_points, CameraIntrinsics(), |
| 343 | CameraDistCoeffs(), R_camera_field_vec, T_camera_field, |
Brian Silverman | 4c7235a | 2021-11-17 19:04:37 -0800 | [diff] [blame^] | 344 | FLAGS_use_prev_pose, cv::SOLVEPNP_ITERATIVE); |
Jim Ostrowski | 39b4f0a | 2021-08-28 15:43:01 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Jim Ostrowski | a3fd387 | 2021-10-09 19:44:18 -0700 | [diff] [blame] | 347 | // We are occasionally seeing NaN in the prior estimate, so checking for |
| 348 | // this If we sit, just bail the pose estimate |
| 349 | if (isnan(T_camera_field.at<double>(0, 0))) { |
| 350 | LOG(ERROR) |
| 351 | << "NAN ERROR in solving for Pose (SolvePnP). Pose returned as: T: " |
| 352 | << T_camera_field << "\nR: " << R_camera_field_vec |
| 353 | << "\nNumber of matches is: " |
| 354 | << per_image_good_match.query_points.size(); |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame] | 355 | VLOG(2) << "Resetting previous values to zero, from: R_prev: " |
| 356 | << prev_camera_field_R_vec_list_[i] |
| 357 | << ", T_prev: " << prev_camera_field_T_list_[i]; |
| 358 | prev_camera_field_R_vec_list_[i] = cv::Mat::zeros(3, 1, CV_32F); |
| 359 | prev_camera_field_T_list_[i] = cv::Mat::zeros(3, 1, CV_32F); |
Jim Ostrowski | a3fd387 | 2021-10-09 19:44:18 -0700 | [diff] [blame] | 360 | |
Jim Ostrowski | 2e67a1b | 2021-10-13 20:24:43 -0700 | [diff] [blame] | 361 | continue; |
Jim Ostrowski | a3fd387 | 2021-10-09 19:44:18 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 364 | CHECK_EQ(cv::Size(1, 3), T_camera_field.size()); |
| 365 | |
| 366 | // Convert to float32's (from float64) to be compatible with the rest |
| 367 | R_camera_field_vec.convertTo(R_camera_field_vec, CV_32F); |
| 368 | T_camera_field.convertTo(T_camera_field, CV_32F); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 369 | |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 370 | // Get matrix version of R_camera_field |
| 371 | cv::Rodrigues(R_camera_field_vec, R_camera_field); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 372 | CHECK_EQ(cv::Size(3, 3), R_camera_field.size()); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 373 | |
| 374 | // Compute H_field_camera = H_camera_field^-1 |
| 375 | R_field_camera = R_camera_field.t(); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 376 | T_field_camera = -R_field_camera * (T_camera_field); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 377 | |
| 378 | // Extract the field_target transformation |
| 379 | const cv::Mat H_field_target(4, 4, CV_32F, |
| 380 | const_cast<void *>(static_cast<const void *>( |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 381 | FieldToTarget(i)->data()->data()))); |
| 382 | |
James Kuszmaul | fac4ca6 | 2021-10-02 18:02:04 -0700 | [diff] [blame] | 383 | training_image_indices.push_back(i); |
| 384 | |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 385 | const cv::Mat R_field_target = |
| 386 | H_field_target(cv::Range(0, 3), cv::Range(0, 3)); |
| 387 | const cv::Mat T_field_target = |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 388 | H_field_target(cv::Range(0, 3), cv::Range(3, 4)); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 389 | |
| 390 | // Use it to get the relative pose from camera to target |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 391 | R_camera_target = R_camera_field * (R_field_target); |
| 392 | T_camera_target = R_camera_field * (T_field_target) + T_camera_field; |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 393 | |
| 394 | // Set H_camera_target |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 395 | { |
Jim Ostrowski | 38bb70b | 2020-02-21 20:46:10 -0800 | [diff] [blame] | 396 | CHECK_EQ(cv::Size(3, 3), R_camera_target.size()); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 397 | CHECK_EQ(cv::Size(1, 3), T_camera_target.size()); |
| 398 | cv::Mat H_camera_target = cv::Mat::zeros(4, 4, CV_32F); |
| 399 | R_camera_target.copyTo(H_camera_target(cv::Range(0, 3), cv::Range(0, 3))); |
| 400 | T_camera_target.copyTo(H_camera_target(cv::Range(0, 3), cv::Range(3, 4))); |
| 401 | H_camera_target.at<float>(3, 3) = 1; |
| 402 | CHECK(H_camera_target.isContinuous()); |
| 403 | camera_target_list.push_back(H_camera_target.clone()); |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | // Set H_field_camera |
| 407 | { |
| 408 | CHECK_EQ(cv::Size(3, 3), R_field_camera.size()); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 409 | CHECK_EQ(cv::Size(1, 3), T_field_camera.size()); |
| 410 | cv::Mat H_field_camera = cv::Mat::zeros(4, 4, CV_32F); |
| 411 | R_field_camera.copyTo(H_field_camera(cv::Range(0, 3), cv::Range(0, 3))); |
| 412 | T_field_camera.copyTo(H_field_camera(cv::Range(0, 3), cv::Range(3, 4))); |
| 413 | H_field_camera.at<float>(3, 3) = 1; |
| 414 | CHECK(H_field_camera.isContinuous()); |
| 415 | field_camera_list.push_back(H_field_camera.clone()); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 416 | } |
Jim Ostrowski | 2e67a1b | 2021-10-13 20:24:43 -0700 | [diff] [blame] | 417 | |
| 418 | // We also sometimes see estimates where the target is behind the camera |
| 419 | // or where we have very large pose estimates. |
| 420 | // This will generally lead to an estimate that is off the field, and also |
| 421 | // will mess up the |
| 422 | if (T_camera_target.at<float>(0, 2) < 0.0 || |
| 423 | T_camera_target.at<float>(0, 2) > 100.0) { |
| 424 | LOG(ERROR) << "Pose returned non-physical pose with camera to target z. " |
| 425 | "T_camera_target = " |
| 426 | << T_camera_target |
| 427 | << "\nAnd T_field_camera = " << T_field_camera; |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame] | 428 | VLOG(2) << "Resetting previous values to zero, from: R_prev: " |
| 429 | << prev_camera_field_R_vec_list_[i] |
| 430 | << ", T_prev: " << prev_camera_field_T_list_[i]; |
| 431 | prev_camera_field_R_vec_list_[i] = cv::Mat::zeros(3, 1, CV_32F); |
| 432 | prev_camera_field_T_list_[i] = cv::Mat::zeros(3, 1, CV_32F); |
Jim Ostrowski | 2e67a1b | 2021-10-13 20:24:43 -0700 | [diff] [blame] | 433 | continue; |
| 434 | } |
| 435 | |
Jim Ostrowski | ebf8cd2 | 2021-10-30 01:08:20 -0700 | [diff] [blame] | 436 | prev_camera_field_R_vec_list_[i] = R_camera_field_vec.clone(); |
| 437 | prev_camera_field_T_list_[i] = T_camera_field.clone(); |
Brian Silverman | 4770c7d | 2020-02-17 20:34:42 -0800 | [diff] [blame] | 438 | } |
Jim Ostrowski | ad5d8a7 | 2020-02-28 00:15:26 -0800 | [diff] [blame] | 439 | // Now, send our two messages-- one large, with details for remote |
| 440 | // debugging(features), and one smaller |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 441 | SendImageMatchResult(image, keypoints, descriptors, all_good_matches, |
Jim Ostrowski | e426426 | 2020-02-29 00:27:24 -0800 | [diff] [blame] | 442 | camera_target_list, field_camera_list, |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 443 | target_point_vector, target_radius_vector, |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 444 | training_image_indices, homography_feature_counts, |
| 445 | &detailed_result_sender_, true); |
Jim Ostrowski | 1075cac | 2020-02-29 15:18:13 -0800 | [diff] [blame] | 446 | SendImageMatchResult(image, keypoints, descriptors, all_good_matches, |
Jim Ostrowski | 18f7fbf | 2020-03-01 13:53:22 -0800 | [diff] [blame] | 447 | camera_target_list, field_camera_list, |
| 448 | target_point_vector, target_radius_vector, |
James Kuszmaul | 9c65835 | 2021-10-22 19:37:58 -0700 | [diff] [blame] | 449 | training_image_indices, homography_feature_counts, |
| 450 | &result_sender_, false); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | void CameraReader::ReadImage() { |
| 454 | if (!reader_->ReadLatestImage()) { |
Austin Schuh | 7256aea | 2020-03-28 18:06:46 -0700 | [diff] [blame] | 455 | if (!FLAGS_skip_sift) { |
| 456 | LOG(INFO) << "No image, sleeping"; |
| 457 | } |
| 458 | read_image_timer_->Setup(event_loop_->monotonic_now() + |
| 459 | std::chrono::milliseconds(10)); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | |
| 463 | ProcessImage(reader_->LatestImage()); |
| 464 | |
| 465 | reader_->SendLatestImage(); |
Austin Schuh | 7256aea | 2020-03-28 18:06:46 -0700 | [diff] [blame] | 466 | read_image_timer_->Setup(event_loop_->monotonic_now()); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::ImageMatch>>> |
| 470 | CameraReader::PackImageMatches( |
| 471 | flatbuffers::FlatBufferBuilder *fbb, |
| 472 | const std::vector<std::vector<cv::DMatch>> &matches) { |
| 473 | // First, we need to pull out all the matches for each image. Might as well |
| 474 | // build up the Match tables at the same time. |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 475 | std::vector<std::vector<sift::Match>> per_image_matches( |
| 476 | number_training_images()); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 477 | for (const std::vector<cv::DMatch> &image_matches : matches) { |
Jim Ostrowski | ce02ebe | 2020-03-07 20:39:02 -0800 | [diff] [blame] | 478 | CHECK_GT(image_matches.size(), 0u); |
| 479 | // We're only using the first of the two matches |
| 480 | const cv::DMatch &image_match = image_matches[0]; |
| 481 | CHECK_LT(image_match.imgIdx, number_training_images()); |
| 482 | per_image_matches[image_match.imgIdx].emplace_back(); |
| 483 | sift::Match *const match = &per_image_matches[image_match.imgIdx].back(); |
| 484 | match->mutate_query_feature(image_match.queryIdx); |
| 485 | match->mutate_train_feature(image_match.trainIdx); |
| 486 | match->mutate_distance(image_match.distance); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | // Then, we need to build up each ImageMatch table. |
| 490 | std::vector<flatbuffers::Offset<sift::ImageMatch>> image_match_tables; |
| 491 | for (size_t i = 0; i < per_image_matches.size(); ++i) { |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 492 | const std::vector<sift::Match> &this_image_matches = per_image_matches[i]; |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 493 | if (this_image_matches.empty()) { |
| 494 | continue; |
| 495 | } |
Brian Silverman | 4d4a70d | 2020-02-17 13:03:19 -0800 | [diff] [blame] | 496 | const auto vector_offset = fbb->CreateVectorOfStructs(this_image_matches); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 497 | sift::ImageMatch::Builder image_builder(*fbb); |
| 498 | image_builder.add_train_image(i); |
| 499 | image_builder.add_matches(vector_offset); |
| 500 | image_match_tables.emplace_back(image_builder.Finish()); |
| 501 | } |
| 502 | |
| 503 | return fbb->CreateVector(image_match_tables); |
| 504 | } |
| 505 | |
| 506 | flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<sift::Feature>>> |
| 507 | CameraReader::PackFeatures(flatbuffers::FlatBufferBuilder *fbb, |
| 508 | const std::vector<cv::KeyPoint> &keypoints, |
| 509 | const cv::Mat &descriptors) { |
| 510 | const int number_features = keypoints.size(); |
| 511 | CHECK_EQ(descriptors.rows, number_features); |
Austin Schuh | 7256aea | 2020-03-28 18:06:46 -0700 | [diff] [blame] | 512 | if (number_features != 0) { |
| 513 | CHECK_EQ(descriptors.cols, 128); |
| 514 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 515 | std::vector<flatbuffers::Offset<sift::Feature>> features_vector( |
| 516 | number_features); |
| 517 | for (int i = 0; i < number_features; ++i) { |
Brian Silverman | f64b9bd | 2020-02-29 12:51:33 -0800 | [diff] [blame] | 518 | const auto submat = |
| 519 | descriptors(cv::Range(i, i + 1), cv::Range(0, descriptors.cols)); |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 520 | CHECK(submat.isContinuous()); |
Brian Silverman | f64b9bd | 2020-02-29 12:51:33 -0800 | [diff] [blame] | 521 | flatbuffers::Offset<flatbuffers::Vector<uint8_t>> descriptor_offset; |
| 522 | { |
| 523 | uint8_t *data; |
| 524 | descriptor_offset = fbb->CreateUninitializedVector(128, &data); |
| 525 | submat.convertTo( |
| 526 | cv::Mat(1, descriptors.cols, CV_8U, static_cast<void *>(data)), |
| 527 | CV_8U); |
| 528 | } |
Brian Silverman | 967e5df | 2020-02-09 16:43:34 -0800 | [diff] [blame] | 529 | sift::Feature::Builder feature_builder(*fbb); |
| 530 | feature_builder.add_descriptor(descriptor_offset); |
| 531 | feature_builder.add_x(keypoints[i].pt.x); |
| 532 | feature_builder.add_y(keypoints[i].pt.y); |
| 533 | feature_builder.add_size(keypoints[i].size); |
| 534 | feature_builder.add_angle(keypoints[i].angle); |
| 535 | feature_builder.add_response(keypoints[i].response); |
| 536 | feature_builder.add_octave(keypoints[i].octave); |
| 537 | CHECK_EQ(-1, keypoints[i].class_id) |
| 538 | << ": Not sure what to do with a class id"; |
| 539 | features_vector[i] = feature_builder.Finish(); |
| 540 | } |
| 541 | return fbb->CreateVector(features_vector); |
| 542 | } |
| 543 | |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 544 | } // namespace vision |
| 545 | } // namespace frc971 |