logo
background
 Home and Links
 Your PC and Security
 Server NAS
 Wargames
 Astronomy
 PhotoStory
 DVD making
 Raspberry Pi
 PIC projects
 Other projects
 Next >>

Using the Pi Camera

Pi Camera usage
You can find the latest raspistill etc. parameter definitions on github as an ODT document RaspiCamDocs.odt. For those unwilling to download Open Office, here's a simple text snapshot as of 2016-08-19

Image capture (with the 5M pixel Pi-camera)

First you have to 'enable' the camera via 'raspi-config'. To change the settings enter (sudo raspi-config). In the options window, tab or mouse cursor down to the 'Enable Camera' option, press 'return' and select 'yes' .. then exit and reboot

Note - the camera software will give you a 'hint' if you try to take a photo without using raspi-config to enable the camera.
 
If it still won't work after you 'enable', it's time to look for a loose cable etc.

To capture single images use raspistill (or, for RAW, raspiyuv). To capture video use raspivid / raspividyuv (or v4l2 (Video for Linux v2) / UV4L (yuv video 4 linux)). Some documentation for the camera can be found at www.raspberrypi.org/documentation/raspbian/applications/camera.md and www.raspberrypi.org/documentation/hardware/camera.md. For the most up=-to-=date command options, type 'raspistill' at the command prompt

The '-md' option sets the 'mode'. Camera modes are :-

-md 0	"automatic" selection (based on requested frame rate)
-md 1 	1920x1080 	16:9 	1-30fps HD (default video)
-md 2 	2592x1944 	4:3 	1-15fps full frame (default stills)
-md 3 	2592x1944 	4:3 	0.1666-1fps
-md 4 	1296x972 	4:3 	1-42fps (2x2 binning)
-md 5 	1296x730 	16:9 	1-49fps (2x2 binning)
-md 6 	640x480 	4:3 	42.1-60fps 	(2x2 binning plus skip)
-md 7 	640x480 	4:3 	60.1-90fps 	(2x2 binning plus skip)
 
The fps limits apply to movie mode (not stills = still images are limited by the sensor read out time, which takes about 200mS for the whole array)
 
The HD modes all (centre aligned) 'crop' the sensor. 'Binning' combines a 4x4 pixel array into a single pixel (thus increasing low-light sensitivity).
 
Speed is limited by how much of the camera has to be read out (by the GPU).

Note that '15 fps full frame' is only possible in VIDEO MODE (h264), not in 'still image' (JPEG) mode. In still image mode, it takes 200mS to 'read' the sensor (this is on top of the 'exposure' time) which gives us a 'theoretical' 5 fps (assuming '0' exposure time, say in bright sunlight, when 1/1000th = 1mS might be realistic). So data output rates for video are about 10x faster than that for still images. 15 fps video uses about the same bandwidth as 1.5 fps jpeg.

With the recent release of the Pi camera 2, you can expect rapid changes. So it's a 'good idea' to start by updating your Pi system software  :-
 
sudo apt-get update && sudo apt-get upgrade -y
 
and GPU firmware :-
sudo rpi-update
 
Then reboot
sudo reboot

Capturing video with raspivid

The default video capture generates a clip 5 seconds long (note horizontal and vertical flip options are same as raspistill)

raspivid -o file-name.h264

To set the video length, add -t length in mS option, for example, for a 30 second video :-

raspivid -o file-name.h264 -t 30000

To view the video on the Pi, you can use omxplayer :-

omxplayer -o hdmi file-name.h264

Capturing still photographs with raspistill / raspiyuv

Note that YUV (RAW) is the camera native format, so this comes out 'first', whilst JPEG (RGB) - which is GPU hardware accelerated - takes 'a little longer' (there may be only 20mS or so difference but, when trying to get the max. still photo speed (5 fps) it can make a difference

On 'start up', the camera is in 'preview' mode and will take some frames to 'auto-white balance' and 'work up' it's 'auto-grain-control' (AGC).
 
Turning off AGC actually 'freezes' it at the current level (and it starts at zero), so unless you give the camera time (500mS or so) to 'balance' and get it's AGC right, you will (usually) end up with completely underexposed (black) frames (this is especially a problem for Python users who 'go direct' to the camera).
 
The built-in utilities,  raspistill / raspiyuv and raspivid / raspividyuv, all default to the correct 'pre-amble' (on the still photo side, this is why you 'always' loose the second frame of a burst = see later)

The default (full) resolution is 2592 x 1944 pixels and a typical .jpg image size will be about 2.2Mb (allow 2.5Mb to be safe). To take a photo and output (-o) it to a file eg. file-name.jpg you can use the commands:-

raspiyuv -o file-name.raw
This runs the camera for 5 seconds and then captures a still at max. resolution (2592x1944). The RAW file is 2592x1952 'pixels' and the YUV format is 'YUV420p' (which is 6 bytes per 4 pixels) giving 7.5 Mb per frame.
 
raspistill -o file-name.jpg
This runs the camera for 5 seconds and then captures a still at max. resolution (2592x1944). The data is coveted from YUV into RGB and JPEG compressed by the GPU. The JPEG file is 2592x1944 pixels and typically about of 2.2Mb

For more on the camera RAW data, see Wikipedia

The '--colfx' flag (-cfx) allows you to modify the U and V data 'depth'. Setting '-cfx 128:128' shifts all the information into the  Y channel (i.e. generates a monochrome image).

For more on the camera modes, see Raspberry Pi documentation

Setting the total capture time (-t) and the single photo time (-tl)

The -t parameter sets the 'total capture time' in mS i.e. the length of a video clip. When capturing single images, -t sets the 'start delay'. When taking multiple images -t sets the time for the whole 'batch' and the -tl time (in mS) is the 'gap' between images.

If not specified, -t is 5 seconds (i.e. same as -t 5000)
-t 0 means 'run for ever' ?? (Ctrl+c aborts)

The -tl is the time lapse between shots, in milliseconds

If a time-lapse value of 0 is entered, the application will take pictures 'as fast as possible'.
 
Note there is an minimum enforced pause of 30ms between captures to ensure that exposure calculations can be made.

Maximum photos per second

To maximise the still photo rate, keep the camera in 'burst mode', set the fastest 'output format' (this is jpeg+no thumbnail = see below) and minimise the photo file destination write time (RAM-disk is quickest, then Class 10 SDHC, o/p to USB stick usually next then Ethernet). So, first we have to 'set up' for max. speed :-