【发布时间】:2014-05-05 05:09:42
【问题描述】:
我使用python作为接口来操作图像,但是当我需要编写一些自定义函数来操作矩阵时,我在迭代时发现numpy.ndarray太慢了。我想将数组转移到 cv::Mat 以便我可以轻松处理它,因为我曾经基于 cv::Mat 结构编写用于图像处理的 C++ 代码。
我的 test.cpp:
#include <Python.h>
#include <iostream>
using namespace std;
static PyObject *func(PyObject *self, PyObject *args) {
printf("What should I write here?\n");
// How to parse the args to get an np.ndarray?
// cv::Mat m = whateverFunction(theParsedArray);
return Py_BuildValue("s", "Any help?");
}
static PyMethodDef My_methods[] = {
{ "func", (PyCFunction) func, METH_VARARGS, NULL },
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC initydw_cvpy(void) {
PyObject *m=Py_InitModule("ydw_cvpy", My_methods);
if (m == NULL)
return;
}
main.py:
if __name__ == '__main__':
print ydw_cvpy.func()
结果:
What should I write here?
Any help?
【问题讨论】:
标签: python c++ opencv numpy python-c-api