opencv 3.0 beta 创建新模块(可在 Python 中使用)

opencv 3.0 beta create new module (usable in Python)

我已经在本地向 OpenCV 3.0 beta(在本地 contrib 存储库中)添加了一个模块,该模块在 C++ 中运行良好。现在我试图让它在 Python 中工作,但没有成功。

按照建议 here I replaced my CV_EXPORTS by CV_EXPORTS_W and added the macro CV_WRAP to the functions in my classes I want to be able to call in Python (as explained here)。

我还通过查看源代码发现我必须在我的模块的 CMakeLists.txt 中添加一些 WRAP python,看起来像这样:

set(the_description "Extended video processing module. Includes an object tracker")
set(OPENCV_MODULE_IS_PART_OF_WORLD OFF)
ocv_define_module(xvideo opencv_core opencv_imgproc opencv_highgui WRAP python)

target_link_libraries(opencv_xvideo)

但看起来我仍然遗漏了一些东西,因为我的 类 和方法在 Python 中不存在。例如下面的代码:

import numpy as np
import cv2
frameManager = cv2.FrameManager.create()

产生以下错误:

AttributeError: 'module' object has no attribute 'FrameManager'

作为补充信息,我发现我的 类 出现在生成的文件中(在编译时)build/modules/python2/pyopencv_generated_include.h :

...
#include "opencv2/xvideo/framemanager.hpp"
...

但不会出现在其他 pyopencv_generated_*.h 文件中。

你有关于我在这里遗漏的信息吗?或者您知道如何使用新模块扩展 OpenCV 3.0 的指南吗? (没找到)

好的,看来我必须在这里尝试自己的 dogfood..

xvideo/include/opencv2/xvideo.hpp:

#ifndef __OPENCV_xvideo_HPP__
#define __OPENCV_xvideo_HPP__

/** @defgroup xvideo Additional video processing algorithms
*/

#include "opencv2/core.hpp"

namespace cv {
namespace xvideo {

    struct CV_EXPORTS_W FrameManager
    {
        CV_WRAP void foo(InputArray inp, float v);

        CV_WRAP static Ptr<FrameManager> create();
    };
}
}

xvideo/src/framemanager.cpp:

#include "opencv2/xvideo.hpp"

namespace cv
{
namespace xvideo
{


    void FrameManager::foo(InputArray inp, float v) {}

    Ptr<FrameManager> FrameManager::create() { return makePtr<FrameManager>(); }

}
}

相同 CmakeLists.txt,您正在使用。

现在 运行 cmake(-gui),看,BUILD_opencv_xvideo=ON,配置,生成。

然后重建,不要忘记运行make install(或在 VS 中构建 INSTALL 项目)

>>> help(cv2.xvideo)
Help on module cv2.xvideo in cv2:

NAME
    cv2.xvideo

FILE
    (built-in)

FUNCTIONS
    FrameManager_create(...)
        FrameManager_create() -> retval


>>> fm = cv2.xvideo.FrameManager_create()
>>> help(fm)
Help on xvideo_FrameManager object:

class xvideo_FrameManager(__builtin__.object)
 |  Methods defined here:
 |
 |  __repr__(...)
 |      x.__repr__() <==> repr(x)
 |
 |  foo(...)
 |      foo(inp, v) -> None
 |
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T

从之前的答案开始(感谢@berak)我将该代码一点一点地转换为我的代码。据我所知,我至少可以说两件事:

不要使用对抽象纯的引用class :

myFunction(abstractType &myObject) 转换为 myFunction(Ptr<abstractType> myObject) 其中 Ptr 是 OpenCV 智能指针。

不要忘记帮助 OpenCV 获取通过引用传递的输出函数参数:

例如:myFunction(CV_OUT SomeType &output)。 PS :还有 CV_IN_OUT 关键字。有关详细信息,请参阅 here

我仍然有嵌套命名空间的问题:

我使用了一些 std::string 类型的函数参数。似乎在编译步骤中,为绑定生成的文件 (pyopencv_generated_*.h) 不正确(他们使用 string 而不是 std::string)在创建文件 cv2.so 时产生错误:error: ‘string’ was not declared in this scope。我通过使用 class String 而不是 std::string 来绕过该错误,这似乎是 OpenCV 的一部分。

但由于我没有真正解决问题,现在我有同样的错误,但有一些 std::vector<...> 争论。不幸的是,我不能给项目的 bitbucket (private) 一个 link 但我做了一个简单的示例代码,如果有人知道出了什么问题,就会面临同样的错误。有示例代码:

xxxvideo/include/opencv2/xxxvideo.hpp :

#ifndef __OPENCV_XXXVIDEO_HPP__
#define __OPENCV_XXXVIDEO_HPP__

/** @defgroup xxxvideo Additional video processing algorithms
*/

#include "opencv2/core.hpp"
#include <string>
#include <vector>
//#include "xxxvideo/framemanager_all.hpp"

namespace cv {
namespace xxxvideo {

class CV_EXPORTS_W DynamicState : public Algorithm
{
    public:
        virtual ~DynamicState(){};
        CV_WRAP virtual Mat toMatrix();
};

class CV_EXPORTS_W DynamicModel : public Algorithm
{
    public:
        virtual ~DynamicModel(){};
        CV_WRAP virtual std::vector< Ptr<DynamicState> > getAllStates();
};

}}

#endif

xxxvideo/src/dynamicmodelimpl.cpp :

#include "opencv2/xxxvideo.hpp"

using namespace std;
using namespace cv;
using namespace cv::xxxvideo;

vector< Ptr<DynamicState> > DynamicModel::getAllStates()
{
    vector< Ptr<DynamicState> > states;
    return states;
}

Mat DynamicState::toMatrix()
{
    Mat m;
    return m;
}

xxxvideo/CMakeLists.txt :

set(the_description "Exxxtended video processing module. Includes an object tracker")
set(OPENCV_MODULE_IS_PART_OF_WORLD OFF)
ocv_define_module(xxxvideo opencv_core opencv_imgproc opencv_highgui WRAP python)

target_link_libraries(opencv_xxxvideo)

我得到的错误是:

[ 98%] Generating pyopencv_generated_include.h, pyopencv_generated_funcs.h, pyopencv_generated_types.h, pyopencv_generated_type_reg.h, pyopencv_generated_ns_reg.h
Scanning dependencies of target opencv_python2
[ 98%] Building CXX object modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o
In file included from /home/matthieu/libs/opencv/opencv-trunk/modules/python/src2/cv2.cpp:1217:0:
/home/matthieu/libs/opencv/opencv-trunk/build/modules/python2/pyopencv_generated_types.h: In function ‘PyObject* pyopencv_cv_xxxvideo_xxxvideo_DynamicModel_getAllStates(PyObject*, PyObject*, PyObject*)’:
/home/matthieu/libs/opencv/opencv-trunk/build/modules/python2/pyopencv_generated_types.h:15927:5: error: ‘vector_Ptr_DynamicState’ was not declared in this scope
     vector_Ptr_DynamicState retval;
     ^

如果您对错误有任何想法,欢迎提供帮助;)

################## 编辑:##################

所以我研究了 Python 绑定是如何生成的(可以找到解释的开头 here)。相关文件在文件夹 modules/python/src2 中。我发现了 2 件事可能与我的问题有关。

首先,OpenCV 中使用的所有 vector<...> 类型似乎都在文件 cv2.cpp 的第 87 行和第 110 行之间定义:

typedef std::vector<uchar> vector_uchar;
typedef std::vector<char> vector_char;
typedef std::vector<int> vector_int;
typedef std::vector<float> vector_float;
typedef std::vector<double> vector_double;
typedef std::vector<Point> vector_Point;
typedef std::vector<Point2f> vector_Point2f;
typedef std::vector<Vec2f> vector_Vec2f;
typedef std::vector<Vec3f> vector_Vec3f;
typedef std::vector<Vec4f> vector_Vec4f;
typedef std::vector<Vec6f> vector_Vec6f;
typedef std::vector<Vec4i> vector_Vec4i;
typedef std::vector<Rect> vector_Rect;
typedef std::vector<KeyPoint> vector_KeyPoint;
typedef std::vector<Mat> vector_Mat;
typedef std::vector<DMatch> vector_DMatch;
typedef std::vector<String> vector_String;
typedef std::vector<Scalar> vector_Scalar;

typedef std::vector<std::vector<char> > vector_vector_char;
typedef std::vector<std::vector<Point> > vector_vector_Point;
typedef std::vector<std::vector<Point2f> > vector_vector_Point2f;
typedef std::vector<std::vector<Point3f> > vector_vector_Point3f;
typedef std::vector<std::vector<DMatch> > vector_vector_DMatch;

其次,std:: 命名空间从第 204 行函数 def parse_arg(self, arg_str, argno): 的文件 hdr_parser.py 中的参数类型中删除:

arg_type = self.batch_replace(arg_type, [("std::", ""), ("cv::", ""), ("::", "_")])

我的部分解决方案:

调查这个问题,在我的错误中,我想出了在 xxxvideo.hpp 文件中添加一个 typedef 的想法,就在 class DynamicState 的定义之后:

typedef std::vector< cv::Ptr<DynamicState> >  vector_Ptr_DynamicState;

编译错误消失了,但是现在,当我在 python 中 import cv2 时出现错误:

>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /usr/local/opencv/opencv-trunk/lib/python2.7/dist-packages/cv2.so: undefined symbol: _ZTIN2cv8xxxvideo12DynamicStateE

再次强调,如果有人知道我应该做什么,或者您可以将我的问题转达给可能的人,我将不胜感激。

################## 编辑(再次):################# #

实际上我的错误在另一轮 cmake ..make、、sudo make install 后消失了。这似乎更正了错误。