Step #1: Install dependencies
The first step is to update and upgrade any existing packages:
sudo apt–get update && sudo apt–get upgrade
We then need to install some developer tools, including CMake
, which helps us configure the OpenCV build process:
sudo apt–get install build–essential cmake pkg–config
Next, we need to install some image I/O packages that allow us to load various image file formats from disk. Examples of such file formats include JPEG, PNG, TIFF, etc.:
sudo apt–get install libjpeg–dev libtiff5–dev libjasper–dev libpng12–dev
Just as we need image I/O packages, we also need video I/O packages. These libraries allow us to read various video file formats from disk as well as work directly with video streams:
sudo apt–get install libavcodec–dev libavformat–dev libswscale–dev libv4l–dev
sudo apt–get install libxvidcore–dev libx264–dev
The OpenCV library comes with a sub-module named highgui which is used to display images to our screen and build basic GUIs. In order to compile the highgui module, we need to install the GTK development library:
sudo apt–get install libgtk2.0–dev libgtk–3–dev
Many operations inside of OpenCV (namely matrix operations) can be optimized further by installing a few extra dependencies:
sudo apt–get install libatlas–base–dev gfortran
These optimization libraries are especially important for resource constrained devices such as the Raspberry Pi.
Lastly, let’s install both the Python 2.7 and Python 3 header files so we can compile OpenCV with Python bindings:
sudo apt–get install python2.7–dev python3–dev
If you’re working with a fresh install of the OS, it is possible that these versions of Python are already at the newest version (you’ll see a terminal message stating this).
If you skip this step, you may notice an error related to the Python.h header file not being found when running make to compile OpenCV.
Step #2: Download the OpenCV source code
Now that we have our dependencies installed, let’s grab the 3.3.0 archive of OpenCV from the official OpenCV repository.. This version includes the dnn module which we discussed in a previous post where we did Deep Learning with OpenCV (Note: As future versions of openCV are released, you can replace 3.3.0 with the latest version number):
wget –O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip
We’ll want the full install
of OpenCV 3 .so we also need to grab the opencv_contrib
repository as well:
wget –O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip
Note: Make sure your opencv and opencv_contrib versions are the same (in this case,3.3.0). If the versions numbers do not match up, then you’ll likely run into either compile-time or runtime errors.
Before we can start compiling OpenCV on our Raspberry Pi 3, we first need to install pip , a Python package manager:
sudo python get–pip.py
Step #3: Compile and Install OpenCV
We are now ready to compile and install OpenCV!
sudo cmake –D CMAKE_BUILD_TYPE=RELEASE –D CMAKE_INSTALL_PREFIX=/usr/local –D INSTALL_PYTHON_EXAMPLES=ON \
–D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib–3.3.0/modules –D BUILD_EXAMPLES=ON ..
Now, before we move on to the actual compilation step, make sure you examine the output of CMake!
Start by scrolling down the section titled Python 2 and Python 3 .
If you are compiling OpenCV 3 for Python 2.7, then make sure your Python 2 section includes valid paths to the Interpreter , Libraries , numpy and packages path , similar to my screenshot below:
Notice how the Interpreter points to our python2.7 binary located in the cv virtual environment. The numpy variable also points to the NumPy installation in the cvenvironment.
Similarly, if you’re compiling OpenCV for Python 3, make sure the Python 3 section looks like the figure below:
Again, the Interpreter points to our python3.4 binary located in the cv virtual environment while numpy points to our NumPy install.
Finally, we are now ready to compile OpenCV:
sudo make –j4
Note: Compiling OpenCV in 72 minutes on the Raspberry Pi 3 is a 24% improvement over the previous 95 minutes for the Raspberry Pi 2. That extra 300MHz makes a big difference!
The –j4 command controls the number of cores to leverage when compiling OpenCV 3. The Raspberry Pi 3 has four cores, thus we supply a value of 4 to allow OpenCV to compile faster.
However, due to race conditions, there are times when make errors out when using multiple cores. If this happens to you, I suggest starting the compilation over again and using only onecore:
From there, all you need to do is install OpenCV 3 on your Raspberry Pi 3:
sudo make install
Step #4: Finish installing OpenCV on your Pi
We’re almost done — just a few more steps to go and you’ll be ready to use your Raspberry Pi 3 with OpenCV 3.
For Python 2.7:
Provided your Step #3 finished without error, OpenCV should now be installed in/usr/local/lib/python2.7/site–pacakges . You can verify this using the ls command:
ls –l /usr/local/lib/python2.7/site–packages/
–rw–r—r— 1 root staff 1895772 Mar 20 20:00 cv2.so
Note: In some cases, OpenCV can be installed in /usr/local/lib/python2.7/dist–packages (note the dist–packages rather than site–packages . If you do not find the cv2.so bindings in site–packages , we be sure to check dist–packages .
For Python 3:
After running make install , your OpenCV + Python bindings should be installed in/usr/local/lib/python3.4/site–packages . Again, you can verify this with the ls command:
ls –l /usr/local/lib/python3.4/site–packages/
–rw–r—r— 1 root staff 1895932 Mar 20 21:51 cv2.cpython–34m.so
Again, I’m not sure exactly why this happens, but it’s an easy fix. All we need to do is rename the file:
sudo mv cv2.cpython–34m.so cv2.so
Step #5: Testing your OpenCV 3 install
Congratulations, you now have OpenCV 3 installed on your Raspberry Pi 3 running Raspbian Jessie!
But before we pop the champagne and get drunk on our victory, let’s first verify that your OpenCV installation is working properly.