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