]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/blob - src/complexmat.cuh
Add backward compatibility code for OpenCV 2.4
[hercules2020/kcf.git] / src / complexmat.cuh
1 #ifndef COMPLEXMAT_H
2 #define COMPLEXMAT_H
3
4 #include <opencv2/opencv.hpp>
5
6 #include "dynmem.hpp"
7 #include "cuda_runtime.h"
8 #include "cufft.h"
9
10 #include "cuda_error_check.hpp"
11
12 class ComplexMat {
13   public:
14     uint cols;
15     uint rows;
16     uint n_channels;
17     uint n_scales = 1;
18     bool foreign_data = false;
19
20     ComplexMat() : cols(0), rows(0), n_channels(0) {}
21
22     ComplexMat(uint _rows, uint _cols, uint _n_channels, uint _n_scales = 1)
23         : cols(_cols), rows(_rows), n_channels(_n_channels * _n_scales), n_scales(_n_scales)
24     {
25         CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
26     }
27
28     ComplexMat(cv::Size size, uint _n_channels, uint _n_scales = 1)
29         : cols(size.width), rows(size.height), n_channels(_n_channels * _n_channels), n_scales(_n_scales)
30     {
31         CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
32     }
33
34     ComplexMat(ComplexMat &&other)
35     {
36         cols = other.cols;
37         rows = other.rows;
38         n_channels = other.n_channels;
39         n_scales = other.n_scales;
40         p_data = other.p_data;
41
42         other.p_data = nullptr;
43     }
44
45     ~ComplexMat()
46     {
47         if (p_data != nullptr && !foreign_data) {
48             CudaSafeCall(cudaFree(p_data));
49             p_data = nullptr;
50         }
51     }
52
53     void create(uint _rows, uint _cols, uint _n_channels)
54     {
55         rows = _rows;
56         cols = _cols;
57         n_channels = _n_channels;
58         CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
59     }
60
61     void create(uint _rows, uint _cols, uint _n_channels, uint _n_scales)
62     {
63         rows = _rows;
64         cols = _cols;
65         n_channels = _n_channels;
66         n_scales = _n_scales;
67         CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
68     }
69     // cv::Mat API compatibility
70     cv::Size size() const { return cv::Size(cols, rows); }
71     uint channels() const { return n_channels; }
72
73     void sqr_norm(DynMem &result) const;
74
75     ComplexMat sqr_mag() const;
76
77     ComplexMat conj() const;
78
79     ComplexMat sum_over_channels() const;
80
81     cufftComplex *get_p_data() const;
82
83     // element-wise per channel multiplication, division and addition
84     ComplexMat operator*(const ComplexMat &rhs) const;
85     ComplexMat operator/(const ComplexMat &rhs) const;
86     ComplexMat operator+(const ComplexMat &rhs) const;
87
88     // multiplying or adding constant
89     ComplexMat operator*(const float &rhs) const;
90     ComplexMat operator+(const float &rhs) const;
91
92     // multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
93     ComplexMat mul(const ComplexMat &rhs) const;
94
95     // multiplying element-wise multichannel by one channel mats (rhs mat is with multiple channel)
96     ComplexMat mul2(const ComplexMat &rhs) const;
97     // text output
98     friend std::ostream &operator<<(std::ostream &os, const ComplexMat &mat)
99     {
100         float *data_cpu = reinterpret_cast<float*>(malloc(mat.rows * mat.cols * mat.n_channels * sizeof(cufftComplex)));
101         CudaSafeCall(cudaMemcpy(data_cpu, mat.p_data, mat.rows * mat.cols * mat.n_channels * sizeof(cufftComplex),
102                                 cudaMemcpyDeviceToHost));
103         // for (int i = 0; i < mat.n_channels; ++i){
104         for (int i = 0; i < 1; ++i) {
105             os << "Channel " << i << std::endl;
106             for (uint j = 0; j < mat.rows; ++j) {
107                 for (uint k = 0; k < 2 * mat.cols - 2; k += 2)
108                     os << "(" << data_cpu[j * 2 * mat.cols + k] << "," << data_cpu[j * 2 * mat.cols + (k + 1)] << ")"
109                        << ", ";
110                 os << "(" << data_cpu[j * 2 * mat.cols + 2 * mat.cols - 2] << ","
111                    << data_cpu[j * 2 * mat.cols + 2 * mat.cols - 1] << ")" << std::endl;
112             }
113         }
114         free(data_cpu);
115         return os;
116     }
117
118     void operator=(ComplexMat &rhs);
119     void operator=(ComplexMat &&rhs);
120
121   private:
122     mutable float *p_data = nullptr;
123 };
124
125 #endif // COMPLEXMAT_H