I am looking to wrap a C ++ class that implements an algorithm that I wrote using Opencv 2.3. I know there are python shells for opencv in general, but I need my own code to use opencv. This seems logical because the lower level of my algorithm will be quickly compiled with C ++ code, and I will be able to call it from python and build a system around it.
My class is actually quite simple, it has 4 main methods:
void train( std::vector<cv::Mat> );
void save();
void load();
bool detect( cv::Mat );
This, in fact, is a big part of what I need to wrap. The problem is that I do not know what is the best way to do this. I looked through ctypes, swig, boost python and pyplusplus. I have not been successful with any of the above to date.
I continue to run into problems with how to wrap an opencv object with cv :: Mat. From python, I will use numpy arrays, so I know that I need the conversion code from the numpy array to cv :: Mat, and I have to register it.
I feel like someone else must have tried something like this at some point, if you can help me, it is very much appreciated
Just repeating the goal: wrap my C ++ class that uses opencv in the python library so that I can use my algorithm from python.
I think the conversion is somewhat clarified (using the opencv source), but I still will not work with python.
, , ( numpy cv:: Mat), . , , - , .
, :
foo.h:
#include <opencv2/core/core.hpp>
class Foo {
public:
Foo();
~Foo();
cv::Mat image;
void bar( cv::Mat in );
};
foo.cpp:
#include "foo.h"
Foo::Foo(){}
Foo::~Foo(){}
void Foo::bar( cv::Mat in) {
image = in;
cv::Canny( image, image, 50, 100 );
cv::imwrite("image.png", image);
}
, boost:: python :
wrap_foo.cpp
#include <boost/python.hpp>
#include <numpy/arrayobject.h>
#include <opencv2/core/core.hpp>
#include "foo.h"
using namespace cv;
namespace bp = boost::python;
void bar(Foo& f, bp::object np);
cv::Mat convertNumpy2Mat(bp::object np);
void bar(Foo& f, bp::object np)
{
Mat img = convertNumpy2Mat(np);
f.bar(img);
return;
}
BOOST_PYTHON_MODULE(lib)
{
bp::class_<Foo>("Foo")
.def("bar", bar)
;
}
cv::Mat convertNumpy2Mat(bp::object np)
{
Mat m;
numpy_to_mat(np.ptr(),m);
return m;
}
numpy_to_mat pano_cv, , . , . bjam , python, . : libFoo.so: undefined symbol: _ZN2cv3Mat10deallocateEv. , .
.