How to look at the imported OpenCV code? (specifically python cv2)

A friend and I use OpenCV for some image processing jobs and wanted to open the "black box" of the findContours method in the python library, since the documentation does not provide much more than the definition / parameters of a function . We read about mathematics for finding contours, but are interested in looking at the specific OpenCV code written for this task.

What we tried:

We looked at the opencv github repository , but it looks like the only function / methods that we have access to are in C ++, not sure how opencv makes its python shells.

We also tried to import cv2 into the python shell and print the location of the source code, but did not know where to start with the .so file, and the other contents in this directory did not help ...

>>> import cv2
>>> print cv2
<module 'cv2' from '/usr/local/lib/python2.7/dist-packages/cv2.so'>

Any links to other answers (is it really a question about C ++ and python shells, or is there an easier way to print the findContours code in the cv2 module or ...?) Or advice on what to do next would be greatly appreciated. Thank!

+4
source share
1 answer
>>> import cv2
>>> help(cv2.findContours)
Help on built-in function findContours in module cv2:

findContours(...)
    findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy

as you may have guessed, C ++ β†’ python wrapper code is generated from C ++ headers, look at modules / python / src / gen2.py.

generated code, for example. for findContours looks like this (pyopencv_generated_funcs.h):

static PyObject* pyopencv_findContours(PyObject* , PyObject* args, PyObject* kw)
{
    PyObject* pyobj_image = NULL;
    Mat image;
    PyObject* pyobj_contours = NULL;
    vector_Mat contours;
    PyObject* pyobj_hierarchy = NULL;
    Mat hierarchy;
    int mode=0;
    int method=0;
    PyObject* pyobj_offset = NULL;
    Point offset;

    const char* keywords[] = { "image", "mode", "method", "contours", "hierarchy", "offset", NULL };
    if( PyArg_ParseTupleAndKeywords(args, kw, "Oii|OOO:findContours", (char**)keywords, &pyobj_image, &mode, &method, &pyobj_contours, &pyobj_hierarchy, &pyobj_offset) &&
        pyopencv_to(pyobj_image, image, ArgInfo("image", 1)) &&
        pyopencv_to(pyobj_contours, contours, ArgInfo("contours", 1)) &&
        pyopencv_to(pyobj_hierarchy, hierarchy, ArgInfo("hierarchy", 1)) &&
        pyopencv_to(pyobj_offset, offset, ArgInfo("offset", 0)) )
    {
        ERRWRAP2( cv::findContours(image, contours, hierarchy, mode, method, offset));
        return Py_BuildValue("(NN)", pyopencv_from(contours), pyopencv_from(hierarchy));
    }

    return NULL;
}
+2
source

Source: https://habr.com/ru/post/1544374/


All Articles