将帧堆叠到3D阵列

时间:2015-07-06 21:02:52

标签: python python-2.7 python-3.x numpy

我正在开发一个Raspberry Pi项目,我需要每秒拍摄大约30张图像(没有电影),并使用numpy数组将每个2D图像堆叠到3D数组,而不将每个2D捕获保存为文件(因为很慢)。

我发现这个Python代码尽可能快地拍摄图像,但我不知道如何将所有图像快速堆叠到3D图像堆栈。

import io
import time
import picamera
#from PIL import Image

def outputs():
    stream = io.BytesIO()
    for i in range(40):
        # This returns the stream for the camera to capture to
        yield stream
        # Once the capture is complete, the loop continues here
        # (read up on generator functions in Python to understand
        # the yield statement). Here you could do some processing
        # on the image...
        #stream.seek(0)
        #img = Image.open(stream)
        # Finally, reset the stream for the next capture
        stream.seek(0)
        stream.truncate()

with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.framerate = 80
    time.sleep(2)
    start = time.time()
    camera.capture_sequence(outputs(), 'jpeg', use_video_port=True)
    finish = time.time()
    print('Captured 40 images at %.2ffps' % (40 / (finish - start)))

您是否有人知道如何使用Python和Raspberry Pi相机模块将此代码中拍摄的2D图像堆叠到3D numpy数组中?不将每个2D捕获保存为文件

最好的问候,Agustín

1 个答案:

答案 0 :(得分:1)

numpy.dstack() / numpy.hstack() / numpy.vstack()numpy.reshape()的任意组合效果都很好,具体取决于您想要达到的目标,例如:

>>> A,B,C = (np.array([[1,2],[3,4]])+i*10 for i in range(3))
>>> A
array([[1, 2],
       [3, 4]])
>>> B
array([[11, 12],
       [13, 14]])
>>> C
array([[21, 22],
       [23, 24]])

>>> D=np.reshape(np.vstack((A,B,C)), (3,2,2))
>>> D
array([[[ 1,  2],
        [ 3,  4]],

       [[11, 12],
        [13, 14]],

       [[21, 22],
        [23, 24]]])

>>> D[0]
array([[1, 2],
       [3, 4]])
>>> D[1]
array([[11, 12],
       [13, 14]])
>>> D[2]
array([[21, 22],
       [23, 24]])
>>>