简体   繁体   中英

Expose OpenCV C++ Mat to Python

Hello everyone basically i have the following

A test.cpp as it follows

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:

#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

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.

At the time i have the following error: 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.

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. 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. Check this part of Boost's python documentation for how to do it.

If you want to use OpenCV in both C++ and Python, it's a little more complicated than it sounds. 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 . 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++). 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 ).

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. Python may be slow, but the development time can be much, much faster. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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