First of all, building OpenCV on Raspbian (Raspberry Pi) will take at least 4 hours. So consider doing this before you sleep so you can leave it compiling overnight.
And also if you’re doing all this through SSH, linux’s “screen” program will definitely very useful. It’s not installed by default, so do this:
sudo apt-get install screen
Click here for short and quick tutorial of how to use screen.
Now let’s cook!
- Prepare fresh installed Rasbian on Raspberry Pi.
sudo apt-get update, then
sudo apt-get upgrade, to make sure everything is updated.
Copy and paste this into the terminal to install all the dependencies (NOTE: Some package names are altered, e.g. libavcodec52 is not available anymore, replaced by libavcodec53):
sudo apt-get -y install build-essential sudo apt-get -y install cmake sudo apt-get -y install pkg-config sudo apt-get -y install libpng12-0 libpng12-dev libpng++-dev libpng3 sudo apt-get -y install libpnglite-dev libpngwriter0-dev libpngwriter0c2 sudo apt-get -y install zlib1g-dbg zlib1g zlib1g-dev sudo apt-get -y install libjasper-dev libjasper-runtime libjasper1 sudo apt-get -y install pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-prog sudo apt-get -y install libavcodec53 libavcodec-dev libavformat53 libavformat-dev libavutil51 libavutil-dev libswscale2 libswscale-dev sudo apt-get -y install libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev sudo apt-get -y install libxine1-ffmpeg libxine-dev libxine1-bin sudo apt-get -y install libunicap2 libunicap2-dev sudo apt-get -y install libdc1394-22-dev libdc1394-22 libdc1394-utils sudo apt-get -y install swig sudo apt-get -y install python-numpy sudo apt-get -y install libpython2.6 python-dev python2.6-dev sudo apt-get -y install libjpeg-progs libjpeg-dev sudo apt-get -y install libgstreamer-plugins-base0.10-dev sudo apt-get -y install libqt4-dev libgtk2.0-dev
- We need to install other dependencies (x264, ffmpeg, and v4l) manually:
sudo apt-get remove ffmpeg x264 x264-dev wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/x264-snapshot-20120528-2245-stable.tar.bz2 tar -xvf x264-snapshot-20120528-2245-stable.tar.bz2 cd x264-snapshot-20120528-2245-stable/ ./configure --enable-shared --enable-pic make sudo make install cd .. wget http://ffmpeg.org/releases/ffmpeg-0.11.1.tar.bz2 echo "Installing ffmpeg" tar -xvf ffmpeg-0.11.1.tar.bz2 cd ffmpeg-0.11.1/ ./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab --enable-shared --enable-pic make sudo make install cd .. wget http://www.linuxtv.org/downloads/v4l-utils/v4l-utils-0.8.8.tar.bz2 tar -xvf v4l-utils-0.8.8.tar.bz2 cd v4l-utils-0.8.8/ make sudo make install cd ..
- Download desired version of OpenCV, in this example we’re using version 2.4.3. Unpack it anywhere you like.
tar -xjvf OpenCV-2.4.3.tar.bz2 rm OpenCV-2.4.3.tar.bz2 cd OpenCV-2.4.3/ mkdir build cd build/
- Then to create standard configuration just follow this command:
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON .. | sudo tee ./CMAKE.log
| sudo tee ./CMAKE.logis used for logging purpose. Normally I used redirection (>>) but it won’t work if you are trying to write inside a folder with no permission.
You can use
cmake-guiif it’s more comfortable for you, but it has to be run from a desktop environment (X Server is running). I did this over SSH so I don’t have the luxury of X.
- Then a configuration will be generated, make sure everything you need has no problem. For example see the first lines, it should be able to found the libraries needed, such as
In my case
linux/videodev.h. According to this source, apparently there’s a change of structure for libv4l but OpenCV didn’t take that into account. In my installation I found the file
/usr/local/include, so we need to make a symlink to the location searched by OpenCV installation:
sudo ln -s /usr/local/include/libv4l1-videodev.h /usr/include/linux/videodev.h
TODO: there’s also one missing library,
ffmpeg/avformat.h, but I’m not sure whether it’s necessary to fix or not.
- Now we’re ready to start the build. This process will take a long time to finish. So as said before, consider doing this before you sleep overnight. I do all this through SSH (I only have 1 keyboard and 1 mouse and I don’t want to back and forth plugging them between my laptop and the Raspberry Pi), so here’s where “screen” program is very useful. You can ignore this if you don’t use SSH:
screen -S OpenCV_Installation
Seems like nothing happened, but what actually happens is “screen” starts a new TTY/session, which you can build OpenCV, then you can “detach” it so it will run on background. After that you can quit the SSH session and turn off your laptop, without even bothering the building process.
Now change user into root
sudo su -
then execute this as root
make && make install
or if you want some logging
make | tee make.log && make install | tee make_install.log
- Now “detach” the compilation “screen” by pressing Ctrl+A then Ctrl+D consecutively. The building process will run in background. To check the process you have to get back to the “screen”, simply call in any session – either SSH or local
If you use the logging with
tee make_install.logas mentioned above, to check the status you can use
tail -f make.log
- Sit back, watch some movie, or sleep. See you in 5 hours!
POST INSTALLATION SETUP
Now that you have energy from your sleep, let’s continue setting up the system to complete the installation.
- We have to tell the system that there are libraries available. Create a file named
sudo touch /etc/ld.so.conf.d/opencv.conf
Then using your favorite editor add this line into the file:
sudo ldconfig /etc/ld.so.conf.d
- Then we have to add this 2 lines into the file /etc/bash.bashrc
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH
NOTE: Seems like my Raspberry Pi is unable to detect my cheap Logitech C170 yet. This is weird.
OpenCV comes with lots of pre-compiled sample programs, find it in
build/bin folder. Some of them are
kmeans. And if you have USB webcam you can check out
Press r then it will start tracking objects. Cool huh?
I also found this over here, to test camera capture using python.
import cv2 cv2.namedWindow("preview") vc = cv2.VideoCapture(0) if vc.isOpened(): # try to get the first frame rval, frame = vc.read() else: rval = False while rval: cv2.imshow("preview", frame) rval, frame = vc.read() key = cv2.waitKey(20) if key == 27: # exit on ESC break
Save it and run it with python:
For obvious reasons you can’t do this over SSH.