2014-11-24 86 views
0

我有一个使用OpenCV lib在Python中编写的图像处理函数(IMP)。如何使用OpenCV将帧缓冲从C传递到Python

现在我想从C代码调用IMP:

#include "/usr/include/python2.7/Python.h" 
int main() 
{ 
    PyObject *pName, *pModule, *pDict, *pFun, *pValue, main_module; 

// Initialize the Python Interpreter 
Py_Initialize(); 
// Build the name object 
pName = PyString_FromString("test"); 
if(pName)printf("OK\n"); 

// Load the module object 
pModule = PyImport_Import(pName); 

    // pFunc is also a borrowed reference 
    pFun = PyObject_GetAttrString(pModule, "IMP"); 

    if (PyCallable_Check(pFun)) 
    { 
    //PyObject_CallObject(pFun, NULL); 
    PyObject_CallFunction(pFun,"o",framebuffer); 
    } 
    else 
    { 
    PyErr_Print(); 
    } 

    // Clean up 
    Py_DECREF(pModule); 
    Py_DECREF(pName); 
    Py_DECREF(pFun); 
    // Finish the Python Interpreter 
    Py_Finalize(); 
    getchar(); 
    return 0; 
} 

如何准备“帧缓冲”传递到我的IMP Python函数?

任何人都可以帮助向我展示一个封装在CV2理解的对象中的示例图像,并使用上面的示例C代码将它传递给IMP?非常感谢你的帮助。

+0

你的'IMP'在做什么?它可能更容易,将其转移到C++。 – berak 2014-11-24 18:21:45

+0

它只是试图识别某些功能并返回yes或no。由于它是在嵌入式系统中运行,所以我使用C作为目标代码,但CV2 C接口已弃用,因此我将直接调用我的Python代码进行开发。如果它被证明了,那么我将来可能会把它翻译成C++,并且稍后再C调用C++ dll。 – 2014-11-24 18:54:42

回答

0

我想我找到了我需要的:只是在我的test.py创建模板“帧缓冲”:

import numpy as np 
import cv2 

framebuffer = cv2.imread('pic.jpg',cv2.IMREAD_COLOR) 

def IMP(p): 
    print "I am here!" 
    print "image.shape h,w,d=",p.shape 
    cv2.imshow("picture",p) 
    cv2.waitKey(0) 
    cv2.destroyAllWindows() 

,然后在我的test.c的,我用grep的“帧缓冲”从pDict进一步然后调用IMP:

#include "/usr/include/python2.7/Python.h" 
#include <numpy/arrayobject.h> 

int main() 
{ 
    PyObject *pName, *pModule, *pDict, *pFun, *pValue, *pArgs, *pFB; 
    int i,j; 

    // Initialize the Python Interpreter 
    Py_Initialize(); 

// Build the name object 
    pName = PyString_FromString("test"); 
    if(pName)printf("OK\n"); 

    // Load the module object 
    pModule = PyImport_Import(pName); 

    // pDict is a borrowed reference 
    pDict = PyModule_GetDict(pModule); 

    // pFunc is also a borrowed reference 
    pFun = PyDict_GetItemString(pDict, "IMP"); 
    pFB = PyDict_GetItemString(pDict, "framebuffer"); 
    if (pFB != NULL) 
     printf("got the framebuffer as pFB!\n"); 
     //copy a new framebuffer into pFB here!!! 
     uint8_t *ptr; 
     ptr = PyArray_DATA(pFB); 
     int col,row,color; 
     int NCOL = 0, NROW = 0, NCLR = 0; 
     int ndim=0; 

     ndim = PyArray_NDIM(pFB); 
     NROW = PyArray_DIM(pFB,0); 
     NCOL = PyArray_DIM(pFB,1); 
     NCLR = PyArray_DIM(pFB,2); 

     for (row=0;row<NROW;row++) 
      for (col=0;col<NCOL;col++) 
      for (color=0;color<NCLR;color++) 
      { 
       *ptr = pixel_value; //copy your framebuffer pixel value to pFB!!! 
       ptr++; 
      } 


    pArgs = PyTuple_New(1); 
    PyTuple_SetItem(pArgs,0,pFB); 

    if (PyCallable_Check(pFun)) 
    { 
     PyObject_CallObject(pFun, pArgs); //call python IMP with updated framebuffer!!! 
    } 
    else 
    { 
     PyErr_Print(); 
    } 

    // Clean up 
    Py_DECREF(pModule); 
    Py_DECREF(pName); 

    //Py_DECREF(pDict); //do not decref on pDict because it is borrowed ref, otherwise it will crash Py_Finalize()!!! 
    //Py_DECREF(pArgs); 
    //Py_DECREF(pFun); 
    //Py_DECREF(pFB); 

    // Finish the Python Interpreter 
    Py_Finalize(); 

    return 0; 
} 

就是这样!