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.


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.