【发布时间】:2012-11-24 13:54:19
【问题描述】:
我在尝试使用 Numpy ndarray 将链接到 OpenCV 并使用 OpenCV 的 Mat 数据类型的动态 C++ 库函数公开到 Python 2.7 时遇到了问题.
我想出了一个类似于lightalchemist's solution here 的解决方案,我也尝试过使用 boost::python 和 boost::numpy(也链接到 Python 2.7)described in this SO question.
现在我坚持前者。我已经到了可以在 iPython 中加载模块的地步,并且我看到一个我正在尝试使用 inspect 模块移植的函数,我什至可以调用它并且它甚至可以执行。但是,当我尝试使用 NumpyAllocator (see lightalchemist's solution) 类将 Mat 对象转换回 ndarray 时,就会出现问题。 首先,当我尝试从外部 C++ 可执行文件中调用 pyopencv_from 函数,并且它使用 NumpyAllocator 进行编码时,它会出现段错误
PyEnsureGIL gil;
,每次都没有消息。 Lightalchemist 的解决方案不在 pyopencv_to 中使用它(编辑:如果传入的 ndarray 已经分配),它似乎可以工作。但是,官方的 OpenCV cv2.cpp 也确实使用了分配器,所以如果我尝试使用该函数,甚至无法将输入的 ndarray 转换为 Mat。
当我尝试使用 iPython 中的模块时,它会看到该函数。再次,它正确执行它(将进度打印到控制台),但是当它到达 pyopencv_from 时,它会出现段错误并终止 iPython shell。
编辑:我使用与 lightalchemist 完全相同的来源,只是我公开了一个函数,与官方 OpenCV 端口的方式相同:
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);
}
有人知道如何解决这个转换问题吗?
【问题讨论】:
标签: python opencv numpy python-2.7 boost-python