James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 1 | #include <opencv2/calib3d.hpp> |
| 2 | #include <opencv2/features2d.hpp> |
| 3 | #include <opencv2/highgui/highgui.hpp> |
| 4 | #include <opencv2/imgproc.hpp> |
| 5 | |
Austin Schuh | b06f03b | 2021-02-17 22:00:37 -0800 | [diff] [blame] | 6 | #include "aos/events/logging/log_reader.h" |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 7 | #include "aos/events/simulated_event_loop.h" |
| 8 | #include "aos/init.h" |
Jim Ostrowski | 977850f | 2022-01-22 21:04:22 -0800 | [diff] [blame] | 9 | #include "frc971/vision/vision_generated.h" |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 10 | |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 11 | DEFINE_string(node, "pi1", "Node name to replay."); |
James Kuszmaul | 3a49a53 | 2020-03-06 21:06:38 -0800 | [diff] [blame] | 12 | DEFINE_string(image_save_prefix, "/tmp/img", |
| 13 | "Prefix to use for saving images from the logfile."); |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 14 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 15 | namespace frc971::vision { |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 16 | namespace { |
| 17 | |
James Kuszmaul | 4567127 | 2021-08-01 20:42:53 -0700 | [diff] [blame] | 18 | void ViewerMain(int argc, char *argv[]) { |
James Kuszmaul | 4567127 | 2021-08-01 20:42:53 -0700 | [diff] [blame] | 19 | // open logfiles |
Austin Schuh | c160973 | 2023-06-26 11:51:28 -0700 | [diff] [blame] | 20 | aos::logger::LogReader reader( |
| 21 | aos::logger::SortParts(aos::logger::FindLogs(argc, argv))); |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 22 | reader.Register(); |
| 23 | const aos::Node *node = nullptr; |
| 24 | if (aos::configuration::MultiNode(reader.configuration())) { |
| 25 | node = aos::configuration::GetNode(reader.configuration(), FLAGS_node); |
| 26 | } |
| 27 | std::unique_ptr<aos::EventLoop> event_loop = |
| 28 | reader.event_loop_factory()->MakeEventLoop("player", node); |
| 29 | |
James Kuszmaul | 3a49a53 | 2020-03-06 21:06:38 -0800 | [diff] [blame] | 30 | int image_count = 0; |
| 31 | event_loop->MakeWatcher("/camera", [&image_count](const CameraImage &image) { |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 32 | cv::Mat image_mat(image.rows(), image.cols(), CV_8U); |
| 33 | CHECK(image_mat.isContinuous()); |
| 34 | const int number_pixels = image.rows() * image.cols(); |
| 35 | for (int i = 0; i < number_pixels; ++i) { |
| 36 | reinterpret_cast<uint8_t *>(image_mat.data)[i] = |
| 37 | image.data()->data()[i * 2]; |
| 38 | } |
| 39 | |
| 40 | cv::imshow("Display", image_mat); |
James Kuszmaul | 3a49a53 | 2020-03-06 21:06:38 -0800 | [diff] [blame] | 41 | if (!FLAGS_image_save_prefix.empty()) { |
| 42 | cv::imwrite("/tmp/img" + std::to_string(image_count++) + ".png", |
| 43 | image_mat); |
| 44 | } |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 45 | cv::waitKey(1); |
| 46 | }); |
| 47 | |
| 48 | reader.event_loop_factory()->Run(); |
| 49 | } |
| 50 | |
| 51 | } // namespace |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 52 | } // namespace frc971::vision |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 53 | |
| 54 | // Quick and lightweight grayscale viewer for images |
| 55 | int main(int argc, char **argv) { |
| 56 | aos::InitGoogle(&argc, &argv); |
James Kuszmaul | 4567127 | 2021-08-01 20:42:53 -0700 | [diff] [blame] | 57 | frc971::vision::ViewerMain(argc, argv); |
James Kuszmaul | 8ae931a | 2020-03-06 19:45:02 -0800 | [diff] [blame] | 58 | } |