Use opencv stitcher from python

OpenCV can be used with pythonbindings, and it works quite well. However, I was wondering (really hopefully) whether the OpenCv stitcher can be used in python. I tried a few things but couldn't get it to work. If at all possible, I probably need to do some additional importing, but I can't figure it out, and Google is not giving me an answer. Hope among you there will be an opencv-python guru who can help me.

+5
source share
1 answer

Okay, so finally I get it. While I was just porting the line method with two arguments, ask me if you have problems exposing everything you might need.

(-fPIC option gcc) , opencv_core opencv_stitching. include python, , Python.h.

, , python.

, , . , (). , , .Stitcher(), ( , , .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 , . , , .

opencv_to opencv_from cv2.cpp - pythonPortAux.h. , . MKTYPE2 .

pythonPort.cpp ( , , Stitcher):

#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);
    }
}
+1

All Articles