Fast capture and 2D image stacking as 3D numpy array with Python and Raspberry Pi

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Jul 7 06:12:50 EDT 2015


On Mon, 6 Jul 2015 at 22:36 Agustin Cruz <agustin.cruz at gmail.com> wrote:

> I'm working on a Python - Raspberry Pi project in which I need to take
> about 30 images per second (no movie) and stack each 2D image to a 3D array
> using numpy array, without saving each 2D capture as a file (because is
> slow).
>
> I found this Python code to take images as fast as possible, but i don't
> know how to stack all images fast to a 3D stack of images.
>

What does the code below actually do? Does it save the images as files? Do
you know how to modify the code so that you get each image as a matrix (2D
array)?


>
> 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)
>

I guess the commented lines above do what you want. From the Image object
there will be some way to retrieve a matrix representing the image. Is the
image black and white? If it's RGB then you would have size 640x480x3 3D
array.


>         # 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)))
>
> Does anyone of you know how to stack the 2D images taken in this code to a
> 3D numpy array using Python and the Raspberry Pi camera module? Without
> saving each 2D capture as a file
>

I can't immediately see how to access the 2D images from the code above.
See if you can change it so that it gets the image matrix for each image
(and e.g. prints out the matrix). Then change it so that you build a list
of the 2D matrices then you can just do:

video = numpy.dstack(list_of_images)

And video should be a 3D array. (I'm assuming black and white images here).

--
Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150707/5ff4ad53/attachment.html>


More information about the Python-list mailing list