Main Content

Use Raspberry Pi Camera Board to Capture Images and Video

This example shows how to create a connection to the Camera Board, capture still images, and record video.

Create a connection to the Raspberry Pi® hardware using raspi.

mypi = raspi

Create a connection to the Camera Board and assign the connection to a handle, mycam. You can use Name-Value pairs to override the default values of most properties, like the Resolution property, shown here.

mycam = cameraboard(mypi,'Resolution','1280x720')
mycam = 

  cameraboard with properties:

                    Name: Camera Board   
              Resolution: '1280x720'       (View available resolutions)
                Rotation: 0               (0, 90, 180 or 270)
          HorizontalFlip: 0              
            VerticalFlip: 0              
               FrameRate: 30              (2 to 30)
               Recording: 0              

   Quality
              Brightness: 50              (0 to 100)
                Contrast: 0               (-100 to 100)
              Saturation: 0               (-100 to 100)
               Sharpness: 0               (-100 to 100)

   Exposure and AWB
            ExposureMode: 'auto'          (View available exposure modes)
    ExposureCompensation: 0               (-10 to 10)
                 AWBMode: 'auto'          (View available AWB modes)
            MeteringMode: 'average'       (View available metering modes)

   Effects
             ImageEffect: 'none'          (View available image effects)
      VideoStabilization: 'off'          
      ROI: [0.00 0.00 1.00 1.00] (0.0 to 1.0 [top, left, width, height])

Capture and display a sequence of ten snapshots on your host computer.

for ii = 1:10
img = snapshot(mycam);
    imagesc(img);
    drawnow;
end

Each of the 10 snapshots is the latest image captured by the camera.

If the image is upside down, change the orientation of the image.

mycam.Rotation = 180

Read eight images as a 4-D array with timestamp, with the oldest image being returned first.

[imagedata,ts] = readFrame(mypi,8);

You can change the values of many mycam properties listed in the "Name-Value Pair Arguments" for cameraboard.

Record a 10 second video.

record(mycam,'myvideo.h264',10)

Before the specified number of seconds have elapsed, you can stop recording video.

stop(mycam)

Copy the video from the board to your host computer.

getFile(mypi,'myvideo.h264','C:\MATLAB')

To free up space, delete the video from the board.

deleteFile(mypi,'myvideo.h264')