简体   繁体   English

将OpenCv Mat从C ++传递到Python

[英]Passing OpenCv Mat from C++ to Python

I need to send an OpenCv image from C++ to Python to do some processing on it. 我需要将一个OpenCv图像从C ++发送到Python来对其进行一些处理。 The Mat will be received through the code but for simplicity I am using imread here for the question. Mat将通过代码收到,但为了简单起见,我在这里使用imread来解决问题。

What I did in the C++ part of the code was: 我在C ++部分代码中所做的是:

#include <Python.h>
#include <arrayobject.h>
#include <iostream>
#include <opencv2/opencv.hpp>


#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION


using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Mat image = imread("test.jpg");

    Py_Initialize();
    PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;

    pName = PyUnicode_FromString("prog");
    if (pName == NULL)
    {
        PyErr_Print();
        return 0;
    }

    pModule = PyImport_Import(pName);
    if (pModule == NULL)
    {
        PyErr_Print();
        return 0;
    }
    pDict = PyModule_GetDict(pModule);
    pFunc = PyDict_GetItemString(pDict, "add");
    if (pFunc == NULL)
    {
        PyErr_Print();
        return 0;
    }

    pArgs = PyTuple_New(1);
    import_array ();

    npy_intp dimensions[3] = {image.rows, image.cols, image.channels()};
    pValue = PyArray_SimpleNewFromData(image.dims + 1, (npy_intp*)&dimensions, NPY_UINT8, image.data);

    PyTuple_SetItem(pArgs, 0, pValue);
    PyObject* pResult = PyObject_CallObject(pFunc, pArgs);

    if(pResult == NULL)
        cout<<"Calling the add method failed"<<endl;

    long result = PyLong_AsLong(pResult);
    cout<<"Result = "<<result<<endl;

    Py_Finalize();
    return 0;
}

This code compiles and runs. 此代码编译并运行。

For the Python part: 对于Python部分:

import cv2
import numpy as np

def add (a):
    print ("Contents of a :")
    print (a)

    # mat_array = cv2.fromarray(a, numpy.float32)
    vis0 = cv.fromarray(a)

    return 0

The Python code receives the numpy array from C++ (I think) and when I print the contents of a , I have an output (so I think I am receiving the image from C++). 的Python代码接收numpy的阵列从C ++(我认为)和当我打印的内容a ,我有一个输出(所以我想我选自C接收图像++)。

Now I need to convert the data in a to a cv2 Mat in Python so that I can work on it. 现在,我需要将数据转换成a以Python中的CV2垫,这样我可以在它的工作。

Once I reach the mat_array = cv2.fromarray(a, numpy.float32) line or vis0 = cv.fromarray(a) the code crashes with the following output: 一旦我到达mat_array = cv2.fromarray(a, numpy.float32)行或者vis0 = cv.fromarray(a) ,代码就会崩溃并输出以下内容:

Exception ignored in: <module 'threading' from '/usr/lib/python3.5/threading.py'>
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 1283, in _shutdown
assert tlock.locked()
SystemError: <built-in method locked of _thread.lock object at 0x7ff0f34d20d0> returned a result with an error set

How do I correctly send / receive the Mat object? 如何正确发送/接收Mat对象?

Please find my answer here . 在这里找到我的答案。 You can also find other answer here . 你也可以在这里找到其他答案。 for converting numpy -> cv::Mat and cv::Mat -> numpy . 转换numpy -> cv::Matcv::Mat -> numpy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM