title: Passing OpenCv Mat from C++ to Python
body: <p>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.</p>
  
  <p>What I did in the C++ part of the code was:</p>
  
  <pre><code>#include &lt;Python.h&gt;
  #include &lt;arrayobject.h&gt;
  #include &lt;iostream&gt;
  #include &lt;opencv2/opencv.hpp&gt;
  
  
  #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*)&amp;dimensions, NPY_UINT8, image.data);
  
      PyTuple_SetItem(pArgs, 0, pValue);
      PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
  
      if(pResult == NULL)
          cout&lt;&lt;"Calling the add method failed"&lt;&lt;endl;
  
      long result = PyLong_AsLong(pResult);
      cout&lt;&lt;"Result = "&lt;&lt;result&lt;&lt;endl;
  
      Py_Finalize();
      return 0;
  }
  </code></pre>
  
  <p>This code compiles and runs.</p>
  
  <p>For the Python part:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The Python code receives the numpy array from C++ (I think) and when I print the contents of <code>a</code>, I have an output (so I think I am receiving the image from C++).</p>
  
  <p>Now I need to convert the data in <code>a</code> to a cv2 Mat in Python so that I can work on it.</p>
  
  <p>Once I reach the <code>mat_array = cv2.fromarray(a, numpy.float32)</code> line or <code>vis0 = cv.fromarray(a)</code> the code crashes with the following output:</p>
  
  <pre><code>Exception ignored in: &lt;module 'threading' from '/usr/lib/python3.5/threading.py'&gt;
  Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 1283, in _shutdown
  assert tlock.locked()
  SystemError: &lt;built-in method locked of _thread.lock object at 0x7ff0f34d20d0&gt; returned a result with an error set
  </code></pre>
  
  <p>How do I correctly send / receive the Mat object?</p>