简体   繁体   English

将 OpenCV C++ Mat 暴露给 Python

[英]Expose OpenCV C++ Mat to Python

Hello everyone basically i have the following大家好,基本上我有以下几点

A test.cpp as it follows如下所示的 test.cpp

cv::Mat load(string filename){

Mat img = imread(filename,CV_LOAD_IMAGE_COLOR);
GpuMat cudaMat;
cudaMat.upload(img);
cuda::DeviceInfo deviceinfo;
cout << "GPU: "<< deviceinfo.cuda::DeviceInfo::name() << endl;   
imshow("opencvtest_load",img);
waitKey(0);
return img;
}

and i'm wrapping with boost as it follows:我用 boost 包装如下:

#include<boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(opencvtest)
{
def("load",load);
}

I generate everythong with the make command To be called from python code test.py我使用 make 命令生成所有内容 To be call from python code test.py

image = "some directory and image"
from opencvtest import load
img3 = load(image)

So, in fact what i need now is to get that Mat from the load method to be processed converted to python.所以,实际上我现在需要的是从要处理的加载方法中获取 Mat 转换为 python。

At the time i have the following error: TypeError: No to_python (by-value) converter found for C++ type:当时我有以下错误:TypeError: No to_python (by-value) converter found for C++ type:
cv::Mat简历::垫子

So i'm done with all the libraries solutions because they trow errors all the time.所以我已经完成了所有的库解决方案,因为它们一直在抛出错误。 Is there a better solution for this.有没有更好的解决方案。 Thanks a lot in advance非常感谢提前

Long story short: in order to use Boost with a C++ class, you need to expose the C++ methods to Python as well.长话短说:为了将 Boost 与 C++ 类一起使用,您还需要将 C++ 方法公开给 Python。

Python's memory management strategy is very different from C++'s, so in order for an object to be managed by both, you need to wrap the C++ class in a Python-friendly wrapper. Python 的内存管理策略与 C++ 的非常不同,因此为了让对象同时被两者管理,您需要将 C++ 类包装在 Python 友好的包装器中。 Boost has functions to facilitate this, though I haven't checked exactly how to do this and Boost's documentation isn't amazing for the Python library. Boost 具有促进这一点的功能,尽管我还没有确切地检查如何做到这一点,而且 Boost 的文档对于 Python 库来说并不令人惊奇。 Check this part of Boost's python documentation for how to do it.查看Boost 的 Python 文档的这一部分以了解如何执行此操作。

If you want to use OpenCV in both C++ and Python, it's a little more complicated than it sounds.如果你想在 C++ 和 Python 中使用 OpenCV,它比听起来要复杂一些。 Even though OpenCV has both C++ and Python bindings, the Python bindings all use NumPy whereas the C++ bindings use classes such as cv::Mat .尽管 OpenCV 具有 C++ 和 Python 绑定,但 Python 绑定都使用 NumPy,而 C++ 绑定使用诸如cv::Mat类的类。 In order to use them together, you'll have to deal with converting to and from those formats.为了一起使用它们,您必须处理与这些格式之间的转换。

However, that conversion can be made a little simpler, since Boost also has NumPy array bindings (which means that the conversion can be handled entirely in C++).然而,这种转换可以更简单一些,因为 Boost 也有 NumPy 数组绑定(这意味着转换可以完全在 C++ 中处理)。 I found this bit of code that helped me handle the rather confusing conversions, and found that even with an HD image the overhead of converting was minimal (since it's mostly memcpy ).我发现这段代码可以帮助我处理相当混乱的转换,并发现即使使用高清图像,转换的开销也很小(因为它主要是memcpy )。

And as for the comment of "why use Python if you have C++ available"- development time of C++ can be slow, especially when dealing with image processing.至于“如果有 C++ 可用,为什么要使用 Python”的评论——C++ 的开发时间可能很慢,尤其是在处理图像处理时。 Python may be slow, but the development time can be much, much faster. Python 可能很慢,但开发时间可以快得多。 If you've written working Python code and want to speed it up, it can be easier to convert pieces of it to C++ than to start rewriting it entirely.如果您已经编写了可运行的 Python 代码并希望加快速度,那么将它的一部分转换为 C++ 比开始完全重写它更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM