如何看待导入的OpenCV代码? (特别是python的cv2)

我和朋友正在使用OpenCV进行一些图像处理工作,并希望在
python库中打开findContours方法的“黑盒子”,因为文档没有提供比
function definition/parameters更多的东西.我们已经读过关于背后的数学查找轮廓但有兴趣查看为此任务编写的特定OpenCV代码.

我们尝试过的事情:

我们已经浏览了opencv github repository,但似乎我们可以访问的唯一函数/方法是在c中,我们不确定opencv是如何制作它的python包装器的.

我们也尝试在python shell中导入cv2并打印源代码的位置,但不知道从.so文件开始的位置,该目录中的其他内容也无济于事……

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

任何其他响应的链接(这实际上应该是关于c和python包装器的问题,还是有一种更简单的方法来打印cv2模块中的findContours代码,或者……?)或提示下一步做什么我将不胜感激.谢谢!

最佳答案

>>> import cv2
>>> help(cv2.findContours)
Help on built-in function findContours in module cv2:

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

正如你已经猜到的那样,c – > python包装代码是从c头生成的,请看modules / python / src / gen2.py.

生成的代码,例如对于findContours看起来像这样(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;
}
点赞