opencv icon indicating copy to clipboard operation
opencv copied to clipboard

pyopencv_from crash while calling Py_INCREF (opencv 4.1.2 python binding with cv2.cpp)

Open ErhanOnur opened this issue 1 year ago • 1 comments

Hello,

I am doing a python binding for a function that returns opencv mat object. so I used pyopencv_from for that as below:

static PyObject *method_convertLeaf2Mat(PyObject *self, PyObject *args) {
    /* inner details....*/
    const cv::Mat& m = i_leaf->getCvMat(cmd_context);
    return pyopencv_from(m);
}

But when pyopencv_from is called, the program crashes. If I remove "Py_INCREF(o);" part from pyopencv_from function then I got following result:

>>> x = mod_imgproc.convertLeaf2Mat(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: <built-in function convertLeaf2Mat> returned NULL without setting an error

Here is the pyopencv_from function definition:

template<>
PyObject* pyopencv_from(const Mat& m)
{
    if( !m.data )
        Py_RETURN_NONE;
    Mat temp, *p = (Mat*)&m;
    if(!p->u || p->allocator != &g_numpyAllocator)
    {
        temp.allocator = &g_numpyAllocator;
        ERRWRAP2(m.copyTo(temp));
        p = &temp;
    }
    PyObject* o = (PyObject*)p->u->userdata;
    Py_INCREF(o);
    return o;
}

I am using opencv 4.1.2. Which part could be wrong?

Thank you

ErhanOnur avatar Feb 28 '23 23:02 ErhanOnur

Hi It is working now. Can you please delete this issue? I started using gen.py. gen.py was not creating the wrapper function. So I did some changes on it, then it started creating the wrapper functions.

ErhanOnur avatar Mar 12 '23 16:03 ErhanOnur