Skip to content

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

@ErhanOnur

Description

@ErhanOnur

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions