-
Notifications
You must be signed in to change notification settings - Fork 23
/
matrix.h
92 lines (75 loc) · 1.68 KB
/
matrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef __MATRIX_H__
#define __MATRIX_H__
#include <opencv2/opencv.hpp>
template <typename T>
class Matrix : public cv::Mat_<T>
{
public:
Matrix() : cv::Mat_<T>() {}
Matrix(int size1) { create(size1); }
Matrix(int size1, int size2) { create(size1, size2); }
Matrix(int size1, int size2, int size3) { create(size1, size2, size3); }
// 1D Matrix
void create(int size1)
{
cv::Mat_<T>::create(size1, 1);
this->size1 = size1;
this->size2 = 0;
this->size3 = 0;
this->step1 = 0;
this->step2 = 0;
}
// 2D Matrix
void create(int size1, int size2)
{
cv::Mat_<T>::create(size1, size2);
this->size1 = size1;
this->size2 = size2;
this->size3 = 0;
this->step1 = size2;
this->step2 = 0;
}
// 3D Matrix
void create(int size1, int size2, int size3)
{
const int sizes[3] = { size1, size2, size3 };
cv::Mat_<T>::create(3, sizes);
this->size1 = size1;
this->size2 = size2;
this->size3 = size3;
this->step1 = size2 * size3;
this->step2 = size3;
}
// 1D Matrix
inline T& operator()(int i)
{
return *((T*)this->data + i);
}
inline const T& operator()(int i) const
{
return *((T*)this->data + i);
}
// 2D Matrix
inline T& operator()(int i, int j)
{
return *((T*)this->data + i * step1 + j);
}
inline const T& operator()(int i, int j) const
{
return *((T*)this->data + i * step1 + j);
}
// 3D Matrix
inline T& operator()(int i, int j, int k)
{
return *((T*)this->data + i * step1 + j * step2 + k);
}
inline const T& operator()(int i, int j, int k) const
{
return *((T*)this->data + i * step1 + j * step2 + k);
}
int size1, size2, size3;
int step1, step2;
};
using Matrixf = Matrix<float>;
using Matrixi = Matrix<int>;
#endif // !__MATRIX_H__