Last week we kicked-off the OpenCV 3.0 install fest by detailing how to install OpenCV 3.0 and Python 2.7+ on the OSX platform.
Today we are going to continue the OpenCV 3.0 install instruction series by moving over to the Ubuntu operating system.
In the remainder of the post I will provide instructions on how to configure and install OpenCV 3.0 and Python 2.7+ on Ubuntu. I have personally tested these instructions on Ubuntu 14.04, but they should pretty much work on any Debian-based operating system.
A quick note before we get started: Yes, OpenCV 3.0 is indeed compatible with Python 3+. However, the install instructions are slightly different between Python 2.7+ and Python 3+. In an effort to keep each article self-contained and easy to follow, I am creating separate OpenCV 3.0 install tutorials for Python 2.7 and Python 3+. If you would like to use OpenCV 3.0 and Python 3+ on your Ubuntu system, please keep an eye on this blog — I will be posting OpenCV 3.0 and Python 3+ install instructions later this month. But for the time being, let’s stick with Python 2.7.
How to Install OpenCV 3.0 and Python 2.7+ on Ubuntu
This is the second article in the OpenCV 3.0 install-fest series. Last week we covered how to install OpenCV 3.0 and Python 2.7+ on OSX. Today we are going to perform the same OpenCV 3.0 and Python 2.7 installation, only on the Ubuntu operating system. In general, you should find installing OpenCV 3.0 and Python 2.7+ on Ubuntu much easier than installing on OSX.
Step 1:
Open up a terminal and update the
apt-getpackage manager followed by upgrading any pre-installed packages:
$ sudo apt-get update $ sudo apt-get upgrade
Step 2:
Now we need to install our developer tools:
$ sudo apt-get install build-essential cmake git pkg-config
The
pkg-configis likely already installed, but be sure to include it just in case. We’ll be using
gitto pull down the OpenCV repositories from GitHub. The
cmakepackage is used to configure our build.
Step 3:
OpenCV needs to be able to load various image file formats from disk, including JPEG, PNG, TIFF, etc. In order to load these image formats from disk, we’ll need our image I/O packages:
$ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev
Step 4:
At this point, we have the ability to load a given image off of disk. But how do we display the actual image to our screen? The answer is the GTK development library, which the
highguimodule of OpenCV depends on to guild Graphical User Interfaces (GUIs):
$ sudo apt-get install libgtk2.0-dev
Step 5:
We can load images using OpenCV, but what about processing video streams and accessing individual frames? We’ve got that covered here:
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
Step 6:
Install libraries that are used to optimize various routines inside of OpenCV:
$ sudo apt-get install libatlas-base-dev gfortran
Step 7:
Install
pip, a Python package manager:
$ wget https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py
Step 8:
Install virtualenv and virtualenvwrapper. These two packages allow us to create separate Python environments for each project we are working on. While installing
virtualenvand
virtualenvwrapperis not a requirement to get OpenCV 3.0 and Python 2.7+ up and running on your Ubuntu system, I highly recommend it and the rest of this tutorial will assume you have them installed!
$ sudo pip install virtualenv virtualenvwrapper $ sudo rm -rf ~/.cache/pip
Now that we have
virtualenvand
virtualenvwrapperinstalled, we need to update our
~/.bashrcfile:
# virtualenv and virtualenvwrapper export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
This quick update will ensure that both
virtualenvand
virtualenvwrapperare loaded each time you login.
To make the changes to our
~/bashrcfile take affect, you can either (1) logout and log back in, (2) close your current terminal window and open a new one, or preferably, (3) reload the contents of your
~/.bashrcfile:
$ source ~/.bashrc
Lastly, we can create our
cvvirtual environment where we’ll be doing our computer vision development and OpenCV 3.0 + Python 2.7+ installation:
$ mkvirtualenv cv
Step 9:
As I mentioned above, this tutorial covers how to install OpenCV 3.0 and Python 2.7+ (I’ll have a OpenCV 3.0 + Python 3 tutorial available later this month), so we’ll need to install our Python 2.7 development tools:
$ sudo apt-get install python2.7-dev
Since OpenCV represents images as multi-dimensional NumPy arrays, we better install NumPy into our
cvvirtual environment:
$ pip install numpy
Step 10:
Our environment is now all setup — we can proceed to change to our home directory, pull down OpenCV from GitHub, and checkout the
3.0.0version:
$ cd ~ $ git clone https://github.com/Itseez/opencv.git $ cd opencv $ git checkout 3.0.0
As I mentioned last week, we also need the opencv_contrib repo as well. Without this repository, we won’t have access to standard keypoint detectors and local invariant descriptors (such as SIFT, SURF, etc.) that were available in the OpenCV 2.4.X version. We’ll also be missing out on some of the newer OpenCV 3.0 features like text detection in natural images:
$ cd ~ $ git clone https://github.com/Itseez/opencv_contrib.git $ cd opencv_contrib $ git checkout 3.0.0
Time to setup the build:
$ cd ~/opencv $ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D BUILD_EXAMPLES=ON ..
Notice how compared to last week our CMake command is substantially less verbose and requires less manual tweaking — this is because CMake is able to better automatically tune our install parameters (at least compared to OSX).
Now we can finally compile OpenCV:
$ make -j4
Where you can replace the 4 with the number of available cores on your processor to speedup the compilation.
Here’s an example of OpenCV 3.0 compiling on my system:
Assuming that OpenCV compiled without error, you can now install it on your Ubuntu system:
$ sudo make install $ sudo ldconfig
Step 11:
If you’ve reached this step without an error, OpenCV should now be installed in
/usr/local/lib/python2.7/site-packages
However, our
cvvirtual environment is located in our home directory — thus to use OpenCV within our
cvenvironment, we first need to sym-link OpenCV into the
site-packagesdirectory of the
cvvirtual environment:
$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/ $ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
Step 12:
Congratulations! You have successfully installed OpenCV 3.0 with Python 2.7+ bindings on your Ubuntu system!
To confirm your installation, simply ensure that you are in the
cvvirtual environment, followed by importing
cv2:
$ workon cv $ python >>> import cv2 >>> cv2.__version__ '3.0.0'
Here’s an example of demonstrating the OpenCV 3.0 and Python 2.7+ install on my own Ubuntu machine:
Step 13:
Now that OpenCV has been configured and installed, let’s build a quick Python script to detect the red game cartridge in the image named
games.jpgbelow:
Open up your favorite editor, create a new file, name it
find_game.py, and insert the following code:
# import the necessary packages import numpy as np import cv2 # load the games image image = cv2.imread("games.jpg") # find the red color game in the image upper = np.array([65, 65, 255]) lower = np.array([0, 0, 200]) mask = cv2.inRange(image, lower, upper) # find contours in the masked image and keep the largest one (_, cnts, _) = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) c = max(cnts, key=cv2.contourArea) # approximate the contour peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.05 * peri, True) # draw a green bounding box surrounding the red game cv2.drawContours(image, [approx], -1, (0, 255, 0), 4) cv2.imshow("Image", image) cv2.waitKey(0)
You’ll also need to download the games.jpg image and place it in the same directory as your
find_game.pyfile. Once the
games.jpgfile has been downloaded, you can execute the script via:
$ python find_game.py
Assuming that you have downloaded the
games.jpgimage and placed it in the same directory as our
find_game.pyscript, you should see the following output:
Notice how our script was able to successfully detect the red game cartridge in the right portion of the image, followed by drawing a green bounding box surrounding it.
Obviously this isn’t the most exciting example in the world — but it has demonstrated that we have OpenCV 3.0 with Python 2.7+ bindings up and running on our Ubuntu system!
Summary
To celebrate the OpenCV 3.0 release, I am working my way through OpenCV 3.0 and Python 2.7/Python 3.4 installation instructions on OSX, Ubuntu, and the Raspberry Pi.
Last week I covered how to install OpenCV 3.0 and Python 2.7+ on OSX.
And today we covered how to install OpenCV 3.0 with Python 2.7 bindings on Ubuntu. I have personally tested these instructions on my own Ubuntu 14.04 machine, but they should work on any Debian-based system.
Next week we’ll continue the install-fest and hop back to OSX — this time installing OpenCV 3.0 and Python 3!
This will be the first time we’ve used Python 3 on the PyImageSearch blog, so you won’t want to miss it!
And please consider subscribing to the PyImageSearch Newsletter by entering your email address in the form below. As we work through the OpenCV install-fest, I’ll be sending out updates as each new OpenCV 3.0 + Python install tutorial is released!
The post Install OpenCV 3.0 and Python 2.7+ on Ubuntu appeared first on PyImageSearch.