File size: 7,243 Bytes
7fc5a59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifndef OPENPOSE_CORE_MAT_HPP
#define OPENPOSE_CORE_MAT_HPP

#include <memory> // std::shared_ptr
#include <openpose/core/macros.hpp>

namespace op
{
    // Convert from Matrix into cv::Mat. Usage example:
        // #include <opencv2/core/core.hpp>
        // ...
        // cv::Mat opMat = OP2CVMAT(cv::Mat());
    #define OP_OP2CVMAT(opMat) \
        (*((cv::Mat*)((opMat).getCvMat())))

    // Convert from Matrix into const cv::Mat. Usage example:
        // #include <opencv2/core/core.hpp>
        // ...
        // cv::Mat opMat = OP2CVCONSTMAT(cv::Mat());
    #define OP_OP2CVCONSTMAT(opMat) \
        (*((cv::Mat*)((opMat).getConstCvMat())))

    // Convert from cv::Mat into Matrix. Usage example:
        // #include <opencv2/core/core.hpp>
        // ...
        // Matrix opMat = CV2OPMAT(Matrix());
    #define OP_CV2OPMAT(cvMat) \
        (op::Matrix((void*)&(cvMat)))

    // Convert from cv::Mat into const Matrix. Usage example:
        // #include <opencv2/core/core.hpp>
        // ...
        // Matrix opMat = CV2OPCONSTMAT(Matrix());
    #define OP_CV2OPCONSTMAT(cvMat) \
        (op::Matrix((const void*)&(cvMat)))

    // Convert from std::vector<Matrix> into std::vector<cv::Mat>. Usage example:
        // #include <opencv2/core/core.hpp>
        // ...
        // std::vector<Matrix> opMats; // Assume filled
        // OP_OP2CVVECTORMAT(cvMats, opMats);
    #define OP_OP2CVVECTORMAT(cvMats, opMats) \
        std::vector<cv::Mat> cvMats; \
        for (auto& opMat : (opMats)) \
        { \
            const auto cvMat = OP_OP2CVCONSTMAT(opMat); \
            cvMats.emplace_back(cvMat); \
        }

    // Convert from std::vector<cv::Mat> into std::vector<Matrix>. Usage example:
        // #include <opencv2/core/core.hpp>
        // ...
        // std::vector<cv::Mat> cvMats; // Assume filled
        // OP_CV2OPVECTORMAT(opMats, cvMats);
    #define OP_CV2OPVECTORMAT(opMats, cvMats) \
        std::vector<op::Matrix> opMats; \
        for (auto& cvMat : (cvMats)) \
        { \
            const auto opMat = OP_CV2OPMAT(cvMat); \
            opMats.emplace_back(opMat); \
        }

    // Convert from std::vector<cv::Mat> into std::vector<Matrix>. Usage example:
        // #include <opencv2/core/core.hpp>
        // ...
        // // Equivalents:
        // OP_CV_VOID_FUNCTION(opMat, size());
        // // and
        // OP_OP2CVMAT(cvMat, opMat);
        // cvMat.size();
    #define OP_MAT_VOID_FUNCTION(opMat, function) \
        { \
            cv::Mat cvMat = OP_OP2CVMAT(cvMat, opMat); \
            cvMat.function; \
        }
    #define OP_CONST_MAT_VOID_FUNCTION(opMat, function) \
        { \
            const cv::Mat cvMat = OP_OP2CVCONSTMAT(opMat); \
            cvMat.function; \
        }
    #define OP_MAT_RETURN_FUNCTION(outputVariable, opMat, function) \
        { \
            cv::Mat cvMat = OP_OP2CVMAT(cvMat, opMat); \
            outputVariable = cvMat.function; \
        }
    #define OP_CONST_MAT_RETURN_FUNCTION(outputVariable, opMat, function) \
        { \
            const cv::Mat cvMat = OP_OP2CVCONSTMAT(opMat); \
            outputVariable = cvMat.function; \
        }

    /**
     * Matrix: Bind of cv::Mat to avoid OpenCV as dependency in the headers.
     */
    class OP_API Matrix
    {
    public:
        /**
         * @param matrixesResized For 3-D OpenPose, if >1, it will assume the image is composed of
         * numberImagesStackedHorizontally horizontally stacked images. It must be already resized to avoid
         * internally allocating/removing elements of std::vector (to avoid errors if using different std DLLs)
         * @param cvMatPtr should be a cv::Mat element or it will provoke a core dumped. Done to
         * avoid explicitly exposing 3rdparty libraries on the headers.
         */
        static void splitCvMatIntoVectorMatrix(std::vector<Matrix>& matrixesResized, const void* const cvMatPtr);

        Matrix();

        /**
         * @param cvMatPtr should be a cv::Mat element or it will provoke a core dumped. Done to
         * avoid explicitly exposing 3rdparty libraries on the headers.
         */
        explicit Matrix(const void* cvMatPtr);

        /**
         * Analog to cv::Mat(int rows, int cols, int type, void *data, size_t step=AUTO_STEP)
         */
        explicit Matrix(const int rows, const int cols, const int type);

        /**
         * Analog to cv::Mat(int rows, int cols, int type, void *data, size_t step=AUTO_STEP)
         * Very important: This Matrix will only "borrow" this pointer, so the caller must make sure to maintain the
         * memory allocated until this Matrix destructor is called and also to handle the ucharPtr memory deallocation.
         * @param ucharPtr should be a cv::Mat::data (or analog) element or it will provoke a core dumped. Done to
         * avoid explicitly exposing 3rdparty libraries on the headers.
         */
        explicit Matrix(const int rows, const int cols, const int type, void* cvMatPtr);

        Matrix clone() const;

        /**
         * @return cv::Mat*.
         */
        void* getCvMat();

        /**
         * @return const cv::Mat*.
         */
        const void* getConstCvMat() const;

        /**
         * Equivalent to cv::Mat::data
         * @return A raw pointer to the internal data of cv::Mat.
         */
        unsigned char* data();
        /**
         * Equivalent to cv::Mat::data
         * @return A raw pointer to the internal data of cv::Mat.
         */
        const unsigned char* dataConst() const;
        /**
         * Similar to dataConst(), but it allows the data to be edited.
         * This function is only implemented for Pybind11 usage.
         * @return A raw pointer to the internal data of cv::Mat.
         */
        unsigned char* dataPseudoConst() const;

        /**
         * Equivalent to cv::Mat::eye
         */
        static Matrix eye(const int rows, const int cols, const int type);
        /**
         * Equivalent to cv::Mat::cols
         */
        int cols() const;
        /**
         * Equivalent to cv::Mat::rows
         */
        int rows() const;
        /**
         * Equivalent to cv::Mat::size[dimension]
         */
        int size(const int dimension) const;
        /**
         * Equivalent to cv::Mat::dims
         */
        int dims() const;

        /**
         * Equivalent to their analog cv::Mat functions
         */
        bool isContinuous() const;
        bool isSubmatrix() const;
        size_t elemSize() const;
        size_t elemSize1() const;
        int type() const;
        int depth() const;
        int channels() const;
        size_t step1(const int i = 0) const;
        bool empty() const;
        size_t total() const;
        int checkVector(const int elemChannels, const int depth = -1, const bool requireContinuous = true) const;

        /**
         * Similar to their analog cv::Mat functions
         */
        void setTo(const double value);
        void copyTo(Matrix& outputMat) const;

    private:
        // PIMPL idiom
        // http://www.cppsamples.com/common-tasks/pimpl.html
        struct ImplMatrix;
        std::shared_ptr<ImplMatrix> spImpl;
    };
}

#endif // OPENPOSE_CORE_MAT_HPP