2012-08-07 124 views
5

的OpenCV可以与pythonbindings使用,它工作得很好。但是我想知道(希望真的)是否有可能在python中使用OpenCv's stitcher。我已经尝试了几件事,但无法让它起作用。如果可能的话,我可能需要做额外的导入,但我无法弄清楚,谷歌也没有给我答案。希望有一位能够帮助我的opencv-python专家。使用OpenCV的拼接蟒蛇

+0

嘿,我知道这可能有点晚了你,但是我现在想要在python中使用opencv Stitcher。我已经想出了如何从opencv python API中“丢失”其他opencv类,并且我之前完成了它。如果我最终将拼接器指向python,我会在今天结束。 – 2012-12-20 15:57:34

回答

1

好了,所以,我想通了最后。到目前为止,我只用两个参数移植了针法,问我是否无法展示任何可能需要的东西。

构建它的最简单方法是通过以位置无关的方式(gcc的-fPIC选项)编译所有内容到动态库中,同时链接opencv_core和opencv_stitching库。你还必须添加包含你正在构建的任何版本的Python的include目录,以便它可以找到正确的Python.h头文件。

如果你建立正确的话,你就可以使用编译库相同的方式,你可以使用一个Python模块。

不幸的是,因为它们不提供访问的构造,我已经满足于做的事情的全局实例。如果还有另一种优雅的方式,我就是耳朵(眼睛)。 这意味着无论何时调用.Stitcher()构造函数,它都会返回相同的实例(在构建期间有一个单独的实例试图使用GPU,对此使用.Stitcher(True))。

这是我的pythonPort.h文件:

/* 
* File: pythonPort.h 
* Author: algomorph 
* 
* Created on December 5, 2012, 10:18 AM 
*/ 

#ifndef PYTHONPORT_H 
#define PYTHONPORT_H 

#define MODULESTR "mycv" 


#include "Python.h" 
#include "numpy/ndarrayobject.h" 
#include <opencv2/core/core.hpp> 
#include <opencv2/stitching/stitcher.hpp> 
/* 
//include your own custom extensions here 
#include "savgol.h" 
#include "filters.hpp" 
*/ 
#include "pythonPortAux.h" 

#endif 

MODULESTR应该是你希望把你的模块什么的。我保持它与它编译的库的名称相同。

你必须复制你从cv2.cpp文件所需要的任何opencv_to和opencv_from程序并投入像我pythonPortAux.h。我有很多例程,你可以找到它at this link。 MKTYPE2宏也在那里。

剩下的就是在这里下pythonPort.cpp文件中(我有其他的东西存在,这只是缝合器相关的部分):

#include "pythonPort.h" 

struct pycvex_Stitcher_t 
{ 
    PyObject_HEAD 
    Ptr<cv::Stitcher> v; 
}; 

static PyTypeObject pycvex_Stitcher_Type = 
{ 
    PyObject_HEAD_INIT(&PyType_Type) 
    0, 
    MODULESTR".Stitcher", 
    sizeof(pycvex_Stitcher_t), 
}; 

static void pycvex_Stitcher_dealloc(PyObject* self) 
{ 
    //((pycvex_Stitcher_t*)self)->v.release(); 
    PyObject_Del(self); 
} 

static PyObject* pyopencv_from(const Ptr<cv::Stitcher>& r) 
{ 
    pycvex_Stitcher_t *m = PyObject_NEW(pycvex_Stitcher_t, &pycvex_Stitcher_Type); 
    new (&(m->v)) Ptr<cv::Stitcher>(); // init Ptr with placement new 
    m->v = r; 
    return (PyObject*)m; 
} 

static bool pyopencv_to(PyObject* src, Ptr<cv::Stitcher>& dst, const char* name="<unknown>") 
{ 
    if(src == NULL || src == Py_None) 
     return true; 
    if(!PyObject_TypeCheck(src, &pycvex_Stitcher_Type)) 
    { 
     failmsg("Expected cv::Stitcher for argument '%s'", name); 
     return false; 
    } 
    dst = ((pycvex_Stitcher_t*)src)->v; 
    return true; 
} 

static PyObject* pycvex_Stitcher_repr(PyObject* self) 
{ 
    char str[1000]; 
    sprintf(str, "<Stitcher %p>", self); 
    return PyString_FromString(str); 
} 

Stitcher gStitcher = cv::Stitcher::createDefault(false); 
Stitcher gStitcherGPU = cv::Stitcher::createDefault(true); 

static PyObject* pycvex_Stitcher_Stitcher(PyObject* , PyObject* args, PyObject* kw) 
{ 
    PyErr_Clear(); 
    { 
     pycvex_Stitcher_t* self = 0; 
     bool try_use_gpu = false; 

     const char* keywords[] = { "img", "pt1", "pt2","connectivity","leftToRight", NULL }; 
     if (PyArg_ParseTupleAndKeywords(args, kw, "|b:Stitcher", 
       (char**) keywords, &try_use_gpu)){ 
      self = PyObject_NEW(pycvex_Stitcher_t, &pycvex_Stitcher_Type); 
      if (self) 
       ERRWRAP2(
         if(try_use_gpu) 
          self->v = &gStitcherGPU; 
         else 
          self->v = &gStitcher; 
         ); 
      return (PyObject*) self; 
     } 
    } 
    return NULL; 
} 
static PyGetSetDef pycvex_Stitcher_getseters[] = 
{ 
    {NULL} /* Sentinel */ 
}; 

static PyObject* pycvex_Stitcher_stitch(PyObject* self, PyObject* args, PyObject* kw){ 
    if(!PyObject_TypeCheck(self, &pycvex_Stitcher_Type)) 
     return failmsgp("Incorrect type of self (must be 'Stitcher' or its derivative)"); 
    Stitcher* _self_ = ((pycvex_Stitcher_t*)self)->v; 
    //Stitcher::Status status; 
    int status; 

    PyObject* pyobj_images = NULL; 
    vector<Mat> images = vector<Mat>(); 
    Mat pano; 

    const char* keywords[] = { "images", NULL }; 
    if(PyArg_ParseTupleAndKeywords(args, kw, "O:Stitcher.stitch", (char**)keywords, &pyobj_images) && 
     pyopencv_to(pyobj_images, images, ArgInfo("images", false))) 
    { 
     ERRWRAP2(status = (int)_self_->stitch(images, pano)); 
     return Py_BuildValue("(NN)", pyopencv_from(status), pyopencv_from(pano)); 
    } 

    return NULL; 
} 

static PyMethodDef pycvex_Stitcher_methods[] = 
{ 
    {"stitch", (PyCFunction)pycvex_Stitcher_stitch, METH_KEYWORDS, "stitch(image) -> status, pano"}, 
    {NULL, NULL} 
}; 

static void pycvex_Stitcher_specials(void) 
{ 
    pycvex_Stitcher_Type.tp_base = NULL; 
    pycvex_Stitcher_Type.tp_dealloc = pycvex_Stitcher_dealloc; 
    pycvex_Stitcher_Type.tp_repr = pycvex_Stitcher_repr; 
    pycvex_Stitcher_Type.tp_getset = pycvex_Stitcher_getseters; 
    pycvex_Stitcher_Type.tp_methods = pycvex_Stitcher_methods; 
} 


static PyMethodDef methods[] = { 
    {"Stitcher",(PyCFunction)pycvex_Stitcher_Stitcher, METH_KEYWORDS, "Stitcher([tryUseGpu=False]) -> <Stitcher object>"}, 
    {NULL, NULL} 
}; 

extern "C"{ 
    #if defined WIN32 || defined _WIN32 
    __declspec(dllexport) 
    #endif 
    void initcvex() 
    { 
     MKTYPE2(Stitcher); 
     import_array(); 
     PyObject* m = Py_InitModule(MODULESTR, methods); 
     PyObject* d = PyModule_GetDict(m); 
     //PyDict_SetItemString(d, "__version__", PyString_FromString(CV_VERSION)) 
     opencv_error = PyErr_NewException((char*)MODULESTR".error", NULL, NULL); 
     PyDict_SetItemString(d, "error", opencv_error); 
    } 
} 
+0

看起来这可能是有用的,但我遇到了一些麻烦。 'filters.hpp'和'savgol.h'在哪里?我也收到有关'python27_d.lib'的错误。有什么建议么? – speedplane 2013-02-25 03:13:00

+0

对不起,你不需要那些(filters.hpp和savgol.h)。这些仅仅是我为opencv添加的扩展,用于Berkley边缘检测。我从上面的代码中评论了他们。 – 2013-02-25 17:16:13

+0

至于python27_d.lib-当你试图在Debug模式下编译任何引用opencv的东西时,Windows平台就会出现这个问题。对于典型的python windows安装,不包括这个库。您可以手动重新配置opencv的python绑定项目来使用python2.lib,或者尝试引用发布版本。如果你使用它真正的意图,请查阅本SO帖子:http://stackoverflow.com/questions/10315662/how-to-obtain-pre-built-debug-version-of-python-library-eg-python27 -d-dll – 2013-02-25 17:20:40