Hi Folks!!
In the previous blog post, we have seen how to program the raspi and arduino to get the analog output of distance sensor converted to a physical distance value. As a continuation of our project let us get some insights in to how to work with Pi cam and lets start programming. There are many tutorials on the Pi cam, but as we are interfacing our Raspi using Matlab, this tutorial gives us an entirety about Pi Cam and the gist of that tutorial is given as follows.
Hardware description about Pi Cam:
Firstly connect the Pi cam's ribbon cable to the CSI interface jack on the Raspberry Pi in such a manner that the blue label is facing towards the Ethernet connection jack. The Pi cam accommodates an OmniVision's OV5647 5MP CMOS image sensor which has an image resolution of 2592 x 1944 Pixels and pixel size of 1.4 µm x 1.4 µm. It accepts a Resolution '160x120', '320x240', '640x480', '800x600', '1024x768', '1280x720', '1920x1080' and generally it has an image transfer rate of QSXGA(2592 x 1944)-15fps, 1080p-30fps, 960p- 45fps, 720p-60fps, VGA(640x480)-90fps and QVGA(320x240)-120fps. Below is the detailed description of the various formats and modes in Pi cam.
Programming Pi cam:
The Pi cam, after necessary connections, should be assigned an object using cameraboard function.
clear rpi % Clears variable rpi
rpi = raspi();
cam = cameraboard(rpi, 'Resolution', '640 x 480'); %Creates an object to point to the Pi cam with given resolution
The above command sets the image transfer rate to 90 fps by default as it is VGA resolution. Now, to capture an image,
img = snapshot(cam); % capture an image
imagesc(img); %Image scale
drawnow; %Display it in a window
To view the properties of the Pi cam, just call the handle cam. And we can change these properties using the cam handle.
cam = cameraboard(rpi,'Resolution','1280x720')
cam =
Cameraboard with Properties:
Name: Camera Board
Resolution: '1280x720' (View available resolutions)
Quality: 10 (1 to 100)
Rotation: 0 (0, 90, 180 or 270)
HorizontalFlip: 0
VerticalFlip: 0
FrameRate: 30 (2 to 30)
Recording: 0
Picture Settings
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])
You can change the properties so easily with just one line of code......
cam.Rotation=180; % Rotates the image 180 degrees
% We can differentiate between Horizontal and vertical flip as follows:
cam.Horizontalflip = 'true';
% As seen in the above table of properties of Pi cam, we can just change any property using cam handle.
To record a video,
record(cam, 'vid.h264', 60); % Record a video named vid in h264 format for a peroid of 60 sec
getFile(rpi, 'vid.h264'); % Copy the file from Raspi to host computer
%The raw H264 stream needs to be converted to a video file format, such as MP4, before you can play it in a media player or load it in MATLAB. Install FFmpeg software.
ffmpegDir = 'C:\ffmpeg-20140218-git-61d5970-win32-static';
cmd = ['"' fullfile(ffmpegDir, 'bin', 'ffmpeg.exe') '" -r 30 -i vid.h264 -vcodec copy myvid.mp4 &'];
[status, message] = system(cmd);
% Now you can view the video in matlab as follows:
vid = VideoReader('myvid.mp4')
for k = 1:vid.NumberOfFrames
image(read(vid, k));
drawnow;
end
The next blog post involves the integration of Pi cam and Servo mechanism (Pan and Tilt) for the detection and tracking of an object of interest. See you in the next blog post.

Top Comments