blob: 2da16e1ead951ddea5f7ca350060bae367a108e7 [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.");
Jim Ostrowski5caf81c2022-01-30 21:02:19 -080019DEFINE_string(png_dir, "", "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
Jim Ostrowski5caf81c2022-01-30 21:02:19 -080032 if (!image_fetcher.Fetch()) {
33 LOG(INFO) << "Couldn't fetch image";
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080034 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
Jim Ostrowski5caf81c2022-01-30 21:02:19 -080053 cv::Mat binarized_image;
54 cv::Mat ret_image(cv::Size(image->cols(), image->rows()), CV_8UC3);
milind-u61f21e82022-01-23 18:34:11 -080055 std::vector<std::vector<cv::Point>> unfiltered_blobs, filtered_blobs;
56 std::vector<BlobDetector::BlobStats> blob_stats;
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080057 cv::Point centroid;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080058 BlobDetector::ExtractBlobs(rgb_image, binarized_image, ret_image,
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080059 filtered_blobs, unfiltered_blobs, blob_stats,
60 centroid);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080061
62 LOG(INFO) << image->monotonic_timestamp_ns()
63 << ": # blobs: " << filtered_blobs.size();
64
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080065 cv::imshow("image", rgb_image);
66 cv::imshow("blobs", ret_image);
67
68 int keystroke = cv::waitKey(1);
69 if ((keystroke & 0xFF) == static_cast<int>('c')) {
70 // Convert again, to get clean image
71 cv::cvtColor(image_color_mat, rgb_image, cv::COLOR_YUV2BGR_YUYV);
72 std::stringstream name;
73 name << "capture-" << aos::realtime_clock::now() << ".png";
74 cv::imwrite(name.str(), rgb_image);
75 LOG(INFO) << "Saved image file: " << name.str();
76 } else if ((keystroke & 0xFF) == static_cast<int>('q')) {
77 return false;
78 }
79 return true;
80}
81
82void ViewerMain() {
83 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
84 aos::configuration::ReadConfig(FLAGS_config);
85
86 aos::ShmEventLoop event_loop(&config.message());
87
88 image_fetcher =
89 event_loop.MakeFetcher<frc971::vision::CameraImage>(FLAGS_channel);
90
91 // Run the display loop
92 event_loop.AddPhasedLoop(
93 [&event_loop](int) {
94 if (!DisplayLoop()) {
95 LOG(INFO) << "Calling event_loop Exit";
96 event_loop.Exit();
97 };
98 },
99 ::std::chrono::milliseconds(100));
100
101 event_loop.Run();
102
103 image_fetcher = aos::Fetcher<frc971::vision::CameraImage>();
104}
105
106void ViewerLocal() {
107 std::vector<cv::String> file_list;
108 cv::glob(FLAGS_png_dir + "/*.png", file_list, false);
109 for (auto file : file_list) {
110 LOG(INFO) << "Reading file " << file;
111 cv::Mat rgb_image = cv::imread(file.c_str());
milind-u61f21e82022-01-23 18:34:11 -0800112 std::vector<std::vector<cv::Point>> filtered_blobs, unfiltered_blobs;
113 std::vector<BlobDetector::BlobStats> blob_stats;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800114 cv::Mat binarized_image =
115 cv::Mat::zeros(cv::Size(rgb_image.cols, rgb_image.rows), CV_8UC1);
116 cv::Mat ret_image =
117 cv::Mat::zeros(cv::Size(rgb_image.cols, rgb_image.rows), CV_8UC3);
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -0800118 cv::Point centroid;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800119 BlobDetector::ExtractBlobs(rgb_image, binarized_image, ret_image,
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -0800120 filtered_blobs, unfiltered_blobs, blob_stats,
121 centroid);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800122
123 LOG(INFO) << ": # blobs: " << filtered_blobs.size() << " (# removed: "
124 << unfiltered_blobs.size() - filtered_blobs.size() << ")";
125 cv::imshow("image", rgb_image);
Milind Upadhyay2b4404c2022-02-04 21:20:57 -0800126 cv::imshow("mask", binarized_image);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800127 cv::imshow("blobs", ret_image);
128
milind-u61f21e82022-01-23 18:34:11 -0800129 int keystroke = cv::waitKey(0);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800130 if ((keystroke & 0xFF) == static_cast<int>('q')) {
131 return;
132 }
133 }
134}
135} // namespace
136} // namespace vision
137} // namespace y2022
138
139// Quick and lightweight viewer for images
140int main(int argc, char **argv) {
141 aos::InitGoogle(&argc, &argv);
142 if (FLAGS_png_dir != "")
143 y2022::vision::ViewerLocal();
144 else
145 y2022::vision::ViewerMain();
146}