为我的算法创建一个使用 Opencv 2.3 的 Python 包装器
我正在寻找包装一个 C++ 类,它实现我使用 Opencv 2.3 编写的算法。我知道 opencv 有一个整体的 python 包装器,但我需要的是包装我自己的使用 opencv 的代码。这似乎是合乎逻辑的,因为我的算法的较低级别将是快速编译的 C++ 代码,我将可以自由地从 python 调用它并围绕它构建一个系统。
我的类实际上非常简单,它有 4 个主要方法:
void train( std::vector<cv::Mat> );
void save();
void load();
bool detect( cv::Mat );
这基本上是我需要包装的大部分内容。问题是我不知道如何最好地解决这个问题。我研究过 ctypes、swig、boost python 和 pyplusplus。到目前为止,我还没有成功完成上述任何一项。
我一直遇到如何包装 opencv 对象 cv::Mat 的问题。在 python 中,我将使用 numpy 数组,所以我知道我需要从 numpy 数组到 cv::Mat 的转换代码,并且我必须注册它。
我觉得其他人一定曾经尝试过这样的事情,如果你能帮助我,我会非常感激,
只是重申一下目标:将我使用 opencv 的 c++ 类包装到 python 库中,以便我可以使用我的算法Python。
我想我已经在某种程度上弄清楚了转换(在 opencv 源的帮助下),但我仍然无法使用 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;
//// 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;
}
numpy_to_mat 函数来自pano_cv 库,但通过使用官方 opencv 源代码,我知道这个函数在某种程度上也存在。完整文件的功能如下我上面写的。这段代码用 bjam 编译得很好,但是当我导入 python 时它崩溃了。错误是这样的:libFoo.so:未定义符号:_ZN2cv3Mat10deallocateEv。我尝试了很多不同的事情,但我无法让它发挥作用。
非常感谢您的帮助。
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.
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 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.
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.
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.
I think I have the conversion somewhat figured out (with the help of the opencv source) but I still won't work from 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.
For example here is a simple class:
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);
}
And here is where I have attempted to wrap this class using boost::python and bits of code from the above link:
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.
Help is most appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在谷歌上找到了一些代码,使用 boost::python 将 numpy 数组转换为 c++ 中的 cv::mat :
链接
I found some code on google that converts numpy array to cv::mat in c++ using boost::python:
link