Brian Silverman | 3fec648 | 2020-01-19 17:56:20 -0800 | [diff] [blame] | 1 | #include <memory> |
| 2 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 3 | #include "absl/flags/flag.h" |
| 4 | #include "absl/log/check.h" |
| 5 | #include "absl/log/log.h" |
Brian Silverman | 3fec648 | 2020-01-19 17:56:20 -0800 | [diff] [blame] | 6 | #include <opencv2/highgui/highgui.hpp> |
| 7 | #include <opencv2/imgcodecs.hpp> |
| 8 | #include <opencv2/imgproc.hpp> |
| 9 | |
| 10 | #include "aos/init.h" |
| 11 | #include "aos/time/time.h" |
| 12 | #include "y2020/vision/sift/fast_gaussian.h" |
Brian Silverman | 3fec648 | 2020-01-19 17:56:20 -0800 | [diff] [blame] | 13 | #include "y2020/vision/sift/sift971.h" |
| 14 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 15 | ABSL_FLAG(std::string, image, "", "Image to test with"); |
Brian Silverman | 3fec648 | 2020-01-19 17:56:20 -0800 | [diff] [blame] | 16 | |
| 17 | int main(int argc, char **argv) { |
| 18 | aos::InitGoogle(&argc, &argv); |
| 19 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 20 | cv::setNumThreads(4); |
Brian Silverman | 3fec648 | 2020-01-19 17:56:20 -0800 | [diff] [blame] | 21 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 22 | const cv::Mat raw_image = cv::imread(absl::GetFlag(FLAGS_image)); |
| 23 | CHECK(!raw_image.empty()) |
| 24 | << ": Failed to read: " << absl::GetFlag(FLAGS_image); |
Brian Silverman | 3fec648 | 2020-01-19 17:56:20 -0800 | [diff] [blame] | 25 | CHECK_EQ(CV_8UC3, raw_image.type()); |
| 26 | #if 0 |
| 27 | cv::Mat color_image; |
| 28 | raw_image.convertTo(color_image, CV_32F, 1.0/255.0); |
| 29 | cv::Mat image; |
| 30 | cv::cvtColor(color_image, image, cv::COLOR_BGR2GRAY); |
| 31 | #else |
| 32 | cv::Mat gray_image; |
| 33 | cv::cvtColor(raw_image, gray_image, cv::COLOR_BGR2GRAY); |
| 34 | cv::Mat float_image; |
| 35 | #if 0 |
| 36 | gray_image.convertTo(float_image, CV_32F, 0.00390625); |
| 37 | #else |
| 38 | float_image = gray_image; |
| 39 | #endif |
| 40 | cv::Mat image; |
| 41 | cv::resize(float_image, image, cv::Size(1280, 720), 0, 0, cv::INTER_AREA); |
| 42 | #endif |
| 43 | #if 0 |
| 44 | #if 0 |
| 45 | cv::namedWindow("source", cv::WINDOW_AUTOSIZE); |
| 46 | cv::imshow("source", raw_image); |
| 47 | cv::namedWindow("converted", cv::WINDOW_AUTOSIZE); |
| 48 | cv::imshow("converted", image); |
| 49 | #endif |
| 50 | |
| 51 | cv::Mat slow_blurred, fast_blurred; |
| 52 | const double sigma = 3.0900155872895909; |
| 53 | cv::GaussianBlur(image, slow_blurred, cv::Size(9, 9), sigma, sigma); |
| 54 | frc971::vision::FastGaussian(image, &fast_blurred, sigma); |
| 55 | cv::namedWindow("slow", cv::WINDOW_AUTOSIZE); |
| 56 | cv::imshow("slow", slow_blurred); |
| 57 | cv::namedWindow("fast", cv::WINDOW_AUTOSIZE); |
| 58 | cv::imshow("fast", fast_blurred); |
| 59 | cv::waitKey(0); |
| 60 | return 0; |
| 61 | #endif |
| 62 | |
| 63 | LOG(INFO); |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 64 | std::unique_ptr<frc971::vision::SIFT971_Impl> sift( |
| 65 | new frc971::vision::SIFT971_Impl()); |
Brian Silverman | 3fec648 | 2020-01-19 17:56:20 -0800 | [diff] [blame] | 66 | std::vector<cv::KeyPoint> keypoints; |
| 67 | cv::Mat descriptors; |
| 68 | LOG(INFO) << "detectAndCompute on " << image.rows << "x" << image.cols; |
| 69 | sift->detectAndCompute(image, cv::noArray(), keypoints, descriptors); |
| 70 | LOG(INFO); |
| 71 | |
| 72 | #if 0 |
| 73 | return 0; |
| 74 | #endif |
| 75 | |
| 76 | static constexpr int kIterations = 40; |
| 77 | const auto start = aos::monotonic_clock::now(); |
| 78 | for (int i = 0; i < kIterations; ++i) { |
| 79 | keypoints.clear(); |
| 80 | descriptors.release(); |
| 81 | sift->detectAndCompute(image, cv::noArray(), keypoints, descriptors); |
| 82 | } |
| 83 | const auto end = aos::monotonic_clock::now(); |
| 84 | LOG(INFO) |
| 85 | << "Took: " |
| 86 | << (std::chrono::duration<double>(end - start) / kIterations).count(); |
| 87 | // Should be ~352 for FRC-Image4-cleaned.png downscaled to 640x360. |
| 88 | // 376 in DoG_TYPE_SHORT mode. |
| 89 | // 344 now with 1280x720 non-upscaled. |
| 90 | LOG(INFO) << "found " << keypoints.size() << " and " << descriptors.size(); |
| 91 | } |