2014-03-29 170 views
4

我正在嘗試創建Python/Cython封裝的C++庫,使用OpenCV中的cv::Mat類。在官方的Python包裝中,所有功能都採用NumPy的ndarray而不是cv::Mat,這非常方便。但在我自己的包裝中,我該如何做這種轉換?也就是說,我如何創建cv::Matnp.ndarray將ndarray轉換爲cv :: Mat的最簡單方法是什麼?

回答

3

正如kyamagu所建議的那樣,您可以使用OpenCV的官方python包裝代碼,特別是pyopencv_topyopencv_from

我一直在努力,因爲你對所有的依賴和生成的頭文件做了。儘管如此,通過將cv2.cpp「清理」爲lightalchemist did here以便僅保留必要的內容,可以降低其複雜性。您需要根據您的需要和您使用的OpenCV版本進行調整,但其基本上與我使用的代碼基本相同。

#include <Python.h> 
#include "numpy/ndarrayobject.h" 
#include "opencv2/core/core.hpp" 

static PyObject* opencv_error = 0; 

static int failmsg(const char *fmt, ...) 
{ 
    char str[1000]; 

    va_list ap; 
    va_start(ap, fmt); 
    vsnprintf(str, sizeof(str), fmt, ap); 
    va_end(ap); 

    PyErr_SetString(PyExc_TypeError, str); 
    return 0; 
} 

class PyAllowThreads 
{ 
public: 
    PyAllowThreads() : _state(PyEval_SaveThread()) {} 
    ~PyAllowThreads() 
    { 
     PyEval_RestoreThread(_state); 
    } 
private: 
    PyThreadState* _state; 
}; 

class PyEnsureGIL 
{ 
public: 
    PyEnsureGIL() : _state(PyGILState_Ensure()) {} 
    ~PyEnsureGIL() 
    { 
     PyGILState_Release(_state); 
    } 
private: 
    PyGILState_STATE _state; 
}; 

#define ERRWRAP2(expr) \ 
try \ 
{ \ 
    PyAllowThreads allowThreads; \ 
    expr; \ 
} \ 
catch (const cv::Exception &e) \ 
{ \ 
    PyErr_SetString(opencv_error, e.what()); \ 
    return 0; \ 
} 

using namespace cv; 

static PyObject* failmsgp(const char *fmt, ...) 
{ 
    char str[1000]; 

    va_list ap; 
    va_start(ap, fmt); 
    vsnprintf(str, sizeof(str), fmt, ap); 
    va_end(ap); 

    PyErr_SetString(PyExc_TypeError, str); 
    return 0; 
} 

static size_t REFCOUNT_OFFSET = (size_t)&(((PyObject*)0)->ob_refcnt) + 
    (0x12345678 != *(const size_t*)"\x78\x56\x34\x12\0\0\0\0\0")*sizeof(int); 

static inline PyObject* pyObjectFromRefcount(const int* refcount) 
{ 
    return (PyObject*)((size_t)refcount - REFCOUNT_OFFSET); 
} 

static inline int* refcountFromPyObject(const PyObject* obj) 
{ 
    return (int*)((size_t)obj + REFCOUNT_OFFSET); 
} 

class NumpyAllocator : public MatAllocator 
{ 
public: 
    NumpyAllocator() {} 
    ~NumpyAllocator() {} 

    void allocate(int dims, const int* sizes, int type, int*& refcount, 
        uchar*& datastart, uchar*& data, size_t* step) 
    { 
     PyEnsureGIL gil; 

     int depth = CV_MAT_DEPTH(type); 
     int cn = CV_MAT_CN(type); 
     const int f = (int)(sizeof(size_t)/8); 
     int typenum = depth == CV_8U ? NPY_UBYTE : depth == CV_8S ? NPY_BYTE : 
         depth == CV_16U ? NPY_USHORT : depth == CV_16S ? NPY_SHORT : 
         depth == CV_32S ? NPY_INT : depth == CV_32F ? NPY_FLOAT : 
         depth == CV_64F ? NPY_DOUBLE : f*NPY_ULONGLONG + (f^1)*NPY_UINT; 
     int i; 
     npy_intp _sizes[CV_MAX_DIM+1]; 
     for(i = 0; i < dims; i++) 
      _sizes[i] = sizes[i]; 
     if(cn > 1) 
     { 
      /*if(_sizes[dims-1] == 1) 
       _sizes[dims-1] = cn; 
      else*/ 
       _sizes[dims++] = cn; 
     } 
     PyObject* o = PyArray_SimpleNew(dims, _sizes, typenum); 
     if(!o) 
      CV_Error_(CV_StsError, ("The numpy array of typenum=%d, ndims=%d can not be created", typenum, dims)); 
     refcount = refcountFromPyObject(o); 
     npy_intp* _strides = PyArray_STRIDES(o); 
     for(i = 0; i < dims - (cn > 1); i++) 
      step[i] = (size_t)_strides[i]; 
     datastart = data = (uchar*)PyArray_DATA(o); 
    } 

    void deallocate(int* refcount, uchar*, uchar*) 
    { 
     PyEnsureGIL gil; 
     if(!refcount) 
      return; 
     PyObject* o = pyObjectFromRefcount(refcount); 
     Py_INCREF(o); 
     Py_DECREF(o); 
    } 
}; 

NumpyAllocator g_numpyAllocator; 

enum { ARG_NONE = 0, ARG_MAT = 1, ARG_SCALAR = 2 }; 

static int pyopencv_to(const PyObject* o, Mat& m, const char* name = "<unknown>", bool allowND=true) 
{ 
    if(!o || o == Py_None) 
    { 
     if(!m.data) 
      m.allocator = &g_numpyAllocator; 
     return true; 
    } 

    if(PyInt_Check(o)) 
    { 
     double v[] = {PyInt_AsLong((PyObject*)o), 0., 0., 0.}; 
     m = Mat(4, 1, CV_64F, v).clone(); 
     return true; 
    } 
    if(PyFloat_Check(o)) 
    { 
     double v[] = {PyFloat_AsDouble((PyObject*)o), 0., 0., 0.}; 
     m = Mat(4, 1, CV_64F, v).clone(); 
     return true; 
    } 
    if(PyTuple_Check(o)) 
    { 
     int i, sz = (int)PyTuple_Size((PyObject*)o); 
     m = Mat(sz, 1, CV_64F); 
     for(i = 0; i < sz; i++) 
     { 
      PyObject* oi = PyTuple_GET_ITEM(o, i); 
      if(PyInt_Check(oi)) 
       m.at<double>(i) = (double)PyInt_AsLong(oi); 
      else if(PyFloat_Check(oi)) 
       m.at<double>(i) = (double)PyFloat_AsDouble(oi); 
      else 
      { 
       failmsg("%s is not a numerical tuple", name); 
       m.release(); 
       return false; 
      } 
     } 
     return true; 
    } 

    if(!PyArray_Check(o)) 
    { 
     failmsg("%s is not a numpy array, neither a scalar", name); 
     return false; 
    } 

    bool needcopy = false, needcast = false; 
    int typenum = PyArray_TYPE(o), new_typenum = typenum; 
    int type = typenum == NPY_UBYTE ? CV_8U : 
       typenum == NPY_BYTE ? CV_8S : 
       typenum == NPY_USHORT ? CV_16U : 
       typenum == NPY_SHORT ? CV_16S : 
       typenum == NPY_INT ? CV_32S : 
       typenum == NPY_INT32 ? CV_32S : 
       typenum == NPY_FLOAT ? CV_32F : 
       typenum == NPY_DOUBLE ? CV_64F : -1; 

    if(type < 0) 
    { 
     if(typenum == NPY_INT64 || typenum == NPY_UINT64 || type == NPY_LONG) 
     { 
      needcopy = needcast = true; 
      new_typenum = NPY_INT; 
      type = CV_32S; 
     } 
     else 
     { 
      failmsg("%s data type = %d is not supported", name, typenum); 
      return false; 
     } 
    } 

    int ndims = PyArray_NDIM(o); 
    if(ndims >= CV_MAX_DIM) 
    { 
     failmsg("%s dimensionality (=%d) is too high", name, ndims); 
     return false; 
    } 

    int size[CV_MAX_DIM+1]; 
    size_t step[CV_MAX_DIM+1], elemsize = CV_ELEM_SIZE1(type); 
    const npy_intp* _sizes = PyArray_DIMS(o); 
    const npy_intp* _strides = PyArray_STRIDES(o); 
    bool ismultichannel = ndims == 3 && _sizes[2] <= CV_CN_MAX; 

    for(int i = ndims-1; i >= 0 && !needcopy; i--) 
    { 
     // these checks handle cases of 
     // a) multi-dimensional (ndims > 2) arrays, as well as simpler 1- and 2-dimensional cases 
     // b) transposed arrays, where _strides[] elements go in non-descending order 
     // c) flipped arrays, where some of _strides[] elements are negative 
     if((i == ndims-1 && (size_t)_strides[i] != elemsize) || 
      (i < ndims-1 && _strides[i] < _strides[i+1])) 
      needcopy = true; 
    } 

    if(ismultichannel && _strides[1] != (npy_intp)elemsize*_sizes[2]) 
     needcopy = true; 

    if (needcopy) 
    { 
     if(needcast) 
      o = (PyObject*)PyArray_Cast((PyArrayObject*)o, new_typenum); 
     else 
      o = (PyObject*)PyArray_GETCONTIGUOUS((PyArrayObject*)o); 
     _strides = PyArray_STRIDES(o); 
    } 

    for(int i = 0; i < ndims; i++) 
    { 
     size[i] = (int)_sizes[i]; 
     step[i] = (size_t)_strides[i]; 
    } 

    // handle degenerate case 
    if(ndims == 0) { 
     size[ndims] = 1; 
     step[ndims] = elemsize; 
     ndims++; 
    } 

    if(ismultichannel) 
    { 
     ndims--; 
     type |= CV_MAKETYPE(0, size[2]); 
    } 

    if(ndims > 2 && !allowND) 
    { 
     failmsg("%s has more than 2 dimensions", name); 
     return false; 
    } 

    m = Mat(ndims, size, type, PyArray_DATA(o), step); 

    if(m.data) 
    { 
     m.refcount = refcountFromPyObject(o); 
     if (!needcopy) 
     { 
      m.addref(); // protect the original numpy array from deallocation 
         // (since Mat destructor will decrement the reference counter) 
     } 
    }; 
    m.allocator = &g_numpyAllocator; 

    return true; 
} 

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

一旦你有一個清理cv2.cpp文件,這裏是一些用Cython代碼,需要轉換的照顧。注意定義和調用import_array()功能(它在某處cv2.cpp包含的頭定義的NumPy的功能),這是必要的定義由pyopencv_to使用一些宏,如果你不把它你會得到分段錯誤的lightalchemist pointed out

from cpython.ref cimport PyObject 

# Declares OpenCV's cv::Mat class 
cdef extern from "opencv2/core/core.hpp": 
    cdef cppclass Mat: 
     pass 

# Declares the official wrapper conversion functions + NumPy's import_array() function 
cdef extern from "cv2.cpp": 
    void import_array() 
    PyObject* pyopencv_from(const _Mat&) 
    int pyopencv_to(PyObject*, _Mat&) 


# Function to be called at initialization 
cdef void init(): 
    import_array() 

# Python to C++ conversion 
cdef Mat nparrayToMat(object array): 
    cdef Mat mat 
    cdef PyObject* pyobject = <PyObject*> array 
    pyopencv_to(pyobject, mat) 
    return <Mat> mat 

# C++ to Python conversion 
cdef object matToNparray(Mat mat): 
    return <object> pyopencv_from(mat) 

注:不知何故,我與NumPy的1.8.0在Fedora 20得到一個錯誤,而在import_array宏編譯由於陌生return語句,我不得不手動刪除它,使其工作,但我不能發現在與NumPy的1.8.0 GitHub的源代碼

+0

我現在無法對其進行測試,但它看起來像是我用過的更好的方法,所以我無需驗證就接受它。 – ffriend

+0

看着https://github.com/numpy/numpy/blob/c90d7c94fd2077d0beca48fa89a423da2b0bb663/numpy/core/code_generators/generate_numpy_api.py如果使用Python3,宏返回NULL值。 您可以修改初始化函數使用Python 3 您可以檢查我的這一個Python3/OpenCV3兼容版本的答案時,返回一個空指針,而不是什麼都沒有。 –

2

我想你可以直接使用或從the converter from the official python wrapper採取一些邏輯。這個模塊沒有太多的文檔,但是可能包裝生成器的輸出有助於理解如何使用它。

+0

謝謝您的回答和對不起已故的答覆。我花了好幾天的時間嘗試整合這種轉換器,但不幸的是它與其他文件密切相關,而這些文件依賴於整個OpenCV基礎架構,包括項目佈局,生成的文件等等。我會嘗試一些更多的方法,但如果你知道替代轉換器,我會很高興看到他們。謝謝。 – ffriend

2

事實證明,沒有簡單的方法將(任何)np.ndarray轉換爲相應的cv::Mat。基本上,只需要做兩件事:

  1. 創建相應大小和類型的空cv::Mat
  2. 複製數據。

但是,魔鬼隱藏在細節中。 ndarrayMat都可以保存相當不同的數據格式。例如,NumPy數組中的數據可能是C語言或Fortran語句的順序,數組對象可能擁有其數據或保留對另一個數組的視圖,通道可能按不同的順序排列(OpenCV中的NumPy與BGR中的RGB)等。

因此,不是試圖解決通用問題,而是決定留下符合我需求的簡單代碼,並且可能會被任何感興趣的人輕鬆修改。

繼用Cython代碼與float32/CV_32FC1圖像默認的字節順序:

cdef void array2mat(np.ndarray arr, Mat& mat): 
    cdef int r = arr.shape[0] 
    cdef int c = arr.shape[1] 
    cdef int mat_type = CV_32FC1   # or CV_64FC1, or CV_8UC3, or whatever 
    mat.create(r, c, mat_type) 
    cdef unsigned int px_size = 4   # 8 for single-channel double image or 
              # 1*3 for three-channel uint8 image 
    memcpy(mat.data, arr.data, r*c*px_size) 

要在用Cython一個使用此代碼也需要聲明一些類型和常量,例如像這樣:

import numpy as np 
# Cython makes it simple to import NumPy 
cimport numpy as np 


# OpenCV's matrix class 
cdef extern from "opencv2/opencv.hpp" namespace "cv": 

    cdef cppclass Mat: 
     Mat() except + 
     Mat(int, int, int, void*) except + 
    void create(int, int, int) 
     void* data 
     int type() const 
     int cols 
     int rows 
     int channels() 
     Mat clone() const 

# some OpenCV matrix types 
cdef extern from "opencv2/opencv.hpp":   
    cdef int CV_8UC3 
    cdef int CV_8UC1 
    cdef int CV_32FC1 
    cdef int CV_64FC1 

相反的轉換(從cv::Matnp.ndarray)可以以類似的方式來實現。

獎勵:還有很好的blog post描述RGB/BGR圖像的相同種類的轉換。

1

這個return語句,如果有幫助,我寫了一個包裝,正是這樣做的。這是一個方便的庫,它註冊了一個boost :: python轉換器,以便在OpenCV的流行的cv :: Mat數據類型和NumPy流行的np.array()數據類型之間進行隱式轉換。這使得開發人員可以相對容易地在OpenCV C++ API和使用NumPy編寫的Python API之間來回切換,避免了編寫額外的處理PyObjects的包裝器的需求。

請看: https://github.com/spillai/numpy-opencv-converter

相關問題