blob: c141e3c1abd3f893a43481f9d60827ca5e32863f [file] [log] [blame]
Jim Ostrowskic560cbe2020-03-07 00:29:30 -08001#include <map>
Jim Ostrowski23eb6582020-03-04 23:15:32 -08002#include <opencv2/calib3d.hpp>
3#include <opencv2/features2d.hpp>
4#include <opencv2/highgui/highgui.hpp>
5#include <opencv2/imgproc.hpp>
Jim Ostrowski56c09322021-10-23 16:17:21 -07006#include <random>
Jim Ostrowski23eb6582020-03-04 23:15:32 -08007
8#include "aos/events/shm_event_loop.h"
9#include "aos/init.h"
Jim Ostrowskibaa43692020-03-08 16:25:10 -070010#include "aos/time/time.h"
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080011#include "y2020/vision/sift/sift_generated.h"
Jim Ostrowski23eb6582020-03-04 23:15:32 -080012#include "y2020/vision/vision_generated.h"
13
14DEFINE_string(config, "config.json", "Path to the config file to use.");
Jim Ostrowski56c09322021-10-23 16:17:21 -070015DEFINE_bool(show_features, true, "Show the SIFT features that matched.");
Austin Schuh69b2f792020-03-15 14:31:55 -070016DEFINE_string(channel, "/camera", "Channel name for the image.");
Jim Ostrowski23eb6582020-03-04 23:15:32 -080017
Austin Schuh57763122021-11-06 20:49:18 -070018DEFINE_string(capture, "",
19 "If set, capture a single image and save it to this filename.");
20
Jim Ostrowski23eb6582020-03-04 23:15:32 -080021namespace frc971 {
22namespace vision {
23namespace {
24
Jim Ostrowski834dddf2021-04-10 14:40:19 -070025aos::Fetcher<CameraImage> image_fetcher;
26aos::Fetcher<sift::ImageMatchResult> match_fetcher;
Jim Ostrowski56c09322021-10-23 16:17:21 -070027cv::Mat palette_;
Jim Ostrowski834dddf2021-04-10 14:40:19 -070028
29bool DisplayLoop() {
Jim Ostrowski56c09322021-10-23 16:17:21 -070030 // Try to get target match data
31 int64_t match_timestamp = 0;
32 const sift::ImageMatchResult *match;
Jim Ostrowski834dddf2021-04-10 14:40:19 -070033 if (match_fetcher.Fetch()) {
Jim Ostrowski56c09322021-10-23 16:17:21 -070034 match = match_fetcher.get();
Jim Ostrowski834dddf2021-04-10 14:40:19 -070035 CHECK(match != nullptr) << "Got null when trying to fetch match result";
36
Jim Ostrowski56c09322021-10-23 16:17:21 -070037 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 Ostrowski834dddf2021-04-10 14:40:19 -070065 }
66 }
67
Jim Ostrowski834dddf2021-04-10 14:40:19 -070068 // 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);
72 cv::cvtColor(image_color_mat, rgb_image, CV_YUV2BGR_YUYV);
73
Austin Schuh57763122021-11-06 20:49:18 -070074 if (!FLAGS_capture.empty()) {
75 cv::imwrite(FLAGS_capture, rgb_image);
76 return false;
77 }
78
Jim Ostrowski56c09322021-10-23 16:17:21 -070079 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 Ostrowski834dddf2021-04-10 14:40:19 -0700116 }
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
122 cv::cvtColor(image_color_mat, rgb_image, CV_YUV2BGR_YUYV);
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
133void ViewerMain() {
Jim Ostrowski56c09322021-10-23 16:17:21 -0700134 // 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 Ostrowskic560cbe2020-03-07 00:29:30 -0800138
Jim Ostrowski23eb6582020-03-04 23:15:32 -0800139 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
140 aos::configuration::ReadConfig(FLAGS_config);
141
142 aos::ShmEventLoop event_loop(&config.message());
143
Jim Ostrowski834dddf2021-04-10 14:40:19 -0700144 image_fetcher = event_loop.MakeFetcher<CameraImage>(FLAGS_channel);
Jim Ostrowski23eb6582020-03-04 23:15:32 -0800145
Jim Ostrowski56c09322021-10-23 16:17:21 -0700146 // 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 Ostrowskibaa43692020-03-08 16:25:10 -0700153
Jim Ostrowski834dddf2021-04-10 14:40:19 -0700154 // 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 Ostrowski23eb6582020-03-04 23:15:32 -0800163
164 event_loop.Run();
Austin Schuh57763122021-11-06 20:49:18 -0700165
166 image_fetcher = aos::Fetcher<CameraImage>();
167 match_fetcher = aos::Fetcher<sift::ImageMatchResult>();
Jim Ostrowski23eb6582020-03-04 23:15:32 -0800168}
169
170} // namespace
171} // namespace vision
172} // namespace frc971
173
174// Quick and lightweight grayscale viewer for images
175int main(int argc, char **argv) {
176 aos::InitGoogle(&argc, &argv);
177 frc971::vision::ViewerMain();
178}