Answer the question
In order to leave comments, you need to log in
How to convert OpenCV Mat to Python ndarray?
Hello everyone, experts, in general, there is a function that I use through Python Boost, I need to transfer OpenCV Mat to Python, when I try to do it directly, boost gives TypeError: No to_python (by-value) converter found for C++ type: cv::Mat, Which makes sense since Python doesn't use ndarray for images.
#include <iostream>
#include <opencv2/highgui.hpp>
#include "opencv2/cudawarping.hpp"
#include <boost/python.hpp>
using namespace std;
using namespace cv;
Mat gpuResize(string path, int maxwidth, int height)
{
Mat inputCpu = imread(path, IMREAD_COLOR);
cuda::GpuMat input(inputCpu);
cuda::GpuMat output;
cuda::resize(input, output, Size(maxwidth, height), 0, 0, INTER_LINEAR);
Mat outputCpu;
output.download(outputCpu);
// PyObject* ret = toNDArray(outputCpu);
return outputCpu;
}
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(cvcuda)
{
using namespace boost::python;
def("gpuResize",gpuResize);
}
from cvcuda import gpuResize
path = '/home/alenorze/Workspace/Current/hair/backend/test_data/0e6303c4-0b29-457e-ab74-ac2d1cfb1ecf.jpeg'
maxwidth = 650
height = 500
print(gpuResize(path, maxwidth, height))
Answer the question
In order to leave comments, you need to log in
In general, there is one solution, and it is completely wrong, but logical. OpenCV does not have code for Python, it is completely in C++, there is a converter in python/src2/cv2.cpp that can convert a lot of things, including Mat, but since you need to have at least some experience with pluses to run it separately ( which I don't have).
I decided to modify the library itself and insert new functions into it in order to import directly through cv2. I found the imread function inside loadsave.cpp, added my function, declared it next to imread, and everything worked, no problems. But again, I do not recommend doing this, it's a sin.
At one time I made a computational module for python in C, transferred C arrays from the module to python and displayed them there using Matplotlib. Implemented the Python Buffer Protocol in the module . Further in python, I received such an object from the module and, using numpy.asarray(), distilled it into ndarray.
If Mat is also based on a regular array, then the Buffer Protocol can be implemented for it in a similar way.
I don't know how to do it with python boost, I used Python C API.
It is also possible to create numpy objects directly in the module, numpy has its own numpy C API. But I had no experience with this.
I don't have any easier options.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question