我有一个以字节为单位的数组,内容是相机帧480x640 x每像素2字节
raw = np.asarray (d.GetFrameData ( ), dtype=np.uint8)
print (raw, raw.shape, type(raw))`
打印输出如下,614400=480x640x2,像素值为0~255
[0 0 0 ... 0 0 0] (614400,) <class 'numpy.ndarray'>
将其转换为 480x640 像素值从 0~65535 的数组的正确方法是什么?
提前谢谢
我尝试使用dtype=np.uint16,但它的形状仍然是(614400,)
reshape (640, 480, 2) 是可行的,但这不是我需要的
我需要一个 uint16 大小的 reshape (640, 480) 结果
numpy.ndarray.view()
:
>>> raw = np.random.default_rng().integers(0, 256, 480 * 640 * 2, np.uint8)
>>> raw
array([205, 10, 58, ..., 26, 204, 55], dtype=uint8)
>>> # dtype can also be 'u2', '=u2', 'H' or '=H',
>>> # whether it is big or little endian depends on the hardware
>>> raw.view(np.uint16).reshape(640, 480)
array([[ 2765, 50234, 24681, ..., 44897, 64548, 55999],
[20626, 58518, 46835, ..., 28429, 29253, 16188],
[65218, 63270, 55857, ..., 57002, 32086, 50488],
...,
[ 4825, 17130, 49826, ..., 33953, 7028, 59005],
[64006, 14762, 55052, ..., 37743, 6392, 31599],
[33882, 6208, 34247, ..., 28039, 6749, 14284]], dtype=uint16)
>>> raw.view('<u2').reshape(640, 480) # or '<H', it is little endian
array([[ 2765, 50234, 24681, ..., 44897, 64548, 55999],
[20626, 58518, 46835, ..., 28429, 29253, 16188],
[65218, 63270, 55857, ..., 57002, 32086, 50488],
...,
[ 4825, 17130, 49826, ..., 33953, 7028, 59005],
[64006, 14762, 55052, ..., 37743, 6392, 31599],
[33882, 6208, 34247, ..., 28039, 6749, 14284]], dtype=uint16)
>>> raw.view('>u2').reshape(640, 480) # or '>H', it is big endian
array([[52490, 15044, 26976, ..., 25007, 9468, 49114],
[37456, 38628, 62390, ..., 3439, 17778, 15423],
[49918, 9975, 12762, ..., 43742, 22141, 14533],
...,
[55570, 59970, 41666, ..., 41348, 29723, 32230],
[ 1786, 43577, 3287, ..., 28563, 63512, 28539],
[23172, 16408, 51077, ..., 34669, 23834, 52279]], dtype='>u2')