从上一个答案开始(感谢@berak),我将代码一点一点地转换为我的代码。据我所知,我至少可以说两件事:
不要使用对抽象纯类的引用:
将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。我通过使用 String 类而不是 std::string 来绕过该错误,这似乎是 OpenCV 的一部分。
但是由于我并没有真正解决问题,所以现在我遇到了同样的错误,但带有一些 std::vector<...> 参数。不幸的是,我无法提供项目的 bitbucket 的链接(私有),但如果有人知道出了什么问题,我制作了一个简单的示例代码,面临同样的错误。有示例代码:
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:: 命名空间从文件 hdr_parser.py 中函数 def parse_arg(self, arg_str, argno): 的第 204 行的参数类型中删除:
arg_type = self.batch_replace(arg_type, [("std::", ""), ("cv::", ""), ("::", "_")])
我的部分解决方案:
在我的错误中,我想出了在文件xxxvideo.hpp 中添加一个typedef 的想法,就在类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 之后消失了。这似乎已经纠正了错误。