-
Notifications
You must be signed in to change notification settings - Fork 40
/
ndarray_converter.h
42 lines (28 loc) · 869 Bytes
/
ndarray_converter.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
# ifndef __NDARRAY_CONVERTER_H__
# define __NDARRAY_CONVERTER_H__
#include <Python.h>
#include <opencv2/core/core.hpp>
class NDArrayConverter {
public:
// must call this first, or the other routines don't work!
static bool init_numpy();
static bool toMat(PyObject* o, cv::Mat &m);
static PyObject* toNDArray(const cv::Mat& mat);
};
//
// Define the type converter
//
#include <pybind11/pybind11.h>
namespace pybind11 { namespace detail {
template <> struct type_caster<cv::Mat> {
public:
PYBIND11_TYPE_CASTER(cv::Mat, _("numpy.ndarray"));
bool load(handle src, bool /* convert */) {
return NDArrayConverter::toMat(src.ptr(), value);
}
static handle cast(const cv::Mat &m, return_value_policy, handle defval) {
return handle(NDArrayConverter::toNDArray(m));
}
};
}} // namespace pybind11::detail
# endif