blob: 202b6a0213179047882c571ff5e3ef374ba1d48c [file] [log] [blame]
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08001#include <map>
2#include <opencv2/calib3d.hpp>
3#include <opencv2/features2d.hpp>
4#include <opencv2/highgui/highgui.hpp>
5#include <opencv2/imgproc.hpp>
6#include <random>
7
8#include "aos/events/shm_event_loop.h"
9#include "aos/init.h"
10#include "aos/time/time.h"
Jim Ostrowski977850f2022-01-22 21:04:22 -080011#include "frc971/vision/vision_generated.h"
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080012#include "y2022/vision/blob_detector.h"
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080013#include "y2022/vision/target_estimator.h"
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080014
15DEFINE_string(capture, "",
16 "If set, capture a single image and save it to this filename.");
17DEFINE_string(channel, "/camera", "Channel name for the image.");
18DEFINE_string(config, "config.json", "Path to the config file to use.");
milind-u61f21e82022-01-23 18:34:11 -080019DEFINE_string(png_dir, "LED_Ring_exp", "Path to a set of images to display.");
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080020DEFINE_bool(show_features, true, "Show the blobs.");
21
22namespace y2022 {
23namespace vision {
24namespace {
25
26aos::Fetcher<frc971::vision::CameraImage> image_fetcher;
27
28bool DisplayLoop() {
29 int64_t image_timestamp = 0;
30 const frc971::vision::CameraImage *image;
31 // Read next image
32 if (!image_fetcher.FetchNext()) {
33 VLOG(2) << "Couldn't fetch next image";
34 return true;
35 }
36
37 image = image_fetcher.get();
38 CHECK(image != nullptr) << "Couldn't read image";
39 image_timestamp = image->monotonic_timestamp_ns();
40 VLOG(2) << "Got image at timestamp: " << image_timestamp;
41
42 // Create color image:
43 cv::Mat image_color_mat(cv::Size(image->cols(), image->rows()), CV_8UC2,
44 (void *)image->data()->data());
45 cv::Mat rgb_image(cv::Size(image->cols(), image->rows()), CV_8UC3);
46 cv::cvtColor(image_color_mat, rgb_image, cv::COLOR_YUV2BGR_YUYV);
47
48 if (!FLAGS_capture.empty()) {
49 cv::imwrite(FLAGS_capture, rgb_image);
50 return false;
51 }
52
53 cv::Mat binarized_image, ret_image;
milind-u61f21e82022-01-23 18:34:11 -080054 std::vector<std::vector<cv::Point>> unfiltered_blobs, filtered_blobs;
55 std::vector<BlobDetector::BlobStats> blob_stats;
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080056 cv::Point centroid;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080057 BlobDetector::ExtractBlobs(rgb_image, binarized_image, ret_image,
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080058 filtered_blobs, unfiltered_blobs, blob_stats,
59 centroid);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080060
61 LOG(INFO) << image->monotonic_timestamp_ns()
62 << ": # blobs: " << filtered_blobs.size();
63
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080064 cv::imshow("image", rgb_image);
65 cv::imshow("blobs", ret_image);
66
67 int keystroke = cv::waitKey(1);
68 if ((keystroke & 0xFF) == static_cast<int>('c')) {
69 // Convert again, to get clean image
70 cv::cvtColor(image_color_mat, rgb_image, cv::COLOR_YUV2BGR_YUYV);
71 std::stringstream name;
72 name << "capture-" << aos::realtime_clock::now() << ".png";
73 cv::imwrite(name.str(), rgb_image);
74 LOG(INFO) << "Saved image file: " << name.str();
75 } else if ((keystroke & 0xFF) == static_cast<int>('q')) {
76 return false;
77 }
78 return true;
79}
80
81void ViewerMain() {
82 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
83 aos::configuration::ReadConfig(FLAGS_config);
84
85 aos::ShmEventLoop event_loop(&config.message());
86
87 image_fetcher =
88 event_loop.MakeFetcher<frc971::vision::CameraImage>(FLAGS_channel);
89
90 // Run the display loop
91 event_loop.AddPhasedLoop(
92 [&event_loop](int) {
93 if (!DisplayLoop()) {
94 LOG(INFO) << "Calling event_loop Exit";
95 event_loop.Exit();
96 };
97 },
98 ::std::chrono::milliseconds(100));
99
100 event_loop.Run();
101
102 image_fetcher = aos::Fetcher<frc971::vision::CameraImage>();
103}
104
105void ViewerLocal() {
106 std::vector<cv::String> file_list;
107 cv::glob(FLAGS_png_dir + "/*.png", file_list, false);
108 for (auto file : file_list) {
109 LOG(INFO) << "Reading file " << file;
110 cv::Mat rgb_image = cv::imread(file.c_str());
milind-u61f21e82022-01-23 18:34:11 -0800111 std::vector<std::vector<cv::Point>> filtered_blobs, unfiltered_blobs;
112 std::vector<BlobDetector::BlobStats> blob_stats;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800113 cv::Mat binarized_image =
114 cv::Mat::zeros(cv::Size(rgb_image.cols, rgb_image.rows), CV_8UC1);
115 cv::Mat ret_image =
116 cv::Mat::zeros(cv::Size(rgb_image.cols, rgb_image.rows), CV_8UC3);
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -0800117 cv::Point centroid;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800118 BlobDetector::ExtractBlobs(rgb_image, binarized_image, ret_image,
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -0800119 filtered_blobs, unfiltered_blobs, blob_stats,
120 centroid);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800121
122 LOG(INFO) << ": # blobs: " << filtered_blobs.size() << " (# removed: "
123 << unfiltered_blobs.size() - filtered_blobs.size() << ")";
124 cv::imshow("image", rgb_image);
125 cv::imshow("blobs", ret_image);
126
milind-u61f21e82022-01-23 18:34:11 -0800127 int keystroke = cv::waitKey(0);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800128 if ((keystroke & 0xFF) == static_cast<int>('q')) {
129 return;
130 }
131 }
132}
133} // namespace
134} // namespace vision
135} // namespace y2022
136
137// Quick and lightweight viewer for images
138int main(int argc, char **argv) {
139 aos::InitGoogle(&argc, &argv);
140 if (FLAGS_png_dir != "")
141 y2022::vision::ViewerLocal();
142 else
143 y2022::vision::ViewerMain();
144}