blob: 580083f13f107aa3959c048b1c21bd3b950d58f5 [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
6#include <opencv2/core/mat.hpp>
7#include "HalideBuffer.h"
8#include "glog/logging.h"
9
10namespace frc971 {
11namespace vision {
12
13// Returns a Halide buffer representing the data in mat.
14template <typename T>
15inline Halide::Runtime::Buffer<T, 2> MatToHalide(const cv::Mat &mat) {
16 CHECK_EQ(cv::DataType<typename std::remove_const<T>::type>::type, mat.type());
17 // Verify that at<T>(row, col) accesses this address:
18 // data + sizeof(T) * (row * cols + col)
19 CHECK_EQ(mat.elemSize(), sizeof(T));
20 CHECK_EQ(mat.elemSize1(), sizeof(T));
21 CHECK_EQ(mat.step1(0), static_cast<size_t>(mat.cols));
22 CHECK_EQ(mat.step1(1), 1u);
23 CHECK_EQ(mat.dims, 2);
24 CHECK(mat.isContinuous());
25 return Halide::Runtime::Buffer<T, 2>(reinterpret_cast<T *>(mat.data),
26 mat.cols, mat.rows);
27}
28
29// Performs a gaussian blur with the specified sigma, truncated to a reasonable
30// width. Attempts to use faster implementations, but will fall back to
31// cv::GaussianBlur otherwise. Only handles a limited set of Mat formats.
32//
33// source and destination may not overlap.
34//
35// Always uses BORDER_REPLICATE mode.
36void FastGaussian(const cv::Mat &source, cv::Mat *destination, double sigma);
37void FastSubtract(const cv::Mat &a, const cv::Mat &b, cv::Mat *destination);
38void FastGaussianAndSubtract(const cv::Mat &source, cv::Mat *blurred,
39 cv::Mat *difference, double sigma);
40
41} // namespace vision
42} // namespace vision
43
44#endif // Y2020_VISION_SIFT_FAST_GAUSSIAN_H_