Wednesday, May 22, 2013

22. The Raspberry Pi Camera

On 14-May-13, the Raspberry Pi Foundation released their much anticipated Camera Board.  I ordered mine from one of the distributors, CPC (Farnell), and I got delivery the next day!  I think I was very lucky because they ran out of them very soon and lots of people who want them will be waiting for some time.  The roughly one-inch square board looks like this:



I have suspended mine with a piece of green jumper wire through two of its mounting holes.  I have photographed it at this angle because this is the orientation it takes pictures in (top of the object at the top of the image).  The images taken are mirror reflections (although you can easily change this with a -hf addition to the command line - see below), as you would expect to see the image in a mirror, rather than as a photographic camera would take them.  [Note that this is not a problem with later Raspbian "wheezy" software updates].  The connector tape can just be seen coming out of the bottom, on its way to the Raspberry Pi.



Above is the camera connected to my Pi.

The images are surprisingly good for such a cheap camera (less than £20 delivered).  
The camera has 5 megapixels, so its resolution is good, and although it is permanently focused at infinity, objects more than a couple of feet away are pretty sharp.  A useful comparison of the Raspberry Pi camera with 2 camera phones and a couple of cameras showed that the Raspi Cam came out not too badly:

http://raspi.tv/2013/comparison-of-real-resolving-power-of-raspicam-with-other-cameras

The software is still being developed by the producers and the plan is to not only improve the image quality, but to increase the video frame rate.

The commands raspistill (for taking still pictures) and raspivid (for taking video) need to be qualified by adding options to the command line - for example,  

raspistill -o image1.jpg will capture a jpeg image named image1.jpg,
raspivid -o video1.h264 will record a 5 second video in h264 (raw) format -  video1.h264,
raspivid -o video1.h264 -t 10000 will record a 10 second video named video1.h264

STILLS (Including TIME LAPSE):
The basic command options for capturing still (2592 pixels by 1944 pixels) are as follows:

 
-?, --help : This help information
-w, --width : Set image width
-h, --height : Set image height
-q, --quality : Set jpeg quality <0 data-blogger-escaped-100="" data-blogger-escaped-to="">
-r, --raw : Add raw bayer data to jpeg metadata
-o, --output : Output filename (to write to stdout, use '-o -'). If not specified, no file is saved
-v, --verbose : Output verbose information during run
-t, --timeout : Time (in ms) before takes picture and shuts down (if not specified, set to 5s)
-th, --thumb : Set thumbnail parameters (x:y:quality)
-d, --demo : Run a demo mode (cycle through range of camera options, no capture)
-e, --encoding : Encoding to use for output file (jpg, bmp, gif, png)
-x, --exif : EXIF tag to apply to captures (format as 'key=value')
-tl, --timelapse : Timelapse mode. Takes a picture every ms
Preview parameter commands
-p, --preview : Preview window settings <'x,y,w,h'>
-f, --fullscreen : Fullscreen preview mode
-n, --nopreview : Do not display a preview window
Image parameter commands
-sh, --sharpness : Set image sharpness (-100 to 100)
-co, --contrast : Set image contrast (-100 to 100)
-br, --brightness : Set image brightness (0 to 100)
-sa, --saturation : Set image saturation (-100 to 100)
-ISO, --ISO : Set capture ISO
-vs, --vstab : Turn on video stablisation
-ev, --ev : Set EV compensation
-ex, --exposure : Set exposure mode (see Notes)
-awb, --awb : Set AWB mode (see Notes)
-ifx, --imxfx : Set image effect (see Notes)
-cfx, --colfx : Set colour effect (U:V)
-mm, --metering : Set metering mode (see Notes)
-rot, --rotation : Set image rotation (0-359)
-hf, --hflip : Set horizontal flip
-vf, --vflip : Set vertical flip
 
_______________________________
Posted by  in Tutorials on May 22, 2013 . 0 Comments.


RaspiStill Camera App
Runs the camera for a specific time, and takes a JPG capture at the end if requested 
Usage: RaspiStill [options] Image parameter commands

Notes 
Exposure mode options:
off, auto, night, nightpreview, backlight, spotlight, sports, snow, beach, verylong, fixedfps, antishake, fireworks 

AWB mode options :
off, auto, sun, cloud, shade, tungsten, fluorescent, incandescent, flash, horizon 

Image Effect mode options:
none, negative, solarise, sketch, denoise, emboss, oilpaint, hatch, gpen, pastel, watercolour, film, blur, saturation, colourswap, washedout, posterise, colourpoint, colourbalance, cartoon 

Metering Mode options :
average, spot, backlit, matrix



To display the still image on the Raspberry Pi, you can simply open the file using Image Viewer or a similar Raspberry Pi program.

Here is an example of a still image which is an image of the Pi and camera in a mirror, with a map background:

PiCam Self Portrait - it's all done using mirrors!


VIDEO:
The options for recording video (1920 x 1080 pixels, at 30 fps (frames per second) (Described as Full HD) or 1280 x 720 (60 fps), or 640 x 480 (at 60 or 90 fps) are:

Posted by  in Tutorials on May 22, 2013 . 0 Comments.


RaspiVid Camera App
Display camera output to display, and optionally saves an H264 capture at requested bitrate Usage: RaspiVid [options] Image parameter commands 


 
-?, --help : This help information
-w, --width : Set image width <size>. Default 1920
-h, --height : Set image height <size>. Default 1080
-b, --bitrate : Set bitrate. Use bits per second (e.g. 10MBits/s would be -b 10000000)
-o, --output : Output filename <filename> (to write to stdout, use '-o -')
-v, --verbose : Output verbose information during run
-t, --timeout : Time (in ms) before takes picture and shuts down. If not specified, set to 5s
-d, --demo : Run a demo mode (cycle through range of camera options, no capture)
-fps, --framerate : Specify the frames per second to record
-e, --penc : Display preview image *after* encoding (shows compression artifacts)
Preview parameter commands
-p, --preview : Preview window settings <'x,y,w,h'>
-f, --fullscreen : Fullscreen preview mode
-n, --nopreview : Do not display a preview window
Image parameter commands
-sh, --sharpness : Set image sharpness (-100 to 100)
-co, --contrast : Set image contrast (-100 to 100)
-br, --brightness : Set image brightness (0 to 100)
-sa, --saturation : Set image saturation (-100 to 100)
-ISO, --ISO : Set capture ISO
-vs, --vstab : Turn on video stablisation
-ev, --ev : Set EV compensation
-ex, --exposure : Set exposure mode (see Notes)
-awb, --awb : Set AWB mode (see Notes)
-ifx, --imxfx : Set image effect (see Notes)
-cfx, --colfx : Set colour effect (U:V)
-mm, --metering : Set metering mode (see Notes)
-rot, --rotation : Set image rotation (0-359)
-hf, --hflip : Set horizontal flip
-vf, --vflip : Set vertical flip
 
Notes
 
Exposure mode options :
off,auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks
 
AWB mode options :
off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,flash,horizon
 
Image Effect mode options :
none,negative,solarise,sketch,denoise,emboss,oilpaint,hatch,gpen,pastel,watercolour,film,blur,saturation,colourswap,washedout,posterise,colourpoint,colourbalance,cartoon
 
Metering Mode options :
average,spot,backlit,matrix

When the video image is captured, you will now want to display it.  On the Raspberry Pi, you can do this using omxplayer:

omxplayer video1.h264 or

omxplayer video_out.mp4

So - how do you get mp4 files?

METHOD 1: (Didn't work for me)
It is possible to convert the h264 video format into mp4 by using ffmpeg:

ffmpeg -r 30 -i video_in.h264 -vcodec copy video_out.mp4

This can also be done for mkv format as well as mp4 format.

I tried to show an example of the video images that are possible.   I recorded a 10 second h264 video, converted it to an mp4 file, but when I got it transferred to my PC from my Pi, the video upload on this blog kept failing....

METHOD 2: (Worked for me)
I eventually got a 5 second mp4 file to work, by trying a different approach - I did a

sudo apt-get install gpac

to install this package, and to convert the .h264 to .mp4 on the Pi (it actually puts the h264 file into an mp4 wrapper), I used the following from the gpac installation:

MP4Box -add filename.h264 filename.mp4

and transferred the resulting mp4 file to my PC by email (note the usual 25 MB limit could be a problem for long videos, so I will have to get some other method to do this kind of transfer).

Anyway - it worked!!  And here is the amazing result: 



What a gripping video!!  Now I have to find out how to get the big files from the Pi to the PC without using email.








1 comment:

  1. Looking good Da, I like the self portrait of the Pi! Have added the blog to my favoutites so I can keep up to speed!

    ReplyDelete