Install OpenCV on Raspberry Pi

I have used raspberry pi for ten months, I like the toy very much and learn a lot from it, you should know that I didn’t have any Linux experience before. At that time, my project is to develop a vision system for robot using raspberry pi, my first task was to install OpenCV library, because it provides so many fantastic function.

I studied how to install opencv and write a script(I didn’t know what is shell script before * . *), here is my script, opencv-2_4_2.sh

#!/bin/sh
#This script is to install OpenCV-2.4.2 on Raspberry Pi with Rasbian OS
#Please download(wget or copy from somewhere) OpenCV-2.4.2.tar.bz2 to /home/pi/ before installation
#It takes about 6 - 7 hours to install OpenCV
#Last changed on 25 Nov 2013
echo "Update and upgrade the current system and install some essential packages"
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential
sudo apt-get install cmake
sudo apt-get install pkg-config
sudo apt-get install libgtk2.0-dev python-dev python-numpy
sudo apt-get install build-essential cmake cmake-qt-gui pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools
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 libdc1394-22-dev libdc1394-22 libdc1394-utils swig libv4l-0 libv4l-dev python-numpy libpython2.6 python-dev python2.6-dev libgtk2.0-dev pkg-config
echo "Extract OpenCV source code"
cd /home/pi
tar xvjf OpenCV-2.4.2.tar.bz2
cd OpenCV-2.4.2
mkdir release
cd release
echo "Configure OpenCV build by running cmake"
cmake –D CMAKE_BUILD_TYPE=RELEASE –D CMAKE_INSTALL_PREFIX=/usr/local –D BUILD_ZLIB=ON –D BUILD_PYTHON_SUPPORT=ON ..
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_ZLIB=ON -D WITH_V4L=ON -D WITH_GSTREAMER=ON -D WITH_OPENEXR=ON -D WITH_UNICAP=ON -D BUILD_PYTHON_SUPPORT=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON ..
echo "Initialize the webcam"
cmake –D CMAKE_BUILD_TYPE=DEBUG -D WITH_CUDA=OFF ..
echo "Compile by using make, it takes about 5 hours"
make
echo "Install OpenCV libraries"
sudo make install
echo "Configuring OpenCV path"
sudo echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf
sudo ldconfig -v
sudo echo "PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig" >> /etc/bash.bashrc
sudo echo "export PKG_CONFIG_PATH" >> /etc/bash.bashrc
sync
echo "OpenCV 2.4.2 ready to be used!"
echo "Please Reboot your System using 'sudo reboot'"

When script is working, sometimes you need to type “yes” to confirm. Remember to download(wget or copy from somewhere) OpenCV-2.4.2.tar.bz2 to /home/pi/ before installation, installation time will be about 6 hours.

After installation, reboot your Pi firstly and you should test OpenCV:

$ cd /home/pi/OpenCV-2.4.2/samples/c
$ sudo chmod^+x^build_all.sh
$ ./build_all.sh
$ ./delaunay

If OpenCV works, you should see this picture:

test

I use Logitech C905 webcam as my vision camera, I test the camera using following capture.c(OpenCV, C), frame rate is about 10 frame per second(320×240):

#include <highgui.h>
#include <cv.h>
#include <time.h>
#include <stdio.h>

int main()
{

time_t start,end;

CvCapture* capture=cvCreateCameraCapture(0);
cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH,320);
cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT,240);

if(!capture){
            printf("Capture failure\n");
            return -1;
      }

cvNamedWindow("Camera",1);

time(&start);
int counter=0;
 
while(1)
{
    IplImage* frame =cvQueryFrame(capture);
    if(!frame) break;

    time(&end);
    ++counter;
    double sec=difftime(end,start);
    double fps=counter/sec;
    printf("\n%.2f",fps);

    cvShowImage("Camera",frame);

    if ( (cvWaitKey(10) & 255) == 27 ) break;
}

cvReleaseCapture(&capture);
cvDestroyAllWindows(); 
}

Here is my Makefile for this program:

LIBS= `pkg-config --libs opencv`
CFLAGS= `pkg-config --cflags opencv`
objects= capture.o

capture: $(objects)
    gcc $(LIBS)$(CFLAGS) -o capture $(objects)

.PHONY: clean
clean:
    rm capture $(objects)

Hope it is helpful for you!

Advertisements

3 thoughts on “Install OpenCV on Raspberry Pi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s