5 #include <opencv2/opencv.hpp>
9 #if defined(CUFFT) || defined(CUFFTW)
10 #include "cuda_runtime.h"
12 #include "cuda_error_check.hpp"
16 template <typename T> class DynMem_ {
24 const size_t num_elem;
26 DynMem_(size_t num_elem) : num_elem(num_elem)
29 CudaSafeCall(cudaHostAlloc(reinterpret_cast<void **>(&ptr_h), num_elem * sizeof(T), cudaHostAllocMapped));
30 CudaSafeCall(cudaHostGetDevicePointer(reinterpret_cast<void **>(&ptr_d), reinterpret_cast<void *>(ptr_h), 0));
32 ptr_h = new T[num_elem];
35 DynMem_(DynMem_ &&other) : num_elem(other.num_elem)
38 other.ptr_h = nullptr;
41 other.ptr_d = nullptr;
47 CudaSafeCall(cudaFreeHost(ptr_h));
52 T *hostMem() { return ptr_h; }
54 T *deviceMem() { return ptr_d; }
56 void operator=(DynMem_ &rhs) {
57 assert(num_elem == rhs.num_elem);
58 memcpy(ptr_h, rhs.ptr_h, num_elem * sizeof(T));
60 void operator=(DynMem_ &&rhs)
62 assert(num_elem == rhs.num_elem);
70 T operator[](uint i) const { return ptr_h[i]; }
73 typedef DynMem_<float> DynMem;
76 class MatDynMem : public DynMem, public cv::Mat {
78 MatDynMem(cv::Size size, int type)
79 : DynMem(size.area() * CV_MAT_CN(type)), cv::Mat(size, type, hostMem())
81 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
83 MatDynMem(int height, int width, int type)
84 : DynMem(width * height * CV_MAT_CN(type)), cv::Mat(height, width, type, hostMem())
86 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
88 MatDynMem(int ndims, const int *sizes, int type)
89 : DynMem(volume(ndims, sizes) * CV_MAT_CN(type)), cv::Mat(ndims, sizes, type, hostMem())
91 assert((type & CV_MAT_DEPTH_MASK) == CV_32F);
93 MatDynMem(std::vector<int> size, int type)
94 : DynMem(std::accumulate(size.begin(), size.end(), 1, std::multiplies<int>()))
95 , cv::Mat(size.size(), size.data(), type, hostMem()) {}
96 MatDynMem(MatDynMem &&other) = default;
97 MatDynMem(const cv::Mat &other)
98 : DynMem(other.total()) , cv::Mat(other) {}
100 void operator=(const cv::MatExpr &expr) {
101 static_cast<cv::Mat>(*this) = expr;
105 static int volume(int ndims, const int *sizes)
108 for (int i = 0; i < ndims; i++)
113 using cv::Mat::create;
116 class Mat3d : public MatDynMem
119 Mat3d(uint dim0, cv::Size size) : MatDynMem({{int(dim0), size.height, size.width}}, CV_32F) {}
121 cv::Mat plane(uint idx) {
123 assert(int(idx) < size[0]);
124 return cv::Mat(size[1], size[2], cv::Mat::type(), ptr(idx));
126 const cv::Mat plane(uint idx) const {
128 assert(int(idx) < size[0]);
129 return cv::Mat(size[1], size[2], cv::Mat::type(), const_cast<uchar*>(ptr(idx)));
134 class MatFeats : public Mat3d
137 MatFeats(uint num_features, cv::Size size) : Mat3d(num_features, size) {}
139 class MatScales : public Mat3d
142 MatScales(uint num_scales, cv::Size size) : Mat3d(num_scales, size) {}
145 class MatScaleFeats : public MatDynMem
148 MatScaleFeats(uint num_scales, uint num_features, cv::Size size)
149 : MatDynMem({{int(num_scales), int(num_features), size.height, size.width}}, CV_32F) {}
151 cv::Mat plane(uint scale, uint feature) {
153 assert(int(scale) < size[0]);
154 assert(int(feature) < size[1]);
155 return cv::Mat(size[2], size[3], cv::Mat::type(), ptr(scale, feature));
157 cv::Mat scale(uint scale) {
159 assert(int(scale) < size[0]);
160 return cv::Mat(3, std::vector<int>({size[1], size[2], size[3]}).data(), cv::Mat::type(), ptr(scale));