I am looking to wrap a c++ class which implements an algorithm I wrote using Opencv 2.3. I am aware that there is python wrappers for opencv as a whole but what I need is to wrap my own code which uses opencv. This seems logical because the lower level of my algorithm will be fast compiled c++ code and I will be free to call it from python and build a system around it.
我正在寻找一个c++类,它实现了我使用Opencv 2.3编写的算法。我知道作为一个整体,opencv有python包装器,但是我需要的是包装我自己的使用opencv的代码。这似乎是合乎逻辑的,因为我的算法的底层将是快速编译的c++代码,我可以从python中调用它,并围绕它构建一个系统。
My class is actually quite simple, it has 4 main methods:
我的类其实很简单,它有4种主要的方法:
void train( std::vector<cv::Mat> );
void save();
void load();
bool detect( cv::Mat );
This is essentially the majority of what I need to wrap. The problem is that I don't know how to best go about this. I have looked into ctypes, swig, boost python and pyplusplus. I have not been successful with any of the above to date.
这就是我需要总结的大部分内容。问题是我不知道该怎么做。我研究了ctype、swig、boost python和pyplusplus。到目前为止,我还没有成功地做到以上任何一项。
I keep encountering issues with how wrap the opencv object cv::Mat. From python I will be using numpy arrays so I know I need conversion code from a numpy array to a cv::Mat and I have to register it.
我一直遇到如何包装opencv对象cv:::Mat的问题。从python中,我将使用numpy数组,因此我知道我需要从numpy数组转换为cv:::Mat,我必须注册它。
I feel like someone else must have tried something like this at one point, if you can help me out it is much appreciated
我觉得一定有人曾经尝试过这样的东西,如果你能帮我,我很感激
Just to reiterate the goal: Wrap my c++ class which uses opencv into a python library so that I can use my algorithm from python.
重申一下目标:将使用opencv的c++类封装到python库中,这样我就可以使用python中的算法了。
I think I have the conversion somewhat figured out (with the help of the opencv source) but I still won't work from python.
我想我已经有了某种程度的转换(在opencv源代码的帮助下),但是我仍然不能使用python。
Okay so I have been working with the code linked to in the above post (a conversion from numpy to cv::Mat) and I am still encountering problems. I am going to post my code and hopefully someone more knowledgeable than I can help me out here.
好吧,我一直在使用上面文章中链接的代码(从numpy到cv::Mat的转换),我还在遇到问题。我将发布我的代码,希望有人比我更有见识。
For example here is a simple class:
例如,这里有一个简单的类:
foo.h :
foo。h:
#include <opencv2/core/core.hpp>
class Foo {
public:
Foo();
~Foo();
cv::Mat image;
void bar( cv::Mat in );
};
foo.cpp :
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);
}
And here is where I have attempted to wrap this class using boost::python and bits of code from the above link:
在这里,我尝试使用boost::python和上面链接中的代码来包装这个类:
wrap_foo.cpp
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;
//// Wrapper Functions
void bar(Foo& f, bp::object np);
//// Converter Functions
cv::Mat convertNumpy2Mat(bp::object np);
//// Wrapper Functions
void bar(Foo& f, bp::object np)
{
Mat img = convertNumpy2Mat(np);
f.bar(img);
return;
}
//// Boost Python Class
BOOST_PYTHON_MODULE(lib)
{
bp::class_<Foo>("Foo")
.def("bar", bar)
;
}
//// Converters
cv::Mat convertNumpy2Mat(bp::object np)
{
Mat m;
numpy_to_mat(np.ptr(),m);
return m;
}
The numpy_to_mat function is from the pano_cv library but from playing around with the official opencv source I know that this function is also there to some degree. The full file has the function below what I wrote above. This code compiles with bjam just fine but the when I import into python it crashes. The error is this: libFoo.so: undefined symbol: _ZN2cv3Mat10deallocateEv. I have tried a number of different things but I can't get this to work.
numpy_to_mat函数来自pano_cv库,但是通过使用官方的opencv源代码,我知道这个函数在某种程度上也存在。完整的文件有我上面写的下面的函数。这段代码用bjam编译,但是当我导入python时,它就崩溃了。错误是:libFoo。所以:未定义符号:_ZN2cv3Mat10deallocateEv。我尝试过很多不同的东西,但是我做不到。
Help is most appreciated.
我很感激帮助。
1 个解决方案
#1
1
I found some code on google that converts numpy array to cv::mat in c++ using boost::python: link
我在谷歌上发现了一些代码,使用boost:::python: link将numpy数组转换为c++中的cv:::mat
#1
1
I found some code on google that converts numpy array to cv::mat in c++ using boost::python: link
我在谷歌上发现了一些代码,使用boost:::python: link将numpy数组转换为c++中的cv:::mat