我正在尝试将图像从 C 传递到 python 并将数组从 python 传递回 C 并获取其中的长度和数据。这是我的 Python 代码:
def detect(pI,col,row,channels):
I_reshape = np.asarray(list(pI), dtype=np.ubyte)
I_reshape = np.reshape(I_reshape,(row,col,channels))
cv2.imshow("testWin",I_reshape)
cv2.waitKey(0)
a = np.arange(4).reshape((2, 2))
return a;
这是我的 C 代码:
#include <python.h>
#include <direct.h>
#include <opencv2\opencv.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pArg, *pResult;
Py_Initialize();
pName = PyUnicode_DecodeFSDefault("getArray");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
pFunc = PyObject_GetAttrString(pModule, "returnArray");
pArgs = PyTuple_New(4);
pArg = PyByteArray_FromStringAndSize((char*)img.data, img.total() * img.elemSize());
PyTuple_SetItem(pArgs, 0, pArg);
PyTuple_SetItem(pArgs, 1, PyLong_FromLong(img.cols));
PyTuple_SetItem(pArgs, 2, PyLong_FromLong(img.rows));
PyTuple_SetItem(pArgs, 3, PyLong_FromLong(img.channels()));
pResult = PyObject_CallObject(pFunc, pArgs);
int SIZE = PyByteArray_Size(pResult);
Py_XDECREF(pResult);
uint *data = (uint *)PyByteArray_AsString(pResult);
Py_XDECREF(pResult);
printf("Return of call (size): %d\n", SIZE/sizeof(uint));
printf("Return of call (data): %d\n", data[0]);
Py_DECREF(pResult);
return 0;
}
问题是我得到了正确的大小,但我的数据只得到零。我哪里错了?