How to convert numpy array to cv::Mat

asked 2019-11-25 15:53:14 -0600

gst gravatar image

Hi,

We are using OpenCV 4 (latest 4.1.2) with Python 3.6 and latest numpy 1.17 and we would like to convert some numpy arrays to cv::Mat type..

how to do that ? is that possible ?

given I can pass numpy arrays from python to c++ (with boost) I can do that either on python side or in c++ side.. (but my preferrence would be to python side).

Thanks for any info.

Greg.

edit retag flag offensive close merge delete

Comments

just to scare you -- have a look here

i seriously doubt, that mixing opencv's attempt at wrapping Mat / UMat and your idea using boost will ever work out nicely together.

berak gravatar imageberak ( 2019-11-26 01:51:31 -0600 )edit

Hi, thx for the link. How are we supposed to use that ? :| I already saw it but I dont see any example in the sources or nowhere on internet.. Regards.

gst gravatar imagegst ( 2019-11-26 06:48:40 -0600 )edit

heh, you are not supposed to use that, it's part of the automated wrapping system.

honestly, i'm more trying to scare you away from the whole idea.

berak gravatar imageberak ( 2019-11-26 07:01:57 -0600 )edit

I don't quite get you :/

there is no way to convert a numpy array to a cv::Mat (direcly or indirectly) ?

thx anyway.

gst gravatar imagegst ( 2019-11-26 08:52:16 -0600 )edit

why do you want to do this ?

if you have existing c++ (opencv) code, it's probably easier to use the existing wrapping mechanism, than to roll your own

berak gravatar imageberak ( 2019-11-26 09:19:12 -0600 )edit

we have previously python code doing operations on numpy arrays. we converted this code to c++ and use opencv to do operations but we want to be able to still pass (or convert) numpy arrays from python to our c++ code (wrapped with boost)..

I am quite suprised this isn't a common need :/

gst gravatar imagegst ( 2019-11-26 09:37:28 -0600 )edit

I could create a cv::Mat from a numpy array. I now have to be able to do that opposite and my problem would be solved..

gst gravatar imagegst ( 2019-11-26 10:36:01 -0600 )edit