Brian Silverman | 3fec648 | 2020-01-19 17:56:20 -0800 | [diff] [blame] | 1 | #ifndef Y2020_VISION_SIFT_FAST_GAUSSIAN_H_ |
| 2 | #define Y2020_VISION_SIFT_FAST_GAUSSIAN_H_ |
| 3 | |
| 4 | #include <type_traits> |
| 5 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 6 | #include "absl/log/check.h" |
| 7 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 8 | #include <opencv2/core/mat.hpp> |
| 9 | |
| 10 | #include "HalideBuffer.h" |
Brian Silverman | 3fec648 | 2020-01-19 17:56:20 -0800 | [diff] [blame] | 11 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 12 | namespace frc971::vision { |
Brian Silverman | 3fec648 | 2020-01-19 17:56:20 -0800 | [diff] [blame] | 13 | |
| 14 | // Returns a Halide buffer representing the data in mat. |
| 15 | template <typename T> |
| 16 | inline Halide::Runtime::Buffer<T, 2> MatToHalide(const cv::Mat &mat) { |
| 17 | CHECK_EQ(cv::DataType<typename std::remove_const<T>::type>::type, mat.type()); |
| 18 | // Verify that at<T>(row, col) accesses this address: |
| 19 | // data + sizeof(T) * (row * cols + col) |
| 20 | CHECK_EQ(mat.elemSize(), sizeof(T)); |
| 21 | CHECK_EQ(mat.elemSize1(), sizeof(T)); |
| 22 | CHECK_EQ(mat.step1(0), static_cast<size_t>(mat.cols)); |
| 23 | CHECK_EQ(mat.step1(1), 1u); |
| 24 | CHECK_EQ(mat.dims, 2); |
| 25 | CHECK(mat.isContinuous()); |
| 26 | return Halide::Runtime::Buffer<T, 2>(reinterpret_cast<T *>(mat.data), |
| 27 | mat.cols, mat.rows); |
| 28 | } |
| 29 | |
| 30 | // Performs a gaussian blur with the specified sigma, truncated to a reasonable |
| 31 | // width. Attempts to use faster implementations, but will fall back to |
| 32 | // cv::GaussianBlur otherwise. Only handles a limited set of Mat formats. |
| 33 | // |
| 34 | // source and destination may not overlap. |
| 35 | // |
| 36 | // Always uses BORDER_REPLICATE mode. |
| 37 | void FastGaussian(const cv::Mat &source, cv::Mat *destination, double sigma); |
| 38 | void FastSubtract(const cv::Mat &a, const cv::Mat &b, cv::Mat *destination); |
| 39 | void FastGaussianAndSubtract(const cv::Mat &source, cv::Mat *blurred, |
| 40 | cv::Mat *difference, double sigma); |
| 41 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 42 | } // namespace frc971::vision |
Brian Silverman | 3fec648 | 2020-01-19 17:56:20 -0800 | [diff] [blame] | 43 | |
| 44 | #endif // Y2020_VISION_SIFT_FAST_GAUSSIAN_H_ |