I've ran into a problem trying to expose dynamic C++ library functions, linked to OpenCV and using OpenCV's Mat datatype, to Python 2.7 with the use of Numpy ndarray.
I've come up with a solution similar to lightalchemist's solution here and I've also tried using boost::python and boost::numpy (also linked to Python 2.7) described in this SO question.
Right now I'm sticking with the former. I've got to a point where I can load the module in iPython, and I see a function I'm trying to port using the inspect module, I can even call it and it even executes. However, the problem occurs specifically when I try to use the NumpyAllocator (see lightalchemist's solution) class to convert the Mat object back to ndarray. Firstly, when I try to call pyopencv_from function from an outside C++ executable, and it uses the NumpyAllocator as it's coded, it segfaults at
PyEnsureGIL gil;
, with no message, every time. Lightalchemist's solution doesn't use it in pyopencv_to (EDIT: if the ndarray passed-in is already allocated), and it seems to work. However, the official OpenCV cv2.cpp does use the allocator in that as well, so if I try to use that the function can't even convert the input ndarray to Mat.
When I try to use the module from iPython, it sees the function. Again, it executes it correctly (prints progress to the console), but when it reaches the pyopencv_from, it segfaults and kills the iPython shell.
EDIT: I'm using exaclty the same source as lightalchemist, except I'm exposing a single function, the same way as the official OpenCV port does:
static PyMethodDef methods[] = {
{"findEdgesCGTG", (PyCFunction)pycvex_findEdgesCGTG, METH_KEYWORDS, "findEdgesCGTG(source) -> edgeGradient, edgeOrientations"},
{NULL, NULL}
};
extern "C"
#if defined WIN32 || defined _WIN32
__declspec(dllexport)
#endif
void initcvex()
{
import_array();
PyObject* m = Py_InitModule(MODULESTR, methods);
PyObject* d = PyModule_GetDict(m);
opencv_error = PyErr_NewException((char*)MODULESTR".error", NULL, NULL);
PyDict_SetItemString(d, "error", opencv_error);
}
Does anyone have any clue on how to resolve this conversion problem?