4 #include <opencv2/opencv.hpp>
6 #include "cuda_runtime.h"
9 #include "cuda/cuda_error_check.cuh"
18 bool foreign_data = false;
19 cudaStream_t stream = nullptr;
21 ComplexMat() : cols(0), rows(0), n_channels(0) {}
22 ComplexMat(int _rows, int _cols, int _n_channels, cudaStream_t _stream) : cols(_cols), rows(_rows), n_channels(_n_channels), stream(_stream)
24 CudaSafeCall(cudaMalloc(&p_data, n_channels*cols*rows*sizeof(cufftComplex)));
27 ComplexMat(int _rows, int _cols, int _n_channels, int _n_scales, cudaStream_t _stream) : cols(_cols), rows(_rows), n_channels(_n_channels), n_scales(_n_scales),
30 CudaSafeCall(cudaMalloc(&p_data, n_channels*cols*rows*sizeof(cufftComplex)));
33 ComplexMat(ComplexMat &&other)
37 n_channels = other.n_channels;
38 n_scales = other.n_scales;
39 p_data = other.p_data;
40 stream = other.stream;
42 other.p_data = nullptr;
47 if(p_data != nullptr && !foreign_data){
48 CudaSafeCall(cudaFree(p_data));
53 void create(int _rows, int _cols, int _n_channels, cudaStream_t _stream = nullptr)
57 n_channels = _n_channels;
59 CudaSafeCall(cudaMalloc(&p_data, n_channels*cols*rows*sizeof(cufftComplex)));
62 void create(int _rows, int _cols, int _n_channels, int _n_scales, cudaStream_t _stream = nullptr)
66 n_channels = _n_channels;
69 CudaSafeCall(cudaMalloc(&p_data, n_channels*cols*rows*sizeof(cufftComplex)));
71 // cv::Mat API compatibility
72 cv::Size size() { return cv::Size(cols, rows); }
73 int channels() { return n_channels; }
74 int channels() const { return n_channels; }
76 void set_stream(cudaStream_t _stream)
82 void sqr_norm(float *result) const;
84 ComplexMat sqr_mag() const;
86 ComplexMat conj() const;
88 ComplexMat sum_over_channels() const;
90 cufftComplex* get_p_data() const;
92 //element-wise per channel multiplication, division and addition
93 ComplexMat operator*(const ComplexMat & rhs) const;
94 ComplexMat operator/(const ComplexMat & rhs) const;
95 ComplexMat operator+(const ComplexMat & rhs) const;
97 //multiplying or adding constant
98 ComplexMat operator*(const float & rhs) const;
99 ComplexMat operator+(const float & rhs) const;
101 //multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
102 ComplexMat mul(const ComplexMat & rhs) const;
104 //multiplying element-wise multichannel by one channel mats (rhs mat is with multiple channel)
105 ComplexMat mul2(const ComplexMat & rhs) const;
107 friend std::ostream & operator<<(std::ostream & os, const ComplexMat & mat)
109 float *data_cpu = (float*) malloc(mat.rows*mat.cols*mat.n_channels*sizeof(cufftComplex));
110 CudaSafeCall(cudaMemcpy(data_cpu, mat.p_data, mat.rows*mat.cols*mat.n_channels*sizeof(cufftComplex), cudaMemcpyDeviceToHost));
111 //for (int i = 0; i < mat.n_channels; ++i){
112 for (int i = 0; i < 1; ++i){
113 os << "Channel " << i << std::endl;
114 for (int j = 0; j < mat.rows; ++j) {
115 for (int k = 0; k < 2*mat.cols-2; k+=2)
116 os << "(" << data_cpu[j*2*mat.cols + k] << "," << data_cpu[j*2*mat.cols + (k+1)] << ")" << ", ";
117 os << "(" << data_cpu[j*2*mat.cols + 2*mat.cols-2] << "," << data_cpu[j*2*mat.cols + 2*mat.cols-1] << ")" << std::endl;
124 void operator=(ComplexMat & rhs);
125 void operator=(ComplexMat && rhs);
129 mutable float *p_data = nullptr;
132 #endif // COMPLEXMAT_H