2014-11-24 81 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; 
} 

就是這樣!