Installing OpenCV 3.2 Computer Vision

Raspberry Pi 3 with Heatsinks

The following guide will take you though the steps to install OpenCV, an open source computer vision library on the Raspberry Pi 3 embedded computer running the Raspbian Jessie operating system.

Timings between steps have been included so you know the approximate time between steps. This also helps you know that the pi is not stuck if a step is suppose to take a significant amount of time to complete

If you are able please hook up a to a wired internet connection as this will speed up the process of downloading the rather large amount of files needed. Also I suggest adding a heat sink to the main processor, as during the compiling of OpenCV the raspberry will reach its thermal envelope thus slowing down the install process significantly.

The Raspberry pi 3 has the following improved features of the previous model. However it does maintain the same form factor and still sips the power.

  • A 1.2GHz 64-bit quad-core ARMv8 CPU
  • 802.11n Wireless LAN
  • Bluetooth 4.1
  • Bluetooth Low Energy (BLE)

Install time is approx 3-4 hours…

Step 1: Expanding the Filesystem

First lets expand the filesystem if you haven’t already. This step will free up the free available space on the micro SD card:

A  configuration screen will appear. Please go ahead and select the first option Expand File System, hit Enter, and use the arrow key to select the Finish button, and then restart your new Raspberry Pi:

You can verify that the step worked by executing df h  and looking at the updated space.

The Raspbian file system has now been expanded to include all 8GB of the micro SD card.

with the filesystem expanded, we have already used a significant portion of the 8GB card!

 

Step 2: Install all Dependencies

Please understand that OpenCV itself takes around 1h 11m to compile.

First update and upgrade existing packages:

Timing: ~1m 28s

Next we will install a few awesome developer tools, including CMake, CMake helps configure OpenCV to build process, Cmake is a very cool tool that will ensure the rather large install of OpenCV completes with no major surprises:

$ sudo apt-get install build-essential cmake pkg-config

Timing: ~40s

Next we will us image packages these will load image file formats when needed from the main disk. Examples of file formats include PNG, JPEG, TIFF and so forth.

Timing: ~30s

Video I/O packages will also be needed. This library help’s read various video file formats from both disk and work directly with live video streams:

Timing: ~30s

OpenCV has a module highgui this is used too display images to the screen and build basic GUI’s. However to compile the highgui module, we will need to install the GTK development library:

Timing: 3m

Next we will use a few dependencies that with help optimize OponCV, It is always good practice to use them when doing computer vision on weaker hardware such as the Pi.:

Timing: ~1m

These libraries are important as the Pi has limited resources. Remember a Pi is not a performance race car but rather a energy efficient hybrid.

Now we will install both Python 2.7 and Python 3 header files so that we can compile OpenCV with Python bindings, Use the following command to complete this step before moving on to downloading the source code for OpenCV.

Timing: ~1m

Do not skip this step as it will likely result in a error related to the Python.h  header file not being found when you compile OpenCV using make.

Step 3: Downloading the OpenCV source code.

All the dependencies should now be installed, go ahead and take the 3.2.0  archive of OpenCV from the official OpenCV repository. (Helpful Note: As newer versions of openCV become avalible, you can easily replace 3.2.0 with the latest matching version):

Timing: ~2m

We will be doing a full install of OpenCV. As this will allow access to SIFT and SURF, so we be installing a additional opencv_contrib repository as well:

Timing: ~1m

Below is the full link to the online archive if you have any problems with the code above.

https://github.com/Itseez/opencv_contrib/archive/3.2.0.zip

 

Next we will be installing pip , a well maintained Python package manager:

Go to the following link to Install a Python virtual environment.

Installing NumPy on your Raspberry Pi

Navigate to the cv  virtual environment if you are not in it currently.  Python has one dependency we need to install called NumPy, a nifty Python package used for numerical processing:

$ pip install numpy

Timing: ~10m

NumPy installation will take a long time to install. This is a good time to take a break or work on another project that you have been putting off. (Ya that one)

Helpful Note: While it may look frozen at times let it continue to process, there are many dependencies it needs to load. 

Step 4: Compile and Install OpenCV

We can now compile and install OpenCV! First make sure you are in the “cv” virtual environment we created earlier, if not, use the below command:

$ workon cv

Once you are in the “cv”  virtual environment, we can setup the build using CMake:

Timing: ~2m

We need to look though CMake to ensure that everything will install like we want start by scrolling down to the section titled Python 2.7  and Python 3 .

If you are using OpenCV 3 for Python 2.7,  make sure that the Python 2  section includes valid paths to the Interpreter , numpy, Libraries and the packages path, Same goes for Python 3 if you are going that route.

The Interpreter points to the python2.7  binary located in the cv  virtual environment we made earlier, and the numpy variable will point to the NumPy install.

You can now compile OpenCV (Computer Vision) on your Pi: \o/

 

Timing: ~1hr

Helpful Note: Compiling OpenCV computer vision software in approximately 72 minutes on the Raspberry Pi 3 which is a 24% improvement over the ~95 minutes for the Raspberry Pi 2. 

The j4  command controls the number of compute cores that will be used when compiling OpenCV 3. The Raspberry Pi 3 has four compute cores, so if we use a value of 4 it will allow OpenCV to compile much faster.

The below command will kick off the install of the OpenCV:

$ sudo make install
$ sudo ldconfig

 

Timing: ~1m

Step 5: Finish installing OpenCV on your Raspberry Pi

There only a few more steps needed to follow to finish out the install.

For Python 2.7:

Provided your Step 5 finished without error, OpenCV should now be installed in/usr/local/lib/python2.7/sitepackages . You can verify this using the ls  command:

Helpful Note: Once in a while OpenCV may be installed in /usr/local/lib/python2.7/distpackages  rather than sitepackages . If you do not find the cv 2.so  bindings in sitepackages , check distpackages instead.

In the final step we reference the OpenCV bindings into the previously created CV virtual environment for Python 2.7:

For Python 3:

After running make install , the Open-CV bindings should be installed in/usr/local/lib/python3.4/sitepackages . Go ahead and verify this with the ls (list Command) command:

$ cd /usr/local/lib/python3.4/site-packages/
$ sudo mv cv2.cpython-34m.so cv2.so

After renaming to cv2.so , we can sym-link our OpenCV bindings into the cv  virtual environment for Python 3.4:

$ cd ~/.virtualenvs/cv/lib/python3.4/site-packages/
$ ln -s /usr/local/lib/python3.4/site-packages/cv2.so cv2.so

Step 6: OpenCV 3.2 is now installed!

Congratulation’s, you now have OpenCV 3 installed on your Raspberry Pi 3.

Before we move on however let’s verify that OpenCV installed properly.

Open up a new terminal window, run the source and workon commands that you know and love, and then import the Python + OpenCV bindings:

Final cleanup time, now that OpenCV has been installed, you can remove both the opencv3.2.0,  andopencv_contrib3.2.0  directories to free up as much critical disk space on the SD card that you can.

Next we will move onto testing Open CV, face detection and face tracking using the official PiCamera.

Here is a link to the tutorial on Single image face detection with OpenCV

This install guide was largely based on the work of Adrian Rosebrock’s page found here.  Some parts were removed and simplified. I would suggest heading over to Adrian’s site http://www.pyimagesearch.com for more in-depth tutorials on using OpenCV with the Raspberry Pi.