Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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/