Ask Your Question

wlei's profile - activity

2020-03-31 07:18:13 -0600 received badge  Popular Question (source)
2020-03-31 05:40:20 -0600 received badge  Popular Question (source)
2018-05-03 17:31:02 -0600 marked best answer how to return a numpy array to python from c++ (shared memory)

Is there a way to return a numpy array from c++ to python (shared memory)? This is for sharing image data between a c++ app and python. I am using SWIG currently.

I was able to shared the memory between c++ and python (for modifying image data in both end). Please see http://answers.opencv.org/question/18...

Now, instead of letting user to retrieve the shared data from memory and construct the right shape for the numpy array, I would like to package that so that it will just return the numpy array (shared memory) and let user manipulates it in python?

numpy_array = np.frombuffer(imgPtr, dtype=np.uint8) //for one byte integer image numpy_array.shape = (rows1, cols1)

So in summary, I would like to package the above two lines of code in my c++ app and provide a function call in python to retrieve the numpy array. This is more user friendly since the user that uses python app are not professional sw developer, so asking them to get the data from shared memory and reshape the numpy array before they can use the numpy array to visualize it in opencv is a little bit too much for them.

Thanks in advamce/

2018-05-03 17:30:39 -0600 answered a question how to return a numpy array to python from c++ (shared memory)

... import_array(); npy_intp size[2] = { height, width }; int type = GetNPEnumTypeForImage(); PyObject* pNdArray = PyArr

2018-05-01 17:01:28 -0600 commented question how to return a numpy array to python from c++ (shared memory)

I think I can use PyArray_SimpleNewFromData()... but I got access violation error with the following code: assuming it

2018-05-01 15:48:06 -0600 commented question how to return a numpy array to python from c++ (shared memory)

no, I am still looking for a solution...I am still researching how to implement GetNumpyArray() on the c++ side...

2018-05-01 15:47:19 -0600 commented question how to return a numpy array to python from c++ (shared memory)

no, I am still looking for a solution...I need to find out how to implement GetNumpyArray() on the c++ side...

2018-05-01 15:46:38 -0600 commented question how to return a numpy array to python from c++ (shared memory)

no, I am still looking for a solution...

2018-05-01 13:01:03 -0600 commented question how to return a numpy array to python from c++ (shared memory)

The original solution requires user to understand the programming basics of retrieving the buffer pointer, create a nump

2018-05-01 12:57:23 -0600 commented question how to return a numpy array to python from c++ (shared memory)

I think you must have misunderstood my question. Instead of letting user retrieve the shared memory pointer, create a n

2018-05-01 12:20:48 -0600 commented question how to return a numpy array to python from c++ (shared memory)

Unfortunately, my users do not have interest or time to learn c++. I wish you are my target user :-). Anyway, thanks fo

2018-05-01 10:52:34 -0600 asked a question how to return a numpy array to python from c++ (shared memory)

how to return a numpy array to python from c++ (shared memory) Is there a way to return a numpy array from c++ to python

2018-05-01 10:52:26 -0600 asked a question how to return a numpy array to python from c++ (shared memory)

how to return a numpy array to python from c++ (shared memory) Is there a way to return a numpy array from c++ to python

2018-04-27 15:42:56 -0600 marked best answer display function (similar to cv2.imshow) in opencv that can display mat with data type =8 (i.e. np.uint32)

Hello,

Does anybody know why np.frombuffer() does not work with np.unit32? Do you know any fix or workaround?

I am not able to update the title... but after investigation, np.frombuffer does work with int8, unint8, int16, uint16, int32... Only uint32 does not work...

Thanks

Update on the investigation : actually, np.frombuffer() with np.uint32 does work. The thing that is not working is cv2.imshow() - when I tried to show the image that I loaded from the buffer. I got the following error saying:

TypeError: mat data type = 8 is not supported

is there any display function in opencv that can display mat with data type =8 (i.e. np.uint32)?

2018-04-27 13:09:04 -0600 commented question display function (similar to cv2.imshow) in opencv that can display mat with data type =8 (i.e. np.uint32)

thanks berak for pointing it out... I am very new to the forum :-)

2018-04-27 13:08:36 -0600 edited question display function (similar to cv2.imshow) in opencv that can display mat with data type =8 (i.e. np.uint32)

(numpy) np.frombuffer does not work with any other dtype except np.int8 or np.uint8 Hello, Does anybody know why np.fro

2018-04-27 13:02:49 -0600 commented question display function (similar to cv2.imshow) in opencv that can display mat with data type =8 (i.e. np.uint32)

since opencv use numpy :-) ... see my updated question... it is actually a question on the opencv side...

2018-04-27 13:02:19 -0600 edited question display function (similar to cv2.imshow) in opencv that can display mat with data type =8 (i.e. np.uint32)

(numpy) np.frombuffer does not work with any other dtype except np.int8 or np.uint8 Hello, Does anybody know why np.fro

2018-04-27 12:59:53 -0600 commented question display function (similar to cv2.imshow) in opencv that can display mat with data type =8 (i.e. np.uint32)

since opencv use numpy :-)

2018-04-27 12:22:47 -0600 edited question display function (similar to cv2.imshow) in opencv that can display mat with data type =8 (i.e. np.uint32)

(numpy) np.frombuffer does not work with any other dtype except np.int8 or np.uint8 Hello, Does anybody know why np.fro

2018-04-27 12:20:32 -0600 commented question display function (similar to cv2.imshow) in opencv that can display mat with data type =8 (i.e. np.uint32)

Correction: I think only np.unit32 does not work work with np.frombuffer(..), all the other datatype work (even np.int32

2018-04-27 09:30:27 -0600 received badge  Enthusiast
2018-04-25 18:02:40 -0600 edited question display function (similar to cv2.imshow) in opencv that can display mat with data type =8 (i.e. np.uint32)

(numpy) np.frombuffer does not work with any other dtype except np.int8 or np.uint8 Hello, Does anybody know why np.fro

2018-04-25 17:29:31 -0600 asked a question (numpy) np.frombuffer does not work with any other dtype except np.int8 or np.uint8

(numpy) np.frombuffer does not work with any other dtype except np.int8 or np.uint8 Hello, Does anybody know why np.fro

2018-04-25 17:29:27 -0600 asked a question display function (similar to cv2.imshow) in opencv that can display mat with data type =8 (i.e. np.uint32)

(numpy) np.frombuffer does not work with any other dtype except np.int8 or np.uint8 Hello, Does anybody know why np.fro

2018-04-25 15:32:40 -0600 edited question PYTHON C++ SWIG share image pixel data in memory

PYTHON C++ SWIG share image pixel data in memory Hello, I am working on passing image pixel data from c++ app to python

2018-04-25 15:32:40 -0600 received badge  Editor (source)
2018-04-25 15:08:54 -0600 commented answer PYTHON C++ SWIG share image pixel data in memory

In this case, we don't really need SWIG to do the interfacing since we already return a PyObject*. But, if one is alrea

2018-04-25 14:14:05 -0600 received badge  Self-Learner (source)
2018-04-25 13:21:01 -0600 answered a question PYTHON C++ SWIG share image pixel data in memory

To expose the shared python buffer from C++ side: PyObject* :GetBufferPtr(Image img, long width, long height) { .....

2018-04-25 13:14:33 -0600 commented answer PYTHON C++ SWIG share image pixel data in memory

Yes, it is working. Sure, I would love to share. I actually already paste the code in my comments. I will reformat it

2018-04-25 09:20:52 -0600 received badge  Supporter (source)
2018-04-25 09:19:38 -0600 marked best answer PYTHON C++ SWIG share image pixel data in memory

Hello,

I am working on passing image pixel data from c++ app to python and vice versa. The use case is user creates an image in the c++ application and then retrieve the image (preferably from memory so that we do not need to copy back and forth) and do some image process in python (using python image processing library such as PIL, opencv etc). Once image processing is done in python, we would want to pass the image back to the c++ application (preferably from memory as well, so that we do not need to copy the image data).

So, my question is how to get the image pixel data from python? I can pass the void* that points to the pixel data as well as the width and height information from the C++ application (with SWIG).

And how do we pass the image pixel data from python back to the c++ app (again prefer to just refer it to the pixel data in memory)?

My application is written in c++ and I am using SWIG to interface with python.

Thanks.

2018-04-25 09:19:38 -0600 received badge  Scholar (source)
2018-04-25 09:18:51 -0600 commented answer PYTHON C++ SWIG share image pixel data in memory

actually, my python code did copy the data to the shared memory. I just did not fill up the complete memory buffer, so

2018-04-24 16:38:59 -0600 commented answer PYTHON C++ SWIG share image pixel data in memory

Sorry, I do not have the c++ code on hand (will take me a long time to find where it is)... but the c++ code does not ha

2018-04-24 14:57:10 -0600 commented answer PYTHON C++ SWIG share image pixel data in memory

yes, it is located at https://gist.github.com/wlei2018/ff5cb5f9e070431d8c92c0b91d9d7811

2018-04-24 13:44:05 -0600 commented answer PYTHON C++ SWIG share image pixel data in memory

import myLib import cv2 import numpy as np imgPtr = myLib.GetImgBuffer(img1, cols1, rows1) numpy_array = np.frombuffer(i

2018-04-24 13:34:17 -0600 commented answer PYTHON C++ SWIG share image pixel data in memory

I am using the latest opencv (3.4.1) The get_img_from_char_array does not help as I was looking at the code on python s

2018-04-24 09:59:50 -0600 commented answer PYTHON C++ SWIG share image pixel data in memory

Thank you. Now, the other part is how to write to the shared memory from python side. I am able retrieve the numpy arra

2018-04-24 09:57:07 -0600 commented question PYTHON C++ SWIG share image pixel data in memory

Now, the other part is how to write to the shared memory from python side. I am able retrieve the numpy array and creat