uoitlogo
Creating with Raspberry Pi
Raspberry Pie with cables

Interacting with the Physical World with a Pi Camera Board

    This tutorial will demonstrate how to use the Raspberry Pi Camera Board. It will cover the raspistill and raspivid programs. We will also stream video from the Camera to the host PC using VLC. Finally, we will use the OpenCV library to process a video feed. It was developed by Michael Lescisin under the supervision of Dr. Qusay H. Mahmoud
Raspberry Pi Camera board
Source: http://www.adafruit.com/images/1200x900/1367-00.jpg
The Raspberry Pi Camera module connects directly to the CSI connector inferface on the board. The camera performs quite well given its size and cost; it has a resolution of 5 megapixels and supports 1080p video.Source

You will need to install VLC Media Player for this tutorial here.
It can be downloaded here.

Component Quantity Description Where to Buy
Raspberry Pi 1 Canada, USA
SD card with Raspbian image installed 1 Canada, USA
5V USB Power Supply (a laptop or desktop will work for this) 1 - -
USB A micro Cable 1 Canada, USA
Ethernet Cable 1 Canada, USA
Raspberry Pi Camera Board 1 Sparkfun

Setting it up the Camera Board:

Initialize the Pi Camera module
Source: http://www.adafruit.com/images/1200x900/1367-01.jpg
    1. Connect the Camera Board to the Raspberry Pi. Carefully follow the instructions that came with the Camera Board.

    2. SSH into the Raspberry Pi (see the SSH tutorial for this) and run the command sudo raspi-config. Use the menu to select Enable Camera and select Enable.

    3. Try taking a picture. Point the camera at a subject of interest and run the command raspistill -o image_001.jpg Viewing the file image_001.jpg will display the picture that you just took.

Streaming Video to Host PC:

setting up the stream with VLC
    1. Install VLC on the Raspberry Pi by running the command sudo apt-get update and then sudo apt-get install vlc. You need to be connected to the Internet for this (see the Internet sharing tutorial for this).

    2. Start streaming the camera video feed by running the command: raspivid -o - -t 999999 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' :demux=h264

    3. On the host PC open VLC media player (UOIT laptops come with this). Right click and go to Open Media then Open Network. Enter the following string for the network URL: rtsp://192.168.137.2:8554/
Stream the Pi video on VLC
    4. You should now see the video stream on your laptop. Please be aware that the response time is slow.

Image Processing with OpenCV:

    1. Firstly, some packages need to be installed onto the Raspberry Pi. Remember this requires an Internet connection on the Raspberry Pi. Run the command sudo apt-get update to update the package cache and then run the command sudo apt-get install cmake libx11-dev libxt-dev libxext-dev libgraphicsmagick1-dev libcv-dev libhighgui-dev to install the required packages.

    2. Next, you will need to get the Raspberry Pi Userland Libraries. Create a directory in a good location, such as the Desktop, named git by running the command mkdir git . Within that directory create a directory called raspberrypi by switching into the git directory (cd git) and then run the command mkdir raspberrypi and switch into it, cd raspberrypi . Clone the Raspberry Pi git repository by running the command git clone https://github.com/raspberrypi/userland.git . Finally, switch into the userland directory (cd userland) and build the project by running the command ./buildme .

    3. Now return back to your git directory. Clone robidouille's git repository by running the command git clone https://github.com/robidouille/robidouille.git

    4. Switch into the raspicam_cv directory by running the command cd robidouille/raspicam_cv . In this directory create a directory called objs by running the command mkdir objs .

    5. Return back to your git directory and copy some files by running the commands shown on the left of this section(please note that each block of text corresponds to one line).
Stream the Pi video on VLC
    6. In the patches directory, from patches.zip open the file raspicam_cv/Makefile in a text editor and set USERLAND_ROOT (line 6) to equal the location of the userland directory. The current location is set to $(HOME)/camera/opencv/git/raspberrypi/userland .

    7. Upload the edited Makefile into the robidouille/raspicam_cv directory on the Raspberry Pi, replacing the old Makefile. Use WinSCP to do this, see the programming in C++ tutorial for instructions.

    8. In a PuTTY session connected to the Raspberry Pi, enter the robidouille/raspicam_cv directory and run the command make . If no errors are printed all has gone well.

    9. Open the graphical desktop via VNC (see VNC tutorial for this). Open LXTerminal and switch into the robidouille/raspicam_cv directory. Run the raspicamtest program by running the command ./raspicamtest . A window showing a video feed from the camera is displayed. Please note that the frame rate is very low. Press any key to exit the program.
Modifying images with OpenCV
    10. Create a directory new directory for a new OpenCV program. Name the directory opencv_brightness (mkdir opencv_brightness). Copy the objs directory into that directory either with WinSCP or with cp -r . Also copy RaspiCamCV.h and libraspicamcv.a into opencv_brightness .

    11. On your PC go into the patches/opencv_brightness directory and open Makefile in a text editor. Change USERLAND_ROOT to what you previously did in step 6.

    12. Upload the files brightness.cpp and Makefile into the opencv_brightness directory on the Raspberry Pi.

    13. Switch into the opencv_brightness directory on the Raspberry Pi (in a PuTTY session) and run the command make . If no errors occur all has gone well.

    14. In the graphical desktop on the Raspberry Pi, open LXTerminal and switch into the opencv_brightness directory. Run the program by calling ./brightness . Press any key to exit the program.

Considerations:

Study the source code of brightness.cpp to learn how OpenCV works. The official documentation for OpenCV can be found at http://docs.opencv.org/. Some great C++ tutorials with example source code for OpenCV can be found at http://opencv-srf.blogspot.ca/.