RaspberryPi 3 with Opencv
Can you believe it’s been over four years since the original Raspberry Pi model B was released? Back then the Pi Model B shipped with only 256MB of RAM and a 700MHz single core processor.
Just over one year ago the Raspberry Pi 2 was unleashed on the world. And man, for something called a “Pi”, this beast made an impact on the computer world like an asteroid. This board sported 1GB of RAM and a 900MHz quad-core processor — quite the upgrade from the original single core, 700MHz system!
In my opinion, the Raspberry Pi 2 is what made computer vision possible on the Pi platform (at least from a Python + OpenCV perspective). The original model B simply didn’t have the processing capacity (or the RAM) to be powerful enough to process images video streams for anything more than trivial operations — the Pi 2 changed all that.
In fact, the Raspberry Pi 2 had such a meaningful impact on the computer vision space, that I even took the time to make a all code examples in Practical Python and OpenCV compatible with the Pi.
And now we have the Raspberry Pi 3:
- 1.2Ghz 64-bit quad-core processor.
- 1GB RAM.
- Integrated 802.11n wireless and bluetooth.
Personally, I was hoping for a bit more RAM (perhaps in the range of 1.5-2GB). But upgrading to a 64-bit processor with 33% increased performance is well worth it.
Just as I have done in previous blog posts, I’ll be demonstrating how to install OpenCV 3 with Python bindings on Raspbian Jessie.
If you are looking for previous installation instructions for different platforms, please consult this list:
- How to install OpenCV 3.0 on Raspbian Jessie.
- Installing OpenCV on your Raspberry Pi Zero running Raspbian Jessie.
- Installing OpenCV 3.0 for both Python 2.7 and Python 3+ on Raspbian Wheezy.
- Install OpenCV 2.4 for Python 2.7 on Raspbian Wheezy.
Otherwise, let’s proceed with getting OpenCV 3 installed on your brand new Raspberry Pi 3!
You should also have either:
- Physical access to your Raspberry Pi 3 so that you can open up a terminal and execute commands.
- Remote access via SSH.
I’ll be doing the majority of this tutorial via SSH, but as long as you have access to a terminal, you can easily follow along.
Installing OpenCV 3 on a Raspberry Pi 3 running Raspbian Jessie
If you’ve ever installed OpenCV on a Raspberry Pi (or any other platform before), you know that the process can be quite time consuming with many dependencies and pre-requisites that have to be installed. The goal of this tutorial is to thus guide you step-by-step through the compile and installation process.
In order to make the installation process go more smoothly, I’ve included timings for each step so you know when to take a break, grab a cup of coffee, and checkup on email while the Pi compiles OpenCV. That said, the Pi 3 is substantially faster than the Pi 2, so the time it takes to compile OpenCV has decreased dramatically.
Anyway, let’s go ahead and get started installing OpenCV 3 on your brand new Raspberry Pi 3 running Raspbian Jessie.
Step #1: Expand filesystem
Are you using a brand new install of Raspbian Jessie?
If so, the first thing you should do is expand your filesystem to include all available space on your micro-SD card:
Once prompted, you should select the first option, “1. Expand File System”, hit Enter on your keyboard, arrow down to the “<Finish>” button, and then reboot your Pi:
After rebooting, your file system should have been expanded to include all available space on your micro-SD card. You can verify that the disk has been expanded by executing df -h and examining the output:
As you can see, my Raspbian filesystem has been expanded to include all 8GB of the micro-SD card.
However, even with my filesystem expanded, I have already used 48% of my 8GB card!
OpenCV, along with all its dependencies, will need a few gigabytes during the compile, so you should delete the Wolfram engine to free up some space on your Pi:
After removing the Wolfram Engine, you can reclaim almost 700mb!
Step #2: Install dependencies
This isn’t the first time I’ve discussed how to install OpenCV on the Raspberry Pi, so I’ll keep these instructions on the briefer side, allowing you to work through the installation process: I’ve also included the amount of time it takes to execute each command so you can plan your OpenCV + Raspberry Pi 3 install accordingly (OpenCV itself takes 1h 12m to compile).
The first step is to update and upgrade any existing packages:
Timing: 1m 26s
We then need to install some developer tools, including CMake, which helps us configure the OpenCV build process:
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.:
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:
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:
Timing: 3m 6s
Many operations inside of OpenCV (namely matrix operations) can be optimized further by installing a few extra dependencies:
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:
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 #3: Download the OpenCV source code
Now that we have our dependencies installed, let’s grab the 3.1.0 archive of OpenCV from the official OpenCV repository. (Note: As future versions of openCV are released, you can replace 3.1.0 with the latest version number):
Timing: 1m 26s
You might need to expand the command above using the “<=>” button during your copy and paste. The .zip in the 3.1.0.zip may appear to be cutoff in some browsers. The full URL of the OpenCV 3.1.0 archive is:
Note: Make sure your opencv and opencv_contrib versions are the same (in this case, 3.1.0 ). If the versions numbers do not match up, then you’ll likely run into either compile-time or runtime.
Step #4: Python 2.7 or Python 3?
Before we can start compiling OpenCV on our Raspberry Pi 3, we first need to install pip , a Python package manager:
If you’re a longtime PyImageSearch reader, then you’ll know that I’m a huge fan of both virtualenv and virtualenvwrapper. Installing these packages is not a requirement and you can absolutely get OpenCV installed without them, but that said, I highly recommend you install them as other PyImageSearch tutorials in the future will also leverage Python virtual environments. I’ll also be assuming that you have both virtualenv and virtualenvwrapper installed throughout the remainder of this guide.
So, given that, what’s the point of using virtualenv and virtualenvwrapper ?
First, it’s important to understand that a virtual environment is a special tool used to keep the dependencies required by different projects in separate places by creating isolated, independent Python environments for each of them.
In short, it solves the “Project X depends on version 1.x, but Project Y needs 4.x” dilemma. It also keeps your global site-packages neat, tidy, and free from clutter.
If you would like a full explanation on why Python virtual environments are good practice, absolutely give this excellent blog post on RealPython a read.
It’s standard practice in the Python community to be using virtual environments of some sort, so I highly recommend that you do the same:
Now that both virtualenv and virtualenvwrapper have been installed, we need to update our ~/.profile file to include the following lines at the bottom of the file:
In previous tutorials, I’ve recommended using your favorite terminal-based text editor such as vim , emacs , or nano to update the ~/.profile file. If you’re comfortable with these editors, go ahead and update the file to reflect the changes mentioned above.
Otherwise, you should simply use cat and output redirection to handle updating ~/.profile :