Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 1 | #include <map> |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 2 | #include <opencv2/calib3d.hpp> |
| 3 | #include <opencv2/features2d.hpp> |
| 4 | #include <opencv2/highgui/highgui.hpp> |
| 5 | #include <opencv2/imgproc.hpp> |
Jim Ostrowski | 56c0932 | 2021-10-23 16:17:21 -0700 | [diff] [blame] | 6 | #include <random> |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 7 | |
| 8 | #include "aos/events/shm_event_loop.h" |
| 9 | #include "aos/init.h" |
Jim Ostrowski | baa4369 | 2020-03-08 16:25:10 -0700 | [diff] [blame] | 10 | #include "aos/time/time.h" |
Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 11 | #include "y2020/vision/sift/sift_generated.h" |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 12 | #include "y2020/vision/vision_generated.h" |
| 13 | |
| 14 | DEFINE_string(config, "config.json", "Path to the config file to use."); |
Jim Ostrowski | 56c0932 | 2021-10-23 16:17:21 -0700 | [diff] [blame] | 15 | DEFINE_bool(show_features, true, "Show the SIFT features that matched."); |
Austin Schuh | 69b2f79 | 2020-03-15 14:31:55 -0700 | [diff] [blame] | 16 | DEFINE_string(channel, "/camera", "Channel name for the image."); |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 17 | |
Austin Schuh | 5776312 | 2021-11-06 20:49:18 -0700 | [diff] [blame] | 18 | DEFINE_string(capture, "", |
| 19 | "If set, capture a single image and save it to this filename."); |
| 20 | |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 21 | namespace frc971 { |
| 22 | namespace vision { |
| 23 | namespace { |
| 24 | |
Jim Ostrowski | 834dddf | 2021-04-10 14:40:19 -0700 | [diff] [blame] | 25 | aos::Fetcher<CameraImage> image_fetcher; |
| 26 | aos::Fetcher<sift::ImageMatchResult> match_fetcher; |
Jim Ostrowski | 56c0932 | 2021-10-23 16:17:21 -0700 | [diff] [blame] | 27 | cv::Mat palette_; |
Jim Ostrowski | 834dddf | 2021-04-10 14:40:19 -0700 | [diff] [blame] | 28 | |
| 29 | bool DisplayLoop() { |
Jim Ostrowski | 56c0932 | 2021-10-23 16:17:21 -0700 | [diff] [blame] | 30 | // Try to get target match data |
| 31 | int64_t match_timestamp = 0; |
| 32 | const sift::ImageMatchResult *match; |
Jim Ostrowski | 834dddf | 2021-04-10 14:40:19 -0700 | [diff] [blame] | 33 | if (match_fetcher.Fetch()) { |
Jim Ostrowski | 56c0932 | 2021-10-23 16:17:21 -0700 | [diff] [blame] | 34 | match = match_fetcher.get(); |
Jim Ostrowski | 834dddf | 2021-04-10 14:40:19 -0700 | [diff] [blame] | 35 | CHECK(match != nullptr) << "Got null when trying to fetch match result"; |
| 36 | |
Jim Ostrowski | 56c0932 | 2021-10-23 16:17:21 -0700 | [diff] [blame] | 37 | match_timestamp = match->image_monotonic_timestamp_ns(); |
| 38 | if (match->camera_poses() != nullptr && match->camera_poses()->size() > 0) { |
| 39 | VLOG(2) << "Got matches for timestamp " << match_timestamp << "\n"; |
| 40 | } |
| 41 | } else { |
| 42 | VLOG(2) << "Didn't get match this cycle"; |
| 43 | } |
| 44 | |
| 45 | int64_t image_timestamp = 0; |
| 46 | bool matching_image_found = false; |
| 47 | const CameraImage *image; |
| 48 | while (!matching_image_found) { |
| 49 | // Read next image |
| 50 | if (!image_fetcher.FetchNext()) { |
| 51 | VLOG(2) << "Couldn't fetch next image"; |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | image = image_fetcher.get(); |
| 56 | CHECK(image != nullptr) << "Couldn't read image"; |
| 57 | image_timestamp = image->monotonic_timestamp_ns(); |
| 58 | VLOG(2) << "Got image at timestamp: " << image_timestamp; |
| 59 | |
| 60 | if (match_timestamp != 0 && image_timestamp == match_timestamp) { |
| 61 | matching_image_found = true; |
| 62 | } else if (image_timestamp > match_timestamp) { |
| 63 | LOG(INFO) << "Image timestamp went past match_timestamp"; |
| 64 | return true; |
Jim Ostrowski | 834dddf | 2021-04-10 14:40:19 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Jim Ostrowski | 834dddf | 2021-04-10 14:40:19 -0700 | [diff] [blame] | 68 | // Create color image: |
| 69 | cv::Mat image_color_mat(cv::Size(image->cols(), image->rows()), CV_8UC2, |
| 70 | (void *)image->data()->data()); |
| 71 | cv::Mat rgb_image(cv::Size(image->cols(), image->rows()), CV_8UC3); |
Brian Silverman | 4c7235a | 2021-11-17 19:04:37 -0800 | [diff] [blame^] | 72 | cv::cvtColor(image_color_mat, rgb_image, cv::COLOR_YUV2BGR_YUYV); |
Jim Ostrowski | 834dddf | 2021-04-10 14:40:19 -0700 | [diff] [blame] | 73 | |
Austin Schuh | 5776312 | 2021-11-06 20:49:18 -0700 | [diff] [blame] | 74 | if (!FLAGS_capture.empty()) { |
| 75 | cv::imwrite(FLAGS_capture, rgb_image); |
| 76 | return false; |
| 77 | } |
| 78 | |
Jim Ostrowski | 56c0932 | 2021-10-23 16:17:21 -0700 | [diff] [blame] | 79 | if (matching_image_found) { |
| 80 | // Draw whatever matches we have |
| 81 | if (match->camera_poses() != nullptr && match->camera_poses()->size() > 0) { |
| 82 | // Draw target point in image |
| 83 | float x = match->camera_poses()->Get(0)->query_target_point_x(); |
| 84 | float y = match->camera_poses()->Get(0)->query_target_point_y(); |
| 85 | float radius = match->camera_poses()->Get(0)->query_target_point_radius(); |
| 86 | cv::circle(rgb_image, cv::Point2f(x, y), radius, cv::Scalar(0, 255, 0), |
| 87 | 5); |
| 88 | } |
| 89 | |
| 90 | if (FLAGS_show_features && match->image_matches() != nullptr && |
| 91 | match->image_matches()->size() > 0) { |
| 92 | // Iterate through matches and draw matched keypoints |
| 93 | for (uint model_match_ind = 0; |
| 94 | model_match_ind < match->image_matches()->size(); |
| 95 | model_match_ind++) { |
| 96 | auto match_list = |
| 97 | match->image_matches()->Get(model_match_ind)->matches(); |
| 98 | if (match_list != nullptr && match_list->size() > 0) { |
| 99 | int train_image_ind = |
| 100 | match->image_matches()->Get(model_match_ind)->train_image(); |
| 101 | VLOG(2) << "Got " << match_list->size() << " matches to model " |
| 102 | << train_image_ind; |
| 103 | |
| 104 | // Picking color from palette and drawing |
| 105 | auto color = palette_.at<cv::Vec3b>(train_image_ind % palette_.cols); |
| 106 | LOG(INFO) << "Using color " << color; |
| 107 | for (uint i = 0; i < match_list->size(); i++) { |
| 108 | uint query_feature_ind = match_list->Get(i)->query_feature(); |
| 109 | float kp_x = match->features()->Get(query_feature_ind)->x(); |
| 110 | float kp_y = match->features()->Get(query_feature_ind)->y(); |
| 111 | cv::circle(rgb_image, cv::Point2f(kp_x, kp_y), 5, color, 2); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
Jim Ostrowski | 834dddf | 2021-04-10 14:40:19 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | cv::imshow("Display", rgb_image); |
| 119 | int keystroke = cv::waitKey(1); |
| 120 | if ((keystroke & 0xFF) == static_cast<int>('c')) { |
| 121 | // Convert again, to get clean image |
Brian Silverman | 4c7235a | 2021-11-17 19:04:37 -0800 | [diff] [blame^] | 122 | cv::cvtColor(image_color_mat, rgb_image, cv::COLOR_YUV2BGR_YUYV); |
Jim Ostrowski | 834dddf | 2021-04-10 14:40:19 -0700 | [diff] [blame] | 123 | std::stringstream name; |
| 124 | name << "capture-" << aos::realtime_clock::now() << ".png"; |
| 125 | cv::imwrite(name.str(), rgb_image); |
| 126 | LOG(INFO) << "Saved image file: " << name.str(); |
| 127 | } else if ((keystroke & 0xFF) == static_cast<int>('q')) { |
| 128 | return false; |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | void ViewerMain() { |
Jim Ostrowski | 56c0932 | 2021-10-23 16:17:21 -0700 | [diff] [blame] | 134 | // Create random color palette for distinguishing multiple models |
| 135 | uchar colors[5][3] = { |
| 136 | {0, 0, 255}, {0, 165, 255}, {0, 255, 255}, {255, 0, 0}, {128, 0, 128}}; |
| 137 | palette_ = cv::Mat(3, 5, CV_8U, &colors); |
Jim Ostrowski | c560cbe | 2020-03-07 00:29:30 -0800 | [diff] [blame] | 138 | |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 139 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 140 | aos::configuration::ReadConfig(FLAGS_config); |
| 141 | |
| 142 | aos::ShmEventLoop event_loop(&config.message()); |
| 143 | |
Jim Ostrowski | 834dddf | 2021-04-10 14:40:19 -0700 | [diff] [blame] | 144 | image_fetcher = event_loop.MakeFetcher<CameraImage>(FLAGS_channel); |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 145 | |
Jim Ostrowski | 56c0932 | 2021-10-23 16:17:21 -0700 | [diff] [blame] | 146 | // If we want to show the features, we have to use the detailed channel |
| 147 | std::string result_channel = FLAGS_channel; |
| 148 | if (FLAGS_show_features) { |
| 149 | result_channel += "/detailed"; |
| 150 | } |
| 151 | match_fetcher = |
| 152 | event_loop.MakeFetcher<sift::ImageMatchResult>(result_channel); |
Jim Ostrowski | baa4369 | 2020-03-08 16:25:10 -0700 | [diff] [blame] | 153 | |
Jim Ostrowski | 834dddf | 2021-04-10 14:40:19 -0700 | [diff] [blame] | 154 | // Run the display loop |
| 155 | event_loop.AddPhasedLoop( |
| 156 | [&event_loop](int) { |
| 157 | if (!DisplayLoop()) { |
| 158 | LOG(INFO) << "Calling event_loop Exit"; |
| 159 | event_loop.Exit(); |
| 160 | }; |
| 161 | }, |
| 162 | ::std::chrono::milliseconds(100)); |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 163 | |
| 164 | event_loop.Run(); |
Austin Schuh | 5776312 | 2021-11-06 20:49:18 -0700 | [diff] [blame] | 165 | |
| 166 | image_fetcher = aos::Fetcher<CameraImage>(); |
| 167 | match_fetcher = aos::Fetcher<sift::ImageMatchResult>(); |
Jim Ostrowski | 23eb658 | 2020-03-04 23:15:32 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | } // namespace |
| 171 | } // namespace vision |
| 172 | } // namespace frc971 |
| 173 | |
| 174 | // Quick and lightweight grayscale viewer for images |
| 175 | int main(int argc, char **argv) { |
| 176 | aos::InitGoogle(&argc, &argv); |
| 177 | frc971::vision::ViewerMain(); |
| 178 | } |