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