简体   繁体   English

将1D字节对象重塑为3D numpy数组

[英]Reshaping a 1D bytes object into a 3D numpy array

I'm using FFmpeg to decode a video, and am piping the RGB24 raw data into python. 我正在使用FFmpeg解码视频,并将RGB24原始数据传递到python中。

So the format of the binary data is: 因此,二进制数据的格式为:

RGBRGBRGBRGB...

I need to convert this into a (640, 360, 3) numpy array, and was wondering if I could use reshape for this and, especially, how. 我需要将其转换为(640, 360, 3) 640,360,3 (640, 360, 3) numpy数组,并且想知道是否可以为此使用reshape ,尤其是如何使用reshape

If rgb is a bytearray with 3 * 360 * 640 bytes, all you need is : 如果rgb是具有3 * 360 * 640字节的字节数组,则您需要做的是:

np.array(rgb).reshape(640, 360, 3)

As an example: 举个例子:

>>> import random
>>> import numpy as np
>>> bytearray(random.getrandbits(8) for _ in range(3 * 4 * 4))
bytearray(b'{)jg\xba\xbe&\xd1\xb9\xdd\xf9@\xadL?GV\xca\x19\xfb\xbd\xad\xc2C\xa8,+\x8aEGpo\x04\x89=e\xc3\xef\x17H@\x90]\xd5^\x94~/')
>>> rgb = bytearray(random.getrandbits(8) for _ in range(3 * 4 * 4))
>>> np.array(rgb)
array([112,  68,   7,  41, 175, 109, 124, 111, 116,   6, 124, 168, 146,
        60, 125, 133,   1,  74, 251, 194,  79,  14,  72, 236, 188,  56,
        52, 145, 125, 236,  86, 108, 235,   9, 215,  49, 190,  16,  90,
         9, 114,  43, 214,  65, 132, 128, 145, 214], dtype=uint8)
>>> np.array(rgb).reshape(4,4,3)
array([[[112,  68,   7],
        [ 41, 175, 109],
        [124, 111, 116],
        [  6, 124, 168]],

       [[146,  60, 125],
        [133,   1,  74],
        [251, 194,  79],
        [ 14,  72, 236]],

       [[188,  56,  52],
        [145, 125, 236],
        [ 86, 108, 235],
        [  9, 215,  49]],

       [[190,  16,  90],
        [  9, 114,  43],
        [214,  65, 132],
        [128, 145, 214]]], dtype=uint8)

You might want to look at existing numpy and scipy methods for image processing . 您可能需要查看用于图像处理的现有numpy和scipy方法 misc.imread could be interesting. misc.imread可能很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM