Ask Your Question

Revision history [back]

Segmentation fault while using pyopencv_from from cv2.cpp

I were trying to embed python in my C++ application. To convert cv::Mat from C++ to PyObject I were using cv2.cpp from opencv pythonwrapper sources.

//Sample Code.

cv::Mat mat = cv::imread("shirt.png", 1); 
pValue = pyopencv_from(mat);

//Segmentaion fault occurs during mat.CopyTo function.

template<> PyObject* pyopencv_from(const Mat& m)
{
    printf("pyopencv_from 1\n");
    if( !m.data )
        Py_RETURN_NONE;
    Mat temp, *p = (Mat*)&m;
    printf("pyopencv_from 2\n");
    if(!p->u || p->allocator != &g_numpyAllocator)
    {
        printf("pyopencv_from 3\n");
        temp.allocator = &g_numpyAllocator;
        printf("pyopencv_from 4\n");
        ERRWRAP2(m.copyTo(temp));
        printf("pyopencv_from 5\n");
        p = &temp;
    }
    printf("pyopencv_from 6\n");
    PyObject* o = (PyObject*)p->u->userdata;
    printf("pyopencv_from 7\n");
    Py_INCREF(o);
    return o;
}

Please provide any hints /solution . Thank you