I have an array in bytes, content is camera frame 480x640 x 2 bytes per pixel
raw = np.asarray (d.GetFrameData ( ), dtype=np.uint8) print (raw, raw.shape, type(raw))`
print output is below, 614400=480x640x2, pixel value is 0~255
[0 0 0 ... 0 0 0] (614400,) <class 'numpy.ndarray'>
what’s correct way to convert it into an array with 480x640 with pixel value from 0~65535 ?
Thx in advance
i tried to use dtype=np.uint16, but its shape is still (614400,)
reshape (640, 480, 2) is workable, but that’s not what i need
i need a result in reshape (640, 480) with uint16 size
If each pixel is represented by 2 bytes, and you want to reshape the array into a 2D array with pixel values ranging from 0 to 65535 (dtype=np.uint16), you can use the view method in NumPy to interpret the existing bytes as uint16 values and then reshape the array.
view
Here’s how you can achieve this:
import numpy as np # Assuming 'raw' is your original array with dtype=np.uint8 raw = np.asarray(d.GetFrameData(), dtype=np.uint8) # Convert the array to uint16 by interpreting the bytes result = raw.view(dtype=np.uint16) # Reshape the array to the desired shape (640, 480) result = result.reshape((640, 480)) print(result, result.shape, result.dtype)
This way, each pair of consecutive bytes in the original array will be interpreted as a single uint16 value. The resulting array will have the shape (640, 480) and dtype=np.uint16.