blob: d9234075cab614ac68086db83a31d386024b4bee [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 Ostrowski977850f2022-01-22 21:04:22 -080011#include "frc971/vision/v4l2_reader.h"
12#include "frc971/vision/vision_generated.h"
Jim Ostrowskic560cbe2020-03-07 00:29:30 -080013#include "y2020/vision/sift/sift_generated.h"
Jim Ostrowski23eb6582020-03-04 23:15:32 -080014
Austin Schuhc5fa6d92022-02-25 14:36:28 -080015DEFINE_string(config, "aos_config.json", "Path to the config file to use.");
Jim Ostrowski56c09322021-10-23 16:17:21 -070016DEFINE_bool(show_features, true, "Show the SIFT features that matched.");
Austin Schuh69b2f792020-03-15 14:31:55 -070017DEFINE_string(channel, "/camera", "Channel name for the image.");
Jim Ostrowski23eb6582020-03-04 23:15:32 -080018
Austin Schuh57763122021-11-06 20:49:18 -070019DEFINE_string(capture, "",
20 "If set, capture a single image and save it to this filename.");
21
Jim Ostrowski23eb6582020-03-04 23:15:32 -080022namespace frc971 {
23namespace vision {
24namespace {
25
Jim Ostrowski834dddf2021-04-10 14:40:19 -070026aos::Fetcher<CameraImage> image_fetcher;
27aos::Fetcher<sift::ImageMatchResult> match_fetcher;
Jim Ostrowski56c09322021-10-23 16:17:21 -070028cv::Mat palette_;
Jim Ostrowski834dddf2021-04-10 14:40:19 -070029
30bool DisplayLoop() {
Jim Ostrowski56c09322021-10-23 16:17:21 -070031 // Try to get target match data
32 int64_t match_timestamp = 0;
33 const sift::ImageMatchResult *match;
Jim Ostrowski834dddf2021-04-10 14:40:19 -070034 if (match_fetcher.Fetch()) {
Jim Ostrowski56c09322021-10-23 16:17:21 -070035 match = match_fetcher.get();
Jim Ostrowski834dddf2021-04-10 14:40:19 -070036 CHECK(match != nullptr) << "Got null when trying to fetch match result";
37
Jim Ostrowski56c09322021-10-23 16:17:21 -070038 match_timestamp = match->image_monotonic_timestamp_ns();
39 if (match->camera_poses() != nullptr && match->camera_poses()->size() > 0) {
40 VLOG(2) << "Got matches for timestamp " << match_timestamp << "\n";
41 }
42 } else {
43 VLOG(2) << "Didn't get match this cycle";
44 }
45
46 int64_t image_timestamp = 0;
47 bool matching_image_found = false;
48 const CameraImage *image;
49 while (!matching_image_found) {
50 // Read next image
51 if (!image_fetcher.FetchNext()) {
52 VLOG(2) << "Couldn't fetch next image";
53 return true;
54 }
55
56 image = image_fetcher.get();
57 CHECK(image != nullptr) << "Couldn't read image";
58 image_timestamp = image->monotonic_timestamp_ns();
59 VLOG(2) << "Got image at timestamp: " << image_timestamp;
60
61 if (match_timestamp != 0 && image_timestamp == match_timestamp) {
62 matching_image_found = true;
63 } else if (image_timestamp > match_timestamp) {
64 LOG(INFO) << "Image timestamp went past match_timestamp";
65 return true;
Jim Ostrowski834dddf2021-04-10 14:40:19 -070066 }
67 }
68
Jim Ostrowski834dddf2021-04-10 14:40:19 -070069 // Create color image:
70 cv::Mat image_color_mat(cv::Size(image->cols(), image->rows()), CV_8UC2,
71 (void *)image->data()->data());
72 cv::Mat rgb_image(cv::Size(image->cols(), image->rows()), CV_8UC3);
Brian Silverman4c7235a2021-11-17 19:04:37 -080073 cv::cvtColor(image_color_mat, rgb_image, cv::COLOR_YUV2BGR_YUYV);
Jim Ostrowski834dddf2021-04-10 14:40:19 -070074
Austin Schuh57763122021-11-06 20:49:18 -070075 if (!FLAGS_capture.empty()) {
76 cv::imwrite(FLAGS_capture, rgb_image);
77 return false;
78 }
79
Jim Ostrowski56c09322021-10-23 16:17:21 -070080 if (matching_image_found) {
81 // Draw whatever matches we have
82 if (match->camera_poses() != nullptr && match->camera_poses()->size() > 0) {
83 // Draw target point in image
84 float x = match->camera_poses()->Get(0)->query_target_point_x();
85 float y = match->camera_poses()->Get(0)->query_target_point_y();
86 float radius = match->camera_poses()->Get(0)->query_target_point_radius();
87 cv::circle(rgb_image, cv::Point2f(x, y), radius, cv::Scalar(0, 255, 0),
88 5);
89 }
90
91 if (FLAGS_show_features && match->image_matches() != nullptr &&
92 match->image_matches()->size() > 0) {
93 // Iterate through matches and draw matched keypoints
94 for (uint model_match_ind = 0;
95 model_match_ind < match->image_matches()->size();
96 model_match_ind++) {
97 auto match_list =
98 match->image_matches()->Get(model_match_ind)->matches();
99 if (match_list != nullptr && match_list->size() > 0) {
100 int train_image_ind =
101 match->image_matches()->Get(model_match_ind)->train_image();
102 VLOG(2) << "Got " << match_list->size() << " matches to model "
103 << train_image_ind;
104
105 // Picking color from palette and drawing
106 auto color = palette_.at<cv::Vec3b>(train_image_ind % palette_.cols);
107 LOG(INFO) << "Using color " << color;
108 for (uint i = 0; i < match_list->size(); i++) {
109 uint query_feature_ind = match_list->Get(i)->query_feature();
110 float kp_x = match->features()->Get(query_feature_ind)->x();
111 float kp_y = match->features()->Get(query_feature_ind)->y();
112 cv::circle(rgb_image, cv::Point2f(kp_x, kp_y), 5, color, 2);
113 }
114 }
115 }
116 }
Jim Ostrowski834dddf2021-04-10 14:40:19 -0700117 }
118
119 cv::imshow("Display", rgb_image);
120 int keystroke = cv::waitKey(1);
121 if ((keystroke & 0xFF) == static_cast<int>('c')) {
122 // Convert again, to get clean image
Brian Silverman4c7235a2021-11-17 19:04:37 -0800123 cv::cvtColor(image_color_mat, rgb_image, cv::COLOR_YUV2BGR_YUYV);
Jim Ostrowski834dddf2021-04-10 14:40:19 -0700124 std::stringstream name;
125 name << "capture-" << aos::realtime_clock::now() << ".png";
126 cv::imwrite(name.str(), rgb_image);
127 LOG(INFO) << "Saved image file: " << name.str();
128 } else if ((keystroke & 0xFF) == static_cast<int>('q')) {
129 return false;
130 }
131 return true;
132}
133
134void ViewerMain() {
Jim Ostrowski56c09322021-10-23 16:17:21 -0700135 // Create random color palette for distinguishing multiple models
136 uchar colors[5][3] = {
137 {0, 0, 255}, {0, 165, 255}, {0, 255, 255}, {255, 0, 0}, {128, 0, 128}};
138 palette_ = cv::Mat(3, 5, CV_8U, &colors);
Jim Ostrowskic560cbe2020-03-07 00:29:30 -0800139
Jim Ostrowski23eb6582020-03-04 23:15:32 -0800140 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
141 aos::configuration::ReadConfig(FLAGS_config);
142
143 aos::ShmEventLoop event_loop(&config.message());
144
Jim Ostrowski834dddf2021-04-10 14:40:19 -0700145 image_fetcher = event_loop.MakeFetcher<CameraImage>(FLAGS_channel);
Jim Ostrowski23eb6582020-03-04 23:15:32 -0800146
Jim Ostrowski56c09322021-10-23 16:17:21 -0700147 // If we want to show the features, we have to use the detailed channel
148 std::string result_channel = FLAGS_channel;
149 if (FLAGS_show_features) {
150 result_channel += "/detailed";
151 }
152 match_fetcher =
153 event_loop.MakeFetcher<sift::ImageMatchResult>(result_channel);
Jim Ostrowskibaa43692020-03-08 16:25:10 -0700154
Jim Ostrowski834dddf2021-04-10 14:40:19 -0700155 // Run the display loop
156 event_loop.AddPhasedLoop(
157 [&event_loop](int) {
158 if (!DisplayLoop()) {
159 LOG(INFO) << "Calling event_loop Exit";
160 event_loop.Exit();
161 };
162 },
163 ::std::chrono::milliseconds(100));
Jim Ostrowski23eb6582020-03-04 23:15:32 -0800164
165 event_loop.Run();
Austin Schuh57763122021-11-06 20:49:18 -0700166
167 image_fetcher = aos::Fetcher<CameraImage>();
168 match_fetcher = aos::Fetcher<sift::ImageMatchResult>();
Jim Ostrowski23eb6582020-03-04 23:15:32 -0800169}
170
171} // namespace
172} // namespace vision
173} // namespace frc971
174
175// Quick and lightweight grayscale viewer for images
176int main(int argc, char **argv) {
177 aos::InitGoogle(&argc, &argv);
178 frc971::vision::ViewerMain();
179}