How to install OpenCV 4 on Ubuntu

How to install OpenCV 4 on Ubuntu

In this blog post, I’ll walk you through the six steps to install OpenCV 4 on your Ubuntu system.

I’ll also cover some Frequently Asked Questions (FAQs) that will help you troubleshoot in the event you run into an error message. I strongly urge you to read the FAQ prior to submitting a comment on this post.

Before we begin, you probably have two burning questions:

1. Which version of Ubuntu OS should I use with OpenCV 4?

Today I’ll be installing OpenCV 4 on Ubuntu 18.04. I’ve also tested these instructions with Ubuntu 16.04 as well.

If you intend on using this machine for deep learning I might suggest you use Ubuntu 16.04 as the GPU drivers are more mature. If you intend on using this machine just for OpenCV and other computer vision tasks, Ubuntu 18.04 is perfectly fine.

2. Should I use Python 2.7 or Python 3 with OpenCV 4?

Python 3 has become the standard and I highly recommend you install Python 3 with OpenCV 4.

That being said, if you’re making the conscious choice to use Python 2.7, you can of course follow these instructions, just be sure you take care to install Python 2.7 development headers and libraries as well as specifying Python 2.7 when creating your virtual environment. Everything else will be the same. Refer to the FAQs down below if you need pointers for Python 2.7.

Let’s begin.

Step #1: Install OpenCV 4 dependencies on Ubuntu

I’ll be using Ubuntu 18.04 to install OpenCV 4 with Python 3 bindings on my machine.

To get the OpenCV 4 install party started, fire up your Ubuntu machine and open a terminal. Alternatively, you may SSH into the box for the install portion.

From there, let’s update our system:

$ sudo apt-get update
$ sudo apt-get upgrade

And then install developer tools:

$ sudo aptget install buildessential cmake unzip pkgconfig

Next, let’s install a handful of image and video I/O libraries.

$ sudo aptget install libjpegdev libpngdev libtiffdev
$ sudo aptget install libavcodecdev libavformatdev libswscaledev libv4ldev
$ sudo aptget install libxvidcoredev libx264dev

These libraries enable us to load image from disk as well as read video files.

From there, let’s install GTK for our GUI backend:

$ sudo aptget install libgtk3dev

Followed by installing two packages which contain mathematical optimizations for OpenCV:

$ sudo aptget install libatlasbasedev gfortran

And finally, let’s install the Python 3 development headers:

$ sudo aptget install python3dev

Once you have all of these prerequisites installed you can move on to the next step.

Step #2: Download OpenCV 4

Our next step is to download OpenCV.

Let’s navigate to our home folder and download both opencv and opencv_contrib. The contrib repo contains extra modules and functions which we frequently use here on the PyImageSearch blog. You should be installing the OpenCV library with the additional contrib modules as well.

When you’re ready, just follow along to download both the opencv  and opencv_contrib  code:

$ cd ~
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.1.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.1.zip

From there, let’s unzip the archives:

$ unzip opencv.zip
$ unzip opencv_contrib.zip

I also like to rename the directories:

$ mv opencv-4.0.0 opencv
$ mv opencv_contrib-4.0.0 opencv_contrib

If you skip renaming the directories, don’t forget to update the CMake paths.

Now that opencv  and opencv_contrib  are downloaded and ready to go, let’s set up our environment.

Step #3: Configure your Python 3 virtual environment for OpenCV 4

Let’s install pip, a Python Package Manager.

To install pip, simply enter the following in your terminal:

$ wget https://bootstrap.pypa.io/getpip.py
$ sudo python3 getpip.py

Make use of virtual environments for Python development

Python virtual environments allow you to work on your Python projects in isolation. They are a best practice for Python development.

For example, maybe you have a Python + OpenCV project that requires an older version of scikit-learn (v0.14), but you want to keep using the latest version of scikit-learn (0.19) for all of your newer projects.

Using virtual environments, you could handle these two software version dependencies separately, something that is not possible using just the system install of Python.

If you would like more information about Python virtual environments take a look at this article on RealPython or read the first half of the this blog post on PyImageSearch.

Note: My preferred way to work with Python virtual environment is via the  virtualenv  and virtualenvwrapper  packages; however if you are more familiar with conda or PyEnv, feel free to use them and skip this part.

Let’s go ahead and install   virtualenv  and virtualenvwrapper  — these packages allow us to create and manage Python virtual environments:

$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/get-pip.py ~/.cache/pip

To finish the install of these tools, we need to update our  ~/.bashrc  file.

Using a terminal text editor such as vi / vim  or nano , add the following lines to your ~/.bashrc :

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

Alternatively, you can append the lines directly via bash commands:

$ echo -e “\n# virtualenv and virtualenvwrapper” >> ~/.bashrc
$ echo “export WORKON_HOME=$HOME/.virtualenvs” >> ~/.bashrc
$ echo “export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3” >> ~/.bashrc
$ echo “source /usr/local/bin/virtualenvwrapper.sh” >> ~/.bashrc

Next, source the ~/.bashrc  file:

$ source ~/.bashrc

Create a virtual environment to hold OpenCV 4 and additional packages

Now we’re at the point where we can create your OpenCV 4 + Python 3 virtual environment:

$ mkvirtualenv cv -p python3

This command simply creates a Python 3 virtual environment named cv .

You can (and should) name your environment(s) whatever you’d like — I like to keep them short and sweet while also providing enough information so I’ll remember what they are for. For example, I like to name my environments like this:

  • py3cv4
  • py3cv3
  • py2cv2
  • etc.

Here my py3cv4  virtual environment can be used for Python 3 + OpenCV 4. My py3cv3  virtual environment is used for Python 3 and OpenCV 3. And my py2cv2  environment can be used to test legacy Python 2.7 + OpenCV 2.4 code. These virtual environment names are easy to remember and allow me to switch between OpenCV + Python versions (nearly) seamlessly.

Let’s verify that we’re in the cv  environment by using the workon  command:

$ workon cv

Figure 1: We use the workon command to activate our cv virtual environment on Ubuntu prior to installing NumPy and OpenCV 4.

Install NumPy

The first package and only Python prerequisite we’ll install is NumPy:

$ pip install numpy

We can now prepare OpenCV 4 for compilation on our Ubuntu machine.

Step #4: CMake and compile OpenCV 4 for Ubuntu

For this step, we’re going to setup our compile with CMake followed by running make  to actually compile OpenCV. This is the most time-consuming step of today’s blog post.

Navigate back to your OpenCV repo and create + enter a build  directory:

$ cd ~/opencv
$ mkdir build
$ cd build

Run CMake for OpenCV 4

Now let’s run CMake to configure the OpenCV 4 build:

Update 2018-11-26: Notice the D OPENCV_ENABLE_NONFREE=ON  flag. Setting this flag with OpenCV 4 ensures that you’ll have access to SIFT/SURF and other patented algorithms.

Be sure to update the above command to use the correct OPENCV_EXTRA_MODULES_PATH  and  PYTHON_EXECUTABLE  in your virtual environment that you’re working in. If you’re using the same directory structure Python virtual environment names these paths should notneed to be updated.

Once CMake finishes, it’s important that you inspect the output. Your output should look similar to mine below:

Figure 2: It is critical to inspect your CMake output when installing OpenCV 4 on Ubuntu prior to kicking off the compile process.

Take a second now to ensure that the Interpreter  points to the correct Python 3 binary. Also check that numpy  points to our NumPy package which is installed inside the virtual environment.

Figure 3: Be sure to check the CMake output to ensure that the “Non-free algorithms” will be installed.

Compile OpenCV 4

Now we’re ready to compile OpenCV 4:

$ make j4

Note: In the make  command above, the j4  argument specifies that I have 4 cores for compilation. Most systems will have 2, 4, or 8 cores. You should update the command to use the number of cores on your processor for a faster compile. If you encounter compilation failures, you could try compiling with 1 core to eliminate race conditions by skipping the optional argument altogether.

Here you can see OpenCV 4 has compiled without any errors:

Figure 4: When compiling OpenCV 4 for Ubuntu, once the make output reaches 100% we can move on.

And from there, let’s install OpenCV 4 with two additional commands:

$ sudo make install
$ sudo ldconfig

Step #5: Link OpenCV 4 into your Python 3 virtual environment

Before we make a symbolic link to link OpenCV 4 into our Python virtual environment, let’s determine our Python version:

$ workon cv
$ python –version
Python 3.5

Using the Python version, we can easily navigate to the correct sitepackages  directory next (although I do recommend tab-completion in the terminal).

Update 2018-12-20: The following paths have been updated. Previous versions of OpenCV installed the bindings in a different location ( /usr/local/lib/python3.5/sitepackages ), so be sure to take a look at the paths below carefully.

At this point, your Python 3 bindings for OpenCV should reside in the following folder:

$ ls /usr/local/python/cv2/python-3.5
cv2.cpython-35m-x86_64-linux-gnu.so

Let’s rename them to simply cv2.so :

$ cd /usr/local/python/cv2/python-3.5
$ sudo mv cv2.cpython-35m-x86_64-linux-gnu.so cv2.so

Pro-tip: If you are installing OpenCV 3 and OpenCV 4 alongside each other, instead of renaming the file to cv2.so , you might consider naming it cv2.opencv4.0.0.so  and then in the next sub-step sym-link appropriately from that file to cv2.so  as well.

Our last sub-step is to sym-link our OpenCV cv2.so  bindings into our cv  virtual environment:

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

Step #6: Test your OpenCV 4 install on Ubuntu

Let’s do a quick sanity test to see if OpenCV is ready to go.

Open a terminal and perform the following:

$ workon cv
$ python
>>> import cv2
>>> cv2.__version__
‘4.0.0’
>>> quit()

The first command activates our virtual environment. Then we run the Python interpreter associated with the environment.

Note: It is not necessary to specify python3  as Python 3 is the only Python executable in the environment.

If you see that you have version 4.0.0 installed, then a “Congratulations!” is in order. Have a swig of your favorite beer or bourbon and let’s get to something more fun than installing libraries and packages.

推荐文章

沪公网安备 31010702002009号