Installing OpenCV in Ubuntu for Python 3

Installing OpenCV in linux for Python 3 is not that straight forward, so in this tutorial, we’ll go through the steps on how do we do it. We’ll be using a 64 bit Ubuntu 16.04 system, and Python 3.5 in this tutorial. (It should also work for Ubuntu 12.04++ onwards and Python 3.x)

cv3_tut

(While copying the code over, do not include the >>> in the terminal: It’s just used to represent the python input.)

Packages Required

Before compiling OpenCV, we have to download some packages for compilation.

Update the repository before installing the necessary packages.

Once the update is done, we’ll proceed on to downloading the required packages to compile OpenCV.

  • GCC
  • CMake
  • Git
  • GTK+2.x or higher, including headers (libgtk2.0-dev)
  • pkg-config
  • ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev

Next, we will install the developer packages for python3. If you are using a different version of python, change it to python3.x-dev. (E.g. If you are using Python 3.3, use this instead: python3.3-dev)

 

Copying the Python dev file

(If you using Python 3.2  or below, you can skip to the next step.)

As the pycofig.h header file is not in the typical place, we would have to copy the file to the expected location. Run the following code below:

The output would look something similar below:

The first part of the output is the expected location & the second part shows the current location of the config file. To solve this problem, we’ll copy the file from the current location to the expected location.

 

Downloading the OpenCV Source Code

cv_git

Make a new directory OpenCV-tmp in your Desktop, then change the directory into OpenCV-tmp. We will be using cloning the OpenCV source from Github into this folder.

A opencv directory will be created in OpenCV-tmp. Rename opencv to opencv-3.

 

Building

cv_cmake

Before compiling the source, we have to generate the make file, so we will be using cmake.

Create a new directory build, which we will build the makefile in. Change the directory into it.

After that, run the cmake utility:

If the command above does not work, you can try this alternative command: (Thanks to supermdguy for pointing this out!)

 

After the makefile is generated, we will build the installation files:

This may take some time depending on your computer.

Side note: We’ll split the make task via the -j argument, and the $(nproc -all) bash command to use all the processors available. Say if you want to only use 2 of the processors to run the make command, the command would look like this:

 

Installing

Once the build is complete, we’ll install OpenCV by running the make file:

 

Checking

cv_test

After OpenCV have finished installing, we have to check to ensure that OpenCV have been properly installed .

To do this, we’ll enter Python 3 and import cv2, and also check it’s version. If there are no errors, the output should look something like this:

With this, you have successfully installed OpenCV for Python 3!

 

Python Libraries

Here are some of the python libraries that are commonly used in conjunction with OpenCV:

Numpy: Fundamental package for scientific computing

Matplotlib: 2D Plotting Library

 

Troubleshooting

ImportError: No module named ‘cv2’

  • Did you miss the “Copying the Python dev file” step? (If you are running Python3.3+)?
  • Make sure that you downloaded the correct python-dev packages. If you are not running Python 3.5, please change the dev packages accordingly. (E.g. Python 3.4 would be python3.4-dev)
  • Recompile the makefile of the build directory. Make sure that you delete everything in the build directory before recompiling the makefile.
  • If it still does not work, try reinstalling the required packages.

 

References

Additional Information:

  • https://www.scivision.co/anaconda-python-opencv3/
Share this post:

You may also like...

6 Responses

  1. supermdguy says:

    Thanks, this is really helpful. For me, it didn’t work to run the cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ../opencv-3 command. I had to change it to `cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..’

    Other than that, it’s working great.

  2. Pablo_RodAgua says:

    Can you put the previous tutorial, this one doesn’t work for me! :/

  3. Muhammad_Abdullah says:

    Hi, I completed the tutorial but still got this problem.
    Python 3.5.2 (default, Nov 17 2016, 17:05:23)
    [GCC 5.4.0 20160609] on linux
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type