Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Pass Mat to Python Script ?

Hi, I am using OpenCV v2.4.13.6 with C++ in QT Creator. I am trying to pass an OpenCV Mat object to a Python script using Python interpreter. I need to convert the OpenCV Mat to a PyObject so that I can pass it to the Python script.

Something like this:

PyObject *pName, *pModule, *pFunc, *pValue, *pMat;
Mat img;
Py_Initialize();
pName = PyUnicode_FromString("load-mat"); //python script is named load-mat
pModule = PyImport_Import(pName);
pFunc = PyObject_GetAttrString(pModule, "load_mat") //python function is named load_mat()
############################################
//Need to convert Mat img to PyObject *pMat
############################################
pValue = PyObject_CallFunctionObjArgs(pFunc, pMat);
Py_Finalize();

I read somewhere:

PyObject* pyopencv_from(const cv::Mat& m)
{
  if( !m.data )
      Py_RETURN_NONE;
  cv::Mat temp, *p = (cv::Mat*)&m;
  if(!p->refcount || p->allocator != &g_numpyAllocator)
  {
      temp.allocator = &g_numpyAllocator;
      m.copyTo(temp);
      p = &temp;
  }
  p->addref();
  return pyObjectFromRefcount(p->refcount);
}

pMat = pyopencv_from(img);

But placing that into my C++ code straight I get the following 2 errors:

error: 'g_numpyAllocator' was not declared in this scope
error: 'pyObjectFromRefcount' was not declared in this scope

What is the easiest method of getting this to work ? Thanks

Pass Mat to Python Script ?

Hi, I am using OpenCV v2.4.13.6 with C++ in QT Creator. I am trying to pass an OpenCV Mat object to a Python script using Python interpreter. I need to convert the OpenCV Mat to a PyObject so that I can pass it to the Python script.

Something like this:

PyObject *pName, *pModule, *pFunc, *pValue, *pMat;
Mat img;
Py_Initialize();
pName = PyUnicode_FromString("load-mat"); //python script is named load-mat
pModule = PyImport_Import(pName);
pFunc = PyObject_GetAttrString(pModule, "load_mat") //python function is named load_mat()
############################################
//Need to convert Mat img to PyObject *pMat
############################################
pValue = PyObject_CallFunctionObjArgs(pFunc, pMat);
Py_Finalize();

I read somewhere:

PyObject* pyopencv_from(const cv::Mat& m)
{
  if( !m.data )
      Py_RETURN_NONE;
  cv::Mat temp, *p = (cv::Mat*)&m;
  if(!p->refcount || p->allocator != &g_numpyAllocator)
  {
      temp.allocator = &g_numpyAllocator;
      m.copyTo(temp);
      p = &temp;
  }
  p->addref();
  return pyObjectFromRefcount(p->refcount);
}

pMat = pyopencv_from(img);

But placing that into my C++ code straight I get the following 2 errors:

error: 'g_numpyAllocator' was not declared in this scope
error: 'pyObjectFromRefcount' was not declared in this scope

What is the easiest method of getting this to work ? Thanks

Pass Mat to Python Script ?

Hi, I am using OpenCV v2.4.13.6 with C++ in QT Creator. I am trying to pass an OpenCV Mat object to a Python script using Python interpreter. I need to convert the OpenCV Mat to a PyObject so that I can pass it to the Python script.

Something like this:

PyObject *pName, *pModule, *pFunc, *pValue, *pMat;
Mat img;
Py_Initialize();
pName = PyUnicode_FromString("load-mat"); //python script is named load-mat
pModule = PyImport_Import(pName);
pFunc = PyObject_GetAttrString(pModule, "load_mat") //python function is named load_mat()
############################################
//Need to convert Mat img to PyObject *pMat
############################################
pValue = PyObject_CallFunctionObjArgs(pFunc, pMat);
Py_Finalize();

I read somewhere:

PyObject* pyopencv_from(const cv::Mat& m)
{
  if( !m.data )
      Py_RETURN_NONE;
  cv::Mat temp, *p = (cv::Mat*)&m;
  if(!p->refcount || p->allocator != &g_numpyAllocator)
  {
      temp.allocator = &g_numpyAllocator;
      m.copyTo(temp);
      p = &temp;
  }
  p->addref();
  return pyObjectFromRefcount(p->refcount);
}

pMat = pyopencv_from(img);

But placing that into my C++ code straight I get the following 2 errors:

error: 'g_numpyAllocator' was not declared in this scope
error: 'pyObjectFromRefcount' was not declared in this scope

What is the easiest method of getting this to work ? Thanks

Pass Mat to Python Script ?

Hi, I am using OpenCV v2.4.13.6 with C++ in QT Creator. I am trying to pass an OpenCV Mat object to a Python script using Python interpreter. I need to convert the OpenCV Mat to a PyObject so that I can pass it to the Python script.

Something like this:

PyObject *pName, *pModule, *pFunc, *pValue, *pMat;
Mat img;
Py_Initialize();
pName = PyUnicode_FromString("load-mat"); //python script is named load-mat
pModule = PyImport_Import(pName);
pFunc = PyObject_GetAttrString(pModule, "load_mat") //python function is named load_mat()
############################################
//Need to convert Mat img to PyObject *pMat
############################################
pValue = PyObject_CallFunctionObjArgs(pFunc, pMat);
pMat, NULL);
Py_Finalize();

I read somewhere:

PyObject* pyopencv_from(const cv::Mat& m)
{
  if( !m.data )
      Py_RETURN_NONE;
  cv::Mat temp, *p = (cv::Mat*)&m;
  if(!p->refcount || p->allocator != &g_numpyAllocator)
  {
      temp.allocator = &g_numpyAllocator;
      m.copyTo(temp);
      p = &temp;
  }
  p->addref();
  return pyObjectFromRefcount(p->refcount);
}

pMat = pyopencv_from(img);

But placing that into my C++ code straight I get the following 2 errors:

error: 'g_numpyAllocator' was not declared in this scope
error: 'pyObjectFromRefcount' was not declared in this scope

What is the easiest method of getting this to work ? Thanks