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