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