Starting from the previous answer (thanks @berak) I transformed that code to little by little get to my very code. As far as I got I can say at least 2 things :
Do not use a reference to an abstract pure class :
Transform myFunction(abstractType &myObject) to myFunction(Ptr<abstractType> myObject) where Ptr are OpenCV smart pointers.
Do not forget to help OpenCV for output function arguments passed by reference :
For example : myFunction(CV_OUT SomeType &output). PS : There is also the CV_IN_OUT keyword. See here for more details.
I still have problems with nested namespaces :
I was using some argument of function of the type std::string. It appears that at compilation step, the generated files (pyopencv_generated_*.h) for bindings are not correct (they used string instead of std::string) producing an error when creating the file cv2.so : error: ‘string’ was not declared in this scope. I bypassed that error by using the class String instead of std::string which seems to be part of OpenCV.
But since I did not really solve the problem, now I have the same error but with some std::vector<...> argument. Unfortunately, I can not give a link to the bitbucket of the project (private) but I made a simple sample code facing the same error if someone knows what is wrong. There is the sample code :
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)
And there is the error I get :
[ 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;
^
If you have any idea on what is wrong, help is welcomed ;)
################## EDIT : ##################
So I went looking into how Python bindings are generated (starting of an explanation can be found here). The relevant files are in the folder modules/python/src2. I found 2 things that might be related to my problem.
First, all vector<...> types used in OpenCV seems to be defined in file cv2.cpp between lines 87 and 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;
Second, the std:: namespace is removed from the argument types in file hdr_parser.py in function def parse_arg(self, arg_str, argno): on line 204 :
arg_type = self.batch_replace(arg_type, [("std::", ""), ("cv::", ""), ("::", "_")])
My partial solution :
Looking into this and in my error I came up with the idea of adding one typedef in the file xxxvideo.hpp just after definition of class DynamicState :
typedef std::vector< cv::Ptr<DynamicState> > vector_Ptr_DynamicState;
Compilation errors are gone, but now, I have an error when I import cv2 in python :
>>> 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
Again, if someone has any idea on what I should do, or if you could relay my question to someone who might, I would be grateful.
################## EDIT (Again): ##################
Actually my error vanished after another round of cmake .., make, ,sudo make install. This seems to have corrected the error.
frameManager = cv2.FrameManager_create()?FrameManager_createinstead ofFrameManagerAttributeError: 'module' object has no attribute 'xvideo'. It seems that already existing modules have something more I don't, I can't figure out what :(