Issue
I am working on a project on which I have to deal with images between Python and C++. In order to send an image from C++ to Python, I send a pointer on the first pixel of my image (being a Mat
object from OpenCV
library) and in Python with two for loops I used this pointer to create a 2D numpy array.
But I don't succeed to do the same in the other way : get the memory address of the first pixel of the numpy 2D array (an image) and pass it to the C++.
Assume x is my image (2D numpy array), I tried solution like :
x.__array_interface__['data'][0]
but this give an integer, not an addressx.data
which give different values each time I call it because it gives me the address of a temporary buffer, not the image itself
Thank you in advance for any advice/help.
Solution
I found a "simple" way to do that:
In C++, the function has to take as argument a void * ptr
, and like this I can create a Mat object using the pointer sent from Python :
void myCfunction(void* ptr){
Mat matrix = Mat(sizes, CV_8UC1, (uchar*)ptr);
}
And, in Python, use __array_interface__
to get the memory address (in int type, weird) :
mydll.myFunction(myNumpyArray.__array_interface__['data'][0], ...)
Answered By - Mathieu Gauquelin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.