Skip to content

Instantly share code, notes, and snippets.

@chatchavan
Last active February 17, 2021 12:47
Show Gist options
  • Save chatchavan/990d3c0a5b085dc7bae1 to your computer and use it in GitHub Desktop.
Save chatchavan/990d3c0a5b085dc7bae1 to your computer and use it in GitHub Desktop.
Setup OpenCV 2 + OpenNI 1 on Raspberry Pi Wheezy

Requirements

  • Raspberry Pi
  • Raspbian wheezy
  • Access to shell, e.g., via SSH

Initialze Raspbian and utilities for easy access

Note the MANUAL operations below

sudo raspi-config   # MANUAL: expand disk, set password, set locale to US, change the network name
sudo nano /etc/ssh/ssh_config   # MANUAL: comment out SendEnv LANG LC...
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install netatalk # apple AFP
mkdir .ssh
nano .ssh/authorized_keys # MANUAL: adds public key authentication for password-less login

Upgrade gcc to 4.8

Source

  1. Allow apt-get to use source from jessie by changing the content of /etc/apt/sources.list
sudo nano /etc/apt/sources.list 

to:

deb http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi
deb http://archive.raspbian.org/raspbian wheezy main contrib non-free rpi
# Source repository to add
deb-src http://archive.raspbian.org/raspbian wheezy main contrib non-free rpi
deb http://mirrordirector.raspbian.org/raspbian/ jessie main contrib non-free rpi
deb http://archive.raspbian.org/raspbian jessie main contrib non-free rpi
# Source repository to add
deb-src http://archive.raspbian.org/raspbian jessie main contrib non-free rpi
  1. Add /etc/apt/preferences
sudo nano /etc/apt/preferences

with the following content

Package: *
Pin: release n=wheezy
Pin-Priority: 900
Package: *
Pin: release n=jessie
Pin-Priority: 300
Package: *
Pin: release o=Raspbian
Pin-Priority: -10
  1. Update apt-get and install gcc
sudo apt-get update
sudo apt-get install -t jessie gcc-4.8 g++-4.8
  1. change links
sudo update-alternatives --remove-all gcc 
sudo update-alternatives --remove-all g++
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 20
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50

Libraries and tools

  1. developer tools
sudo apt-get -y install build-essential cmake pkg-config cmake-curses-gui htop
  1. dependencies required by OpenCV
sudo apt-get -y install libpng12-0 libpng12-dev libpng++-dev libpng3 \
libpnglite-dev \
zlib1g-dbg zlib1g zlib1g-dev \
pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools \
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 \
libv4l-0 libv4l-dev \
libdc1394-22-dev libdc1394-22 libdc1394-utils \
libatlas-base-dev gfortran swig zlib1g-dbg zlib1g zlib1g-dev
  1. dependencies required by OpenNI
sudo apt-get -y install libusb-1.0 doxygen freeglut3-dev openjdk-6-jdk graphviz
  1. python
sudo apt-get -y install libpython2.7 python-dev python2.7-dev python-numpy python-pip

OpenNI

According to my test, the compiler optimization below doubles the framerate of Xtion from 8 FPS to 18 FPS!

  1. Clone source
git clone https://github.com/OpenNI/OpenNI.git -b unstable
git clone https://github.com/PrimeSense/Sensor.git -b unstable
  1. Enable compiler optimization for OpenNI specific to Raspberry Pi 2
sudo nano OpenNI/Platform/Linux/Build/Common/Platform.Arm

Replace

CFLAGS += -march=armv7-a -mtune=cortex-a8 -mfpu=neon -mfloat-abi=softfp #-mcpu=cortex-a8

with

CFLAGS += -march=native  -mfpu=neon-vfpv4 -mfloat-abi=hard
  1. Enable compiler optimization for Sensor specific to Raspberry Pi 2
sudo nano ~/Sensor/Platform/Linux/Build/Common/Platform.Arm

Replace

CFLAGS += -march=armv7-a -mtune=cortex-a8 -mfpu=neon -mfloat-abi=softfp #-mcpu=cortex-a8

with

CFLAGS += -march=native  -mfpu=neon-vfpv4 -mfloat-abi=hard
  1. Install
cd ~/OpenNI/Platform/Linux/CreateRedist/
./RedistMaker.Arm
cd ~/OpenNI/Platform/Linux/Redist/OpenNI-Bin-Dev-Linux-Arm-v1.5.8.5   # TODO: version number may change
sudo ./install.sh

cd ~/Sensor/Platform/Linux/CreateRedist/
./RedistMaker Arm
cd ~/Sensor/Platform/Linux/Redist/Sensor-Bin-Linux-Arm-v5.1.6.5  # TODO: version number may change
sudo ./install.sh

Open CV

wget https://github.com/Itseez/opencv/archive/2.4.10.tar.gz
tar -xzf 2.4.10.tar.gz
cd opencv-2.4.10
mkdir build
cd build

sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
   -D CMAKE_INSTALL_PREFIX=/usr/local \
   -D BUILD_NEW_PYTHON_SUPPORT=ON \
   -D WITH_OPENNI=ON \
   -D ENABLE_NEON=ON -D ENABLE_VFPV3=ON \
   .. 

These are configuration I've got

--     Linker flags (Release):      
--     Linker flags (Debug):        
--     Precompiled headers:         YES
-- 
--   OpenCV modules:
--     To be built:                 core flann imgproc highgui features2d calib3d ml video legacy objdetect photo gpu ocl nonfree contrib python stitching superres ts videostab
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 androidcamera dynamicuda java viz
-- 
--   GUI: 
--     QT:                          NO
--     GTK+ 2.x:                    NO
--     GThread :                    YES (ver 2.40.0)
--     GtkGlExt:                    NO
--     OpenGL support:              NO
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/arm-linux-gnueabihf/libz.so (ver 1.2.7)
--     JPEG:                        /usr/lib/arm-linux-gnueabihf/libjpeg.so (ver 80)
--     PNG:                         /usr/lib/arm-linux-gnueabihf/libpng.so (ver 1.2.49)
--     TIFF:                        /usr/lib/arm-linux-gnueabihf/libtiff.so (ver 42 - 3.9.6)
--     JPEG 2000:                   build (ver 1.900.1)
--     OpenEXR:                     build (ver 1.7.1)
-- 
--   Video I/O:
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  YES (ver 2.2.0)
--     FFMPEG:                      NO
--       codec:                     YES (ver 54.35.0)
--       format:                    YES (ver 54.20.4)
--       util:                      YES (ver 52.3.0)
--       swscale:                   NO
--       gentoo-style:              YES
--     GStreamer:                   NO
--     OpenNI:                      YES (ver 1.5.8, build 5)
--     OpenNI PrimeSensor Modules:  YES (/usr/lib/libXnCore.so)
--     PvAPI:                       NO
--     GigEVisionSDK:               NO
--     UniCap:                      NO
--     UniCap ucil:                 NO
--     V4L/V4L2:                    Using libv4l1 (ver 1.0.0) / libv4l2 (ver 1.0.0)
--     XIMEA:                       NO
--     Xine:                        NO
-- 
--   Other third-party libraries:
--     Use IPP:                     NO
--     Use Eigen:                   NO
--     Use TBB:                     NO
--     Use OpenMP:                  NO
--     Use GCD                      NO
--     Use Concurrency              NO
--     Use C=:                      NO
--     Use Cuda:                    NO
--     Use OpenCL:                  YES
-- 
--   OpenCL:
--     Version:                     dynamic
--     Include path:                /home/pi/opencv-2.4.10/3rdparty/include/opencl/1.2
--     Use AMD FFT:                 NO
--     Use AMD BLAS:                NO
-- 
--   Python:
--     Interpreter:                 /usr/bin/python2 (ver 2.7.3)
--     Libraries:                   /usr/lib/libpython2.7.so (ver 2.7.3)
--     numpy:                       /usr/lib/pymodules/python2.7/numpy/core/include (ver 1.6.2)
--     packages path:               lib/python2.7/dist-packages
-- 
--   Java:
--     ant:                         NO
--     JNI:                         NO
--     Java tests:                  NO
-- 
--   Documentation:
--     Build Documentation:         NO
--     Sphinx:                      NO
--     PdfLaTeX compiler:           /usr/bin/pdflatex
-- 
--   Tests and samples:
--     Tests:                       YES
--     Performance tests:           YES
--     C/C++ Examples:              NO
-- 
--   Install path:                  /usr/local
-- 
--   cvconfig.h is in:              /home/pi/opencv-2.4.10/build
-- -----------------------------------------------------------------

Install

sudo make; sudo make install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment