Skip to main content
dot-patterns opencv

Computer vision, OpenCV, Raspberry Pi, Webcams and CSI Raspicam

Before start to talk about the installation of openCV in a Raspberry Pi it’s important to clarify something:

  1. If you use a regular USB camera attached to the Raspberry Pi, the installation is basically the same as any other debian like distribution.
  2. If you use the Raspicam CSI camera, you must use UV4L (UserVideo4Linux) drivers and all will be a little bit tricky, and currently I didn’t tried yet to install the UV4L drivers to use the Raspicam CSI camera, but here are some resources about it*:

http://tothinkornottothink.com/post/59305587476/raspberry-pi-simplecv-opencv-raspicam-csi-camera

http://www.linux-projects.org/modules/sections/index.php?op=viewarticle&artid=14

http://www.raspberrypi.org/forums/viewtopic.php?f=33&t=61834

*NOTE: OpenCV works with raspicam following the instructions below (the same as the USB camera), but as far as I have been able to test, the video driver with raspicam has very low framerate and the scripts don’t run as good as with USB camera.

Installation

Ok, so, let’s continue with the installation of openCV in a debian-like distribution (Raspbian in particular):

The information below is not mine, it’s based on this great website about openCV and Raspberry Pi, I reproduce here the basic installation steps for record purposes only.

sudo aptget update

sudo aptget upgrade

#check to see that your webcam, or pi-cam is being read by your pi:

lsusb

#you can test it by downloading the following

sudo aptget install guvcview

#and enter the following command:

guvcview

#for easy package installation you can download synaptic

sudo aptget install synaptic

#install the python scipy stack

sudo aptget install pythonnumpy pythonscipy pythonmatplotlib pythonnotebook pythonpandas pythonsympy pythonnose

#to get the OpenCV zip or the tar.bz2 or the tar.gz:

wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip

#install basic development environment

sudo aptget install buildessential cmake pkgconfig

sudo aptget install defaultjdk ant

sudo aptget install libgtkglext1dev

sudo aptget install bison

sudo aptget install qt4devtools libqt4dev libqt4core libqt4gui

sudo aptget install v4lutils

sudo aptget install qtcreator           (you do not really have to have it)

#not on the original video tutorial

#more raspberry pi specific opencv-related dependencies that will make your life much easier

sudo apt-get install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev swig libv4l-0 libv4l-dev python-numpy libpython2.7 python-dev python2.7-dev libgtk2.0-dev

#ready to start compiling opencv

#now extract the opencv folder by unzipping the zip file you downloaded from Sourceforge

unzip opencv2.4.9.zip

#change directory to the folder that you just extracted

cd opencv2.4.9.1

#create a directory called build

mkdir build

#change into that recently created directory

cd build

#very important step to create the flags and instructions for the compilation process

cmake D CMAKE_BUILD_TYPE=RELEASE D INSTALL_C_EXAMPLES=ON D INSTALL_CPP_EXAMPLES=ON D INSTALL_PYTHON_EXAMPLES=ON D BUILD_EXAMPLES=ON D WITH_QT=ON D CMAKE_INSTALL_PREFIX=/usr/local D WITH_OPENGL=ON D WITH_V4L=ON ..

#now you are ready for make

sudo make

#and now you are ready to install  ***very important make sure you include sudo with the command

sudo make install

#create the following file:

sudo nano /etc/ld.so.conf.d/opencv.conf

#enter the following line into the empty file:

/usr/local/lib

#after saving the file, enter the following command:

sudo ldconfig

#open your bash.bashrc file by entering the command:

sudo nano /etc/bash.bashrc

#in your bash.bashrc file enter the following two lines at the end of the document:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig

export PKG_CONFIG_PATH

#save it and close the terminal, and log back in by opening a new terminal to make sure your changes to the bash.bashrc file take effect.

#Testing to make sure that everything is working properly:

#open the c samples directory:

cd ~/OpenCV-2.4.9/samples/c

chmod +x build_all.sh

./build_all.sh

#find an executable and run it by entering the following:

./facedetect

#if you want to test the python, go to the python directory and enter the following:

#python name_of_the_file.py

e.g   python facedetect.py

#and it will run.

Tutorial video

There is also a great video tutorial from the same guys:

The final test

Edge python script running 🙂

OpenCV edge script running
OpenCV edge script running

Leave a Reply

%d bloggers like this: