numpy 合并将两个单元8合并为一个单元16

nhjlsmyf  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(129)

我有一个字节数组,内容是相机帧480 x640 x 2字节每像素

raw = np.asarray (d.GetFrameData ( ), dtype=np.uint8)
print (raw, raw.shape, type(raw))`

字符串
打印输出为614400= 480 x640 x2,像素值为0~255

[0 0 0 ... 0 0 0] (614400,) <class 'numpy.ndarray'>


如何将其转换为480 x640的数组,像素值从0到65535?
提前谢谢
我尝试使用dtype=np.uint16,但它的形状仍然是(614400,)
shape(640,480,2)是可行的,但这不是我所需要的
我需要一个结果在整形(640,480)与uint 16大小

ki1q1bka

ki1q1bka1#

您正在寻找的是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')

字符串

wkyowqbh

wkyowqbh2#

Thx @mechanic-pig如果我将你的代码的第一行替换为我的帧数据,它就可以工作了

raw = np.asarray (d.StreamFrameData ( ), dtype=np.uint8)
#raw = np.random.default_rng().integers(0, 256, 480 * 640 * 2, np.uint8)
print (raw)
print (raw.view(np.uint16).reshape(640, 480))
print (raw.view('<u2').reshape(640, 480))
print (raw.view('>u2').reshape(640, 480))

字符串

相关问题