30 #ifndef _MIRA_TOOLBOXES_PYTHON_IMAGEWRAPPER_H_ 31 #define _MIRA_TOOLBOXES_PYTHON_IMAGEWRAPPER_H_ 33 #define BOOST_HAS_NUMPY_API (BOOST_VERSION >= 106300) 37 #if BOOST_HAS_NUMPY_API 38 #include <boost/python/numpy.hpp> 40 #include <boost/python/numeric.hpp> 45 #if BOOST_HAS_NUMPY_API 47 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 50 #include <numpy/ndarrayobject.h> 58 #if BOOST_HAS_NUMPY_API 67 static boost::python::numpy::dtype
getNumpyDtype(
int cvDtype) {
68 namespace np = boost::python::numpy;
71 return np::dtype::get_builtin<uint8>();
73 return np::dtype::get_builtin<int8>();
75 return np::dtype::get_builtin<uint16>();
77 return np::dtype::get_builtin<int16>();
79 return np::dtype::get_builtin<int32>();
81 return np::dtype::get_builtin<float>();
83 return np::dtype::get_builtin<double>();
85 return np::dtype::get_builtin<char>();
89 static int getCvDtype(boost::python::numpy::dtype numpyDtype) {
90 namespace np = boost::python::numpy;
92 if (numpyDtype == np::dtype::get_builtin<uint8>()) {
94 }
else if (numpyDtype == np::dtype::get_builtin<int8>()) {
96 }
else if (numpyDtype == np::dtype::get_builtin<uint16>()) {
98 }
else if (numpyDtype == np::dtype::get_builtin<int16>()) {
100 }
else if (numpyDtype == np::dtype::get_builtin<int32>()) {
102 }
else if (numpyDtype == np::dtype::get_builtin<float>()) {
104 }
else if (numpyDtype == np::dtype::get_builtin<double>()) {
107 MIRA_THROW(XLogical,
"Cannot map given dtype from NumPy to OpenCV");
126 numpyDtype = NPY_UBYTE;
129 numpyDtype = NPY_BYTE;
132 numpyDtype = NPY_USHORT;
135 numpyDtype = NPY_SHORT;
138 numpyDtype = NPY_INT;
141 numpyDtype = NPY_FLOAT;
144 numpyDtype = NPY_DOUBLE;
147 MIRA_THROW(XLogical,
"Cannot convert CV dtype to NumPy dtype!");
184 #if BOOST_HAS_NUMPY_API 186 static int getCVByteDepth(
int cvDtype) {
199 default:
MIRA_THROW(XLogical,
"Cannot convert CV dtype to byte depth!");
211 template<
typename TPixel,
int TChannels>
219 using namespace boost::python;
222 return boost::python::object();
226 #if BOOST_HAS_NUMPY_API 228 int nBytes = getCVByteDepth(image.
depth());
231 tuple stride = make_tuple(image.
width() * image.
channels() * nBytes,
233 if (mat.isContinuous()) {
234 return numpy::from_data((TPixel *) mat.data, dtype,
235 shape, stride,
object());
238 return numpy::from_data((TPixel *) clone.data, dtype,
239 shape, stride,
object());
245 if(mat.isContinuous()) {
246 pyObj = PyArray_SimpleNewFromData(
252 pyObj = PyArray_SimpleNewFromData(
256 handle<> handle(pyObj);
257 return numeric::array(handle);
261 #if BOOST_HAS_NUMPY_API 263 static cv::Mat arrayToMat(
const boost::python::numpy::ndarray &arr)
269 PyObject * pyObj = arr.ptr();
270 if (!PyArray_Check(pyObj))
MIRA_THROW(XRuntime,
"Not an array object");
272 #if BOOST_HAS_NUMPY_API 273 int ndims = arr.get_nd();
276 int size[2] = {(int) arr.get_shape()[0], (int) arr.get_shape()[1]};
277 size_t step[2] = {(size_t) arr.get_strides()[0], (size_t) arr.get_strides()[1]};
283 type = CV_MAKETYPE(
type, arr.get_shape()[2]);
286 void *data = (
void *) arr.get_data();
289 PyArrayObject* arrayObj =
reinterpret_cast<PyArrayObject*
>(pyObj);
291 int ndims = PyArray_NDIM(arrayObj);
295 size[0] = PyArray_DIMS(arrayObj)[0];
296 size[1] = PyArray_DIMS(arrayObj)[1];
299 step[0] = PyArray_STRIDES(arrayObj)[0];
300 step[1] = PyArray_STRIDES(arrayObj)[1];
306 type = CV_MAKETYPE(
type, PyArray_DIMS(arrayObj)[2]);
309 void* data = PyArray_DATA(arrayObj);
324 template<
typename TPixel,
int TChannels>
333 #if BOOST_HAS_NUMPY_API 334 static void setMat(
ImgType &image,
const boost::python::numpy::ndarray &arr)
336 static void setMat(
ImgType& image,
const boost::python::numeric::array& arr)
353 #if BOOST_HAS_NUMPY_API 354 static void setMat(
ImgType &image,
const boost::python::numpy::ndarray &arr)
356 static void setMat(
ImgType& image,
const boost::python::numeric::array& arr)
365 #endif //_MIRA_TOOLBOXES_PYTHON_IMAGEWRAPPER_H_
static void setMat(ImgType &image, const boost::python::numeric::array &arr)
Set image matrix from numpy.ndarray.
Definition: ImageWrapper.h:336
mira::Img< void, 1 > ImgType
Definition: ImageWrapper.h:347
Dummy image accessor base struct.
Definition: ImageWrapper.h:208
#define MIRA_THROW(ex, msg)
PropertyHint type(const std::string &t)
static boost::python::object getMat(const mira::Img< TPixel, TChannels > &image)
Get image matrix as numpy.ndarray.
Definition: ImageWrapper.h:218
Include this instead of boost/python.hpp to reduce compile time warning spam from Boost internal inco...
const cv::Mat & getMat() const
PropertyHint step(const T &step)
static cv::Mat arrayToMat(const boost::python::numeric::array &arr)
Definition: ImageWrapper.h:265
static int getNumpyDtype(int cvDtype)
Return numerical numpy dtype identifier corresponding to a numerical dtype from OpenCV.
Definition: ImageWrapper.h:120
static int getCvDtype(int numpyDtype)
Definition: ImageWrapper.h:152
static void setMat(ImgType &image, const boost::python::numeric::array &arr)
Set image matrix from numpy.ndarray.
Definition: ImageWrapper.h:356
mira::Img< TPixel, TChannels > ImgType
Definition: ImageWrapper.h:327
Dummy image accessor struct.
Definition: ImageWrapper.h:319