我正在开发一个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
答案 0 :(得分:0)
这可能无法直接作为copy-n-paste工作,但应该演示如何预先分配内存并在那里写入结果。我不熟悉pycamera
,但示例here显示了内存流的一些不同用法。
import numpy as np
def outputs(resolution, n_pics, clr_channels=3):
# Note that the first dimension is height and second is width
images = np.zeros((resolution[1], resolution[0], clr_channels, n_pics), dtype=np.uint8)
stream = io.BytesIO()
for i in range(n_pics):
yield stream
images[:,:,:,i] = Image.open(stream)
stream.seek(0)
stream.truncate()