How to use sl.Mat.init_mat_cpu()

Dear StereoLabs support,

The sl.Mat API includes the init_mat_cpu() method, as documented in the API Reference, however I haven’t found any example code in the documentation or GitHub repositories and it’s not clear to me how to use it.

I’m trying to initialise an sl.Mat object from a data pointer to previously allocated memory, but all attempts have failed, in the sense that the sl.Mat object either is not initialised or it points to different memory.

First, I tried to use the init_mat_cpu() method to create an sl.Mat pointing to pre-allocated memory, so that calling retrieve_image() would read camera frames into such memory, but no frame showed up and the variable remained empty.

So I put together the following code snippet to test the init_mat_cpu() method and realised that even though I used init_mat_cpu() the sl.Mat would point to a different memory address, so of course any attempt to set either the numpy variable or the sl.Mat does not affect the other variable.

import numpy as np
import pycuda.driver as cuda
import pycuda.autoinit
import pyzed.sl as sl

width, height = 4, 3
np_frame = cuda.managed_empty(shape=(height, width, 3), dtype=np.uint8, mem_flags=cuda.mem_attach_flags.GLOBAL)
zed_frame = sl.Mat(width, height, sl.MAT_TYPE.U8_C3, sl.MEM.CPU)
zed_frame.init_mat_cpu(width, height, sl.MAT_TYPE.U8_C3, str(np_frame.ctypes.data), np_frame.ctypes.strides[0])
print(zed_frame.is_init())  # prints True
print(zed_frame.get_pointer() == np_frame.ctypes.data)  # prints False, expected True

zed_frame.set_to([64, 64, 64])
print(np_frame)  # prints all 0, expected all 64
np_frame.fill(128)
print(zed_frame.get_data())  # prints all 64, expected all 128

Same outcome without pycuda and using just numpy to initialise np_frame:

np_frame = np.zeros(shape=(height, width, 3), dtype=np.uint8)

Also, creating the sl.Mat object with default constructor zed_frame = sl.Mat() and then calling init_mat_cpu() on it ends up with an object that is not really initialised, in fact is_init() returns False.

What is the proper way to use init_mat_cpu()?

In case it is relevant, I’m on Orin NX with Jetpack 6.2.1 and ZED SDK 5.2.3.

Thanks in advance,
Massimo

P.S. this topic is partially related to an older topic: Create Mat object from Numpy array

Hi @mminervini
The described behavior of init_mat_cpu is not expected.

The problem has been noted, and we will try to replicate and fix it.

Have you checked if the latest v5.3 is also affected by this issue?

Hi @Myzhar

I upgraded to ZED SDK v5.3.0 and observed the same issue.

Thank you for the feedback

1 Like

@mminervini, the SDK Team detected an issue in the code of the Python wrapper and fixed it.
The fix will be available with the next ZED SDK v5.3.1.

Thanks! Looking forward to the new SDK release

1 Like