Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing OpenCv Mat from C++ to Python

Tags:

c++

python

opencv

I need to send an OpenCv image from C++ to Python to do some processing on it. The Mat will be received through the code but for simplicity I am using imread here for the question.

What I did in the C++ part of the code was:

#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:

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++).

Now I need to convert the data in a to a cv2 Mat in Python so that I can work on it.

Once I reach the mat_array = cv2.fromarray(a, numpy.float32) line or vis0 = cv.fromarray(a) the code crashes with the following output:

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?

like image 378
EYakoumi Avatar asked Aug 30 '18 06:08

EYakoumi


People also ask

Is OpenCV in C faster than Python?

Slower run time : Compared to C++, your programs in Python will typically run slower. To add an extra punch you can use the GPU ( using CUDA or OpenCL ) in OpenCV (C++) and have code that runs 10x faster than the Python implementation.

Is OpenCV better in C++ or Python?

I use both C++ and Python with OpenCV. For quick tests, prototyping, doing some simple operations Python is much faster. But when passing to a more serious development stage, stage, I prefer C++. So try to avoid the situation above in Python.

How do you create a mat in Python?

Creates a matrix header and allocates the matrix data. Python: cv. CreateMat(rows, cols, type) → mat Parameters: rows – Number of rows in the matrix cols – Number of columns in the matrix type – The type of the matrix elements in the form CV_<bit depth><S|U|F>C<number of channels> , where S=signed, U=unsigned, F=float.


1 Answers

Please find my answer here. You can also find other answer here. for converting numpy -> cv::Mat and cv::Mat -> numpy.

like image 63
Milind Deore Avatar answered Sep 29 '22 14:09

Milind Deore