Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 1 | #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 Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 11 | #include "frc971/vision/vision_generated.h" |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 12 | #include "y2022/vision/blob_detector.h" |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 13 | #include "y2022/vision/target_estimator.h" |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 14 | |
| 15 | DEFINE_string(capture, "", |
| 16 | "If set, capture a single image and save it to this filename."); |
| 17 | DEFINE_string(channel, "/camera", "Channel name for the image."); |
| 18 | DEFINE_string(config, "config.json", "Path to the config file to use."); |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 19 | DEFINE_string(png_dir, "LED_Ring_exp", "Path to a set of images to display."); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 20 | DEFINE_bool(show_features, true, "Show the blobs."); |
| 21 | |
| 22 | namespace y2022 { |
| 23 | namespace vision { |
| 24 | namespace { |
| 25 | |
| 26 | aos::Fetcher<frc971::vision::CameraImage> image_fetcher; |
| 27 | |
| 28 | bool 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-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 54 | std::vector<std::vector<cv::Point>> unfiltered_blobs, filtered_blobs; |
| 55 | std::vector<BlobDetector::BlobStats> blob_stats; |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 56 | cv::Point centroid; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 57 | BlobDetector::ExtractBlobs(rgb_image, binarized_image, ret_image, |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 58 | filtered_blobs, unfiltered_blobs, blob_stats, |
| 59 | centroid); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 60 | |
| 61 | LOG(INFO) << image->monotonic_timestamp_ns() |
| 62 | << ": # blobs: " << filtered_blobs.size(); |
| 63 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 64 | 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 | |
| 81 | void 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 | |
| 105 | void 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-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 111 | std::vector<std::vector<cv::Point>> filtered_blobs, unfiltered_blobs; |
| 112 | std::vector<BlobDetector::BlobStats> blob_stats; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 113 | 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 Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 117 | cv::Point centroid; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 118 | BlobDetector::ExtractBlobs(rgb_image, binarized_image, ret_image, |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 119 | filtered_blobs, unfiltered_blobs, blob_stats, |
| 120 | centroid); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 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-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 127 | int keystroke = cv::waitKey(0); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 128 | 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 |
| 138 | int 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 | } |