blob: 8ecb36d6af78765018fce7c4e52f4a7dda3395e5 [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"
13
14DEFINE_string(capture, "",
15 "If set, capture a single image and save it to this filename.");
16DEFINE_string(channel, "/camera", "Channel name for the image.");
17DEFINE_string(config, "config.json", "Path to the config file to use.");
milind-u61f21e82022-01-23 18:34:11 -080018DEFINE_string(png_dir, "LED_Ring_exp", "Path to a set of images to display.");
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080019DEFINE_bool(show_features, true, "Show the blobs.");
20
21namespace y2022 {
22namespace vision {
23namespace {
24
25aos::Fetcher<frc971::vision::CameraImage> image_fetcher;
26
27bool DisplayLoop() {
28 int64_t image_timestamp = 0;
29 const frc971::vision::CameraImage *image;
30 // Read next image
31 if (!image_fetcher.FetchNext()) {
32 VLOG(2) << "Couldn't fetch next image";
33 return true;
34 }
35
36 image = image_fetcher.get();
37 CHECK(image != nullptr) << "Couldn't read image";
38 image_timestamp = image->monotonic_timestamp_ns();
39 VLOG(2) << "Got image at timestamp: " << image_timestamp;
40
41 // Create color image:
42 cv::Mat image_color_mat(cv::Size(image->cols(), image->rows()), CV_8UC2,
43 (void *)image->data()->data());
44 cv::Mat rgb_image(cv::Size(image->cols(), image->rows()), CV_8UC3);
45 cv::cvtColor(image_color_mat, rgb_image, cv::COLOR_YUV2BGR_YUYV);
46
47 if (!FLAGS_capture.empty()) {
48 cv::imwrite(FLAGS_capture, rgb_image);
49 return false;
50 }
51
52 cv::Mat binarized_image, ret_image;
milind-u61f21e82022-01-23 18:34:11 -080053 std::vector<std::vector<cv::Point>> unfiltered_blobs, filtered_blobs;
54 std::vector<BlobDetector::BlobStats> blob_stats;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080055 BlobDetector::ExtractBlobs(rgb_image, binarized_image, ret_image,
56 filtered_blobs, unfiltered_blobs, blob_stats);
57
58 LOG(INFO) << image->monotonic_timestamp_ns()
59 << ": # blobs: " << filtered_blobs.size();
60
61 // Downsize for viewing
62 cv::resize(rgb_image, rgb_image,
63 cv::Size(rgb_image.cols / 2, rgb_image.rows / 2),
64 cv::INTER_LINEAR);
65
66 cv::imshow("image", rgb_image);
67 cv::imshow("blobs", ret_image);
68
69 int keystroke = cv::waitKey(1);
70 if ((keystroke & 0xFF) == static_cast<int>('c')) {
71 // Convert again, to get clean image
72 cv::cvtColor(image_color_mat, rgb_image, cv::COLOR_YUV2BGR_YUYV);
73 std::stringstream name;
74 name << "capture-" << aos::realtime_clock::now() << ".png";
75 cv::imwrite(name.str(), rgb_image);
76 LOG(INFO) << "Saved image file: " << name.str();
77 } else if ((keystroke & 0xFF) == static_cast<int>('q')) {
78 return false;
79 }
80 return true;
81}
82
83void ViewerMain() {
84 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
85 aos::configuration::ReadConfig(FLAGS_config);
86
87 aos::ShmEventLoop event_loop(&config.message());
88
89 image_fetcher =
90 event_loop.MakeFetcher<frc971::vision::CameraImage>(FLAGS_channel);
91
92 // Run the display loop
93 event_loop.AddPhasedLoop(
94 [&event_loop](int) {
95 if (!DisplayLoop()) {
96 LOG(INFO) << "Calling event_loop Exit";
97 event_loop.Exit();
98 };
99 },
100 ::std::chrono::milliseconds(100));
101
102 event_loop.Run();
103
104 image_fetcher = aos::Fetcher<frc971::vision::CameraImage>();
105}
106
107void ViewerLocal() {
108 std::vector<cv::String> file_list;
109 cv::glob(FLAGS_png_dir + "/*.png", file_list, false);
110 for (auto file : file_list) {
111 LOG(INFO) << "Reading file " << file;
112 cv::Mat rgb_image = cv::imread(file.c_str());
milind-u61f21e82022-01-23 18:34:11 -0800113 std::vector<std::vector<cv::Point>> filtered_blobs, unfiltered_blobs;
114 std::vector<BlobDetector::BlobStats> blob_stats;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800115 cv::Mat binarized_image =
116 cv::Mat::zeros(cv::Size(rgb_image.cols, rgb_image.rows), CV_8UC1);
117 cv::Mat ret_image =
118 cv::Mat::zeros(cv::Size(rgb_image.cols, rgb_image.rows), CV_8UC3);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800119 BlobDetector::ExtractBlobs(rgb_image, binarized_image, ret_image,
120 filtered_blobs, unfiltered_blobs, blob_stats);
121
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}