Create Mat object from Numpy array

Hi Stereolabs,

I am trying to do the opposite of sl.Mat.get_data(), namely creating an sl.Mat, object that contains the data from an existing numpy.ndarray.
Is this possible with the Python API?

Kind regards

Hello and thank you for reaching out to us !
If you don’t use the deep_copy option when creating your numpy array, the sl.Mat and the numpy array share the same pointer. Meaning the if you modify the numpy array, the sl.Mat will be modified too.

You can use that to do what you need here :

  • Create a sl.Mat
  • Create a numpy array with get_data()
  • Modify the numpy array
  • The sl.Mat is modified too.

Regards

1 Like

Thank you @alassagne for your quick response! This order of operations did the job for me.
Although, I do think a classmethod such as sl.Mat.from_numpy() would have come in handy in this case. Maybe it is an idea for some added functionality but I will use your workaround for now.

Indeed. Glad the workaround did the trick in the meantime, we’ll think about a feature.

Hi,alassagne.
If, as you said, I want to extract data from a certain area of Mat, how do I do it?

Hi, please create a new thread for your question (and put more explanations / context within it)

Hey, Is there really no way of exchanging the pointer the sl::Mat points to? I’d like to share the underlying memory buffer as numpy.ndarray via a python multiproccessing.Array. I cannot find a way to either tell the multiprocessing.Array to use the sl::Mat data or vise versa.