Part 1: Installing OpenCV 3.1.0 on Raspberry Pi Debian Jessy with Java Library

  • Posted on: 26 April 2016
  • By: robin
terminal  Installing OpenCV

The Raspberry Pi has become a Icon. I have nearly 10 Raspberry Pis and i love every single one of them. ;-) In December 2015 I started my Smart Mirror (Magic Mirror) Project and now its finally ready. Or better said the Hardware or the Smart Mirror is finally ready. What is now missing is a mind blowing Software. While searching for features a friend of my, Hendrik,  came up with a mind blowing idea. A absolute key feature for a Smart Mirror - Face recognition. (At this points dear Magic-Mirrors-Startups: In case you want to copy this idea now, act fairly enough and publish were you got this idea from). 

However, since it is not so easy, here it comes: How to install OpenCV 3.1.0 on a Raspberry Pi with Java Support.

If you have a fresh Debian Jessy Image on the PI (I use the March 2016 image) it should work perfectly when you follow the steps.


Step 1 - Required Packages:


So the first thing you want to do is installing all the necessary packages.

Most important:
sudo apt-get update && sudo apt-get install oracle-java7-jdk cmake ant
But also:
sudo apt-get install build-essential cmake pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools  libtiff4 libtiffxx0c2 libtiff-tools libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs libavcodec-dev   libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev  libunicap2 libunicap2-dev libdc1394-22-dev libdc1394-22 libdc1394-utils swig libv4l-0 libv4l-dev
Eazy !

Step 2 - Set Environment Variables:

It's such an annoying topic: Setting the JAVA_HOME Variable ! In our case we also need the ANT_HOME.
Here I want to teach you how you can find out by yourself - If you not interested jump to the .bashrc editing.
Quick Lesson:
So we have java and ant installed but the have no PATH set and we don't know which path we need to take.
The command "which" tells you the total path of a application. If we enter "which ant" we get "/usr/bin/ant". BUT this is not where ant is actually located. To find out we need "ls". "ls -l /usr/bin/ant" tells us more: "/usr/bin/ant -> ../share/ant/bin/ant". Jackpot - That the path we where locking for. The same for Java. "which java" leads us to "/usr/bin/java". And "ls -l /usr/bin/java" is pointing to "/etc/alternatives/java". Which is not what we are looking for because it is another link. So "ls -l /etc/alternatives/java" returns: "/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/bin/java". BINGO.
Now we need to edit the .bashrc file, set JAVA_HOME and ANT_HOME and also add it to the PATH variable. For this i use the nano editor.
"nano ~/.bashrc" (CTRL + V till you reach the very bottom of the file)
and than simply add this four lines:
export ANT_HOME=/usr/share/ant/
export PATH=${PATH}:${ANT_HOME}/bin
export JAVA_HOME=/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/
export PATH=$PATH:$JAVA_HOME/bin
Notice that the path is pointing to the JDK folder and not to the java application.
50% done. But first do a reboot. I mean you should really do it !
"sudo reboot

Step 3 - Downloading OpenCV 3.1.0:

I hope (for you) that this link is still alive when you do this tutorial:
mv 3.1.0
cd opencv-3.1.0/
You are now in extracted folder of opencv-3.1.0.

Step 4 - Compiling:

We want to use cmake to create a nice Makefile and build OpenCV from source code WITH the Java Library.
First create a build folder "mkdir build" and navigate into it "cd build"
Now simply take my cmake call and adjust Pathes if necessary.
Important are the flags "-D BUILD_SHARED_LIBS=OFF " and "-D BUILD_PERF_TESTS=OFF".
Hope this works.
Now - YES you could run the compilation process in paralled with this -j # flag. - However I had problems with this when compiling opencv and I would recommend to not use it. Even it will take like 2-3 h. :-/
If this HOPEFULLY ends successful you can install it with "make install".
To verify if the Java Library was generated check the folder "build/libs". There should be a file called "" and a "opencv-310.jar" in the folder "build/bin".
Congratulations! You did it. 
If you have problems: register - wait 24h till I accept your request and write a comment.
raspberry pi, opencv, open cv, magic mirror, smart mirror, pi, raspberry, face recognition, java, how to, tutorial