【问题标题】:Fast capture and 2D image stacking as 3D numpy array with Python and Raspberry Pi使用 Python 和 Raspberry Pi 快速捕获和 2D 图像堆叠为 3D numpy 数组
【发布时间】:2015-09-18 00:54:50
【问题描述】:

我正在研究一个 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 捕获保存为文件

最好的问候,阿古斯丁

【问题讨论】:

  • 您想要numpy.dstack docs.scipy.org/doc/numpy/reference/generated/numpy.dstack.html 还是寻找更复杂的东西?
  • 嗨乔。我想要 dstack 之类的东西,但我不知道如何实现它。
  • @AgustinCruz 你事先知道要堆叠多少张图片吗?如果是,您可以预先分配您的数组并在捕获每个图像后填充它。
  • 嗨,Saullo。是的,图像数量是预先固定的。我不知道如何在这段代码中实现预分配:(

标签: python image numpy raspberry-pi pillow


【解决方案1】:

这可能不能直接用作复制粘贴,但应该演示如何预先分配内存并在那里写入结果。我不熟悉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()

【讨论】:

    猜你喜欢
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    相关资源
    最近更新 更多