blob: 4e246e9ea6f0bcb171b8dcb32f517db589e02972 [file] [log] [blame]
Brian Silverman3fec6482020-01-19 17:56:20 -08001#ifndef Y2020_VISION_SIFT_FAST_GAUSSIAN_H_
2#define Y2020_VISION_SIFT_FAST_GAUSSIAN_H_
3
4#include <type_traits>
5
Austin Schuh99f7c6a2024-06-25 22:07:44 -07006#include "absl/log/check.h"
7#include "absl/log/log.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07008#include <opencv2/core/mat.hpp>
9
10#include "HalideBuffer.h"
Brian Silverman3fec6482020-01-19 17:56:20 -080011
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080012namespace frc971::vision {
Brian Silverman3fec6482020-01-19 17:56:20 -080013
14// Returns a Halide buffer representing the data in mat.
15template <typename T>
16inline 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.
37void FastGaussian(const cv::Mat &source, cv::Mat *destination, double sigma);
38void FastSubtract(const cv::Mat &a, const cv::Mat &b, cv::Mat *destination);
39void FastGaussianAndSubtract(const cv::Mat &source, cv::Mat *blurred,
40 cv::Mat *difference, double sigma);
41
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080042} // namespace frc971::vision
Brian Silverman3fec6482020-01-19 17:56:20 -080043
44#endif // Y2020_VISION_SIFT_FAST_GAUSSIAN_H_