Skip to content

Commit 7be045c

Browse files
committed
feat; update ndarray converter from cv2
1 parent 225589d commit 7be045c

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

ndarray_converter.cpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ bool NDArrayConverter::init_numpy() {
2525
return true;
2626
}
2727

28-
/*
29-
* The following conversion functions are taken/adapted from OpenCV's cv2.cpp file
30-
* inside modules/python/src2 folder (OpenCV 3.1.0)
31-
*/
28+
// The following conversion functions are taken/adapted from OpenCV's cv2.cpp file
29+
// inside modules/python/src2 folder (OpenCV 4.5.2)
30+
3231

3332
static PyObject* opencv_error = 0;
3433

@@ -129,7 +128,7 @@ class NumpyAllocator : public MatAllocator
129128
_sizes[i] = sizes[i];
130129
if( cn > 1 )
131130
_sizes[dims++] = cn;
132-
PyObject* o = PyArray_SimpleNew(dims, _sizes, typenum);
131+
PyObject* o = PyArray_SimpleNew(dims, _sizes.data(), typenum);
133132
if(!o)
134133
CV_Error_(Error::StsError, ("The numpy array of typenum=%d, ndims=%d can not be created", typenum, dims));
135134
return allocate(o, dims0, sizes, type, step);
@@ -192,7 +191,7 @@ bool NDArrayConverter::toMat(PyObject *o, Mat &m)
192191
m = Mat(sz, 1, CV_64F);
193192
for( i = 0; i < sz; i++ )
194193
{
195-
PyObject* oi = PyTuple_GET_ITEM(o, i);
194+
PyObject* oi = PyTuple_GetItem(o, i);
196195
if( PyInt_Check(oi) )
197196
m.at<double>(i) = (double)PyInt_AsLong(oi);
198197
else if( PyFloat_Check(oi) )
@@ -276,11 +275,11 @@ bool NDArrayConverter::toMat(PyObject *o, Mat &m)
276275

277276
if (needcopy)
278277
{
279-
//if (info.outputarg)
280-
//{
281-
// failmsg("Layout of the output array %s is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)", info.name);
282-
// return false;
283-
//}
278+
// if (info.outputarg)
279+
// {
280+
// failmsg("Layout of the output array %s is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)", info.name);
281+
// return false;
282+
// }
284283

285284
if( needcast ) {
286285
o = PyArray_Cast(oarr, new_typenum);

ndarray_converter.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class NDArrayConverter {
99
public:
1010
// must call this first, or the other routines don't work!
1111
static bool init_numpy();
12-
12+
1313
static bool toMat(PyObject* o, cv::Mat &m);
1414
static PyObject* toNDArray(const cv::Mat& mat);
1515
};
@@ -21,22 +21,22 @@ class NDArrayConverter {
2121
#include <pybind11/pybind11.h>
2222

2323
namespace pybind11 { namespace detail {
24-
24+
2525
template <> struct type_caster<cv::Mat> {
2626
public:
27-
27+
2828
PYBIND11_TYPE_CASTER(cv::Mat, _("numpy.ndarray"));
29-
30-
bool load(handle src, bool) {
29+
30+
bool load(handle src, bool /* convert */) {
3131
return NDArrayConverter::toMat(src.ptr(), value);
3232
}
33-
33+
3434
static handle cast(const cv::Mat &m, return_value_policy, handle defval) {
3535
return handle(NDArrayConverter::toNDArray(m));
3636
}
3737
};
38-
39-
38+
39+
4040
}} // namespace pybind11::detail
4141

4242
# endif

0 commit comments

Comments
 (0)