How to install OpenCV 3.4.0 on Ubuntu 16.04

Install OpenCV 3.4.0 on Ubuntu 16.04 for all versions of python (2.7, 3.5 and 3.6)

OpenCV is a most popular free and open-source computer vision library among students, researchers, and developers alike. It has  C, C++, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS, and Android. At the time of writing of this blog, the latest version of OpenCV is 3.4.0. This tutorial is designed to help you install OpenCV 3.4.0 on Ubuntu 16.04. This tutorial works for versions 2.7, 3.5 and 3.6 of python. If you want to know how to install python 3.6 on Ubuntu, check out our other tutorial Install Python 3.6 on Ubuntu.

You can install opencv by using official prebuilt pip package or build from source.

To install from official pip package:

Use following commands in a new active empty virtual environment.

To install Opencv Only:

pip install opencv-python

To add contrib package in opencv:

pip install opencv-contrib-python

To install opencv by building from source:

OpenCV can be used in python as C++ python binding. I am going to explain how to build OpenCV from source and generate python binding.

The steps needed to be taken in order to install OpenCV 3.4.0 are:

  1. Update and Upgrade and Cleanup (Required for error-free installation)
  2. Install Dependencies
  3. Download and Build OpenCV

Update and Upgrade and Cleanup (Required for error-free installation)

sudo apt-get update
sudo apt-get upgrade

Now that we have the updates ready, we are ready to install dependencies.

Install Dependencies

sudo apt-get install build-essential 
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

The following command is needed to process images:

sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

To process videos:

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev

For GUI:

sudo apt-get install libgtk-3-dev

For optimization:

sudo apt-get install libatlas-base-dev gfortran pylint

To build OpenCV binding for both python 2 and 3.

sudo apt-get install python2.7-dev python3.5-dev

We are now ready to download and build OpenCV on our Ubuntu machine.

Download and Build OpenCV

To Download OpenCV 3.4.0, go to terminal and type:

wget https://github.com/opencv/opencv/archive/3.4.0.zip -O opencv-3.4.0.zip

To Download OpenCV Contrib 3.4.0 (Contrib has very useful algorithms which is a must for anyone working on Computer Vision), go to terminal and type:

wget https://github.com/opencv/opencv_contrib/archive/3.4.0.zip -O opencv_contrib-3.4.0.zip

We need to unzip to extract the zip files. Install unzip if not installed using the command:

sudo apt-get install unzip

Now extract OpenCV and OpenCV Contrib:

unzip opencv-3.4.0.zip
unzip opencv_contrib-3.4.0.zip

Make a directory named build inside OpenCV-3.4.0:

cd  opencv-3.4.0
mkdir build
cd build

Now we are going to configure cmake:

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.4.0/modules -DOPENCV_ENABLE_NONFREE=True ..

The command will take some time to execute. Wait for few seconds.

If the configuration is done without error, we will use make command to build.

make -j4 (where 4 is number of cores)

This command will take a few minutes to complete. On my PC, it took about 20 minutes.

Install in the location /usr/local using command

sudo make install

Reinitialize static libs using the following command

sudo ldconfig

Check if the OpenCV file exists

  • python2
“/usr/local/lib/python2.7/dist-packages/cv2.so”
  • python3.5
“/usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so”
  • python3.6
“/usr/local/lib/python3.6/dist-packages/cv2.cpython-36m-x86_64-linux-gnu.so”

To use OpenCV 3.4.0 in virtual environment like virtualenv, conda or any other, copy related .so file in your virtual environment python directory

“lib/site-packages/”

Finally, check OpenCV version. Go to the terminal and typepython
On the python terminal, type

import cv2
cv2.__version__

If you ran into any problem while installing OpenCV or have any query related to OpenCV then feel free to comment below. I will try my best to solve it.

 

4 Comments on How to install OpenCV 3.4.0 on Ubuntu 16.04

  1. im stuck in this command
    sudo make install, what is the problem?

    administrator@administrator-Veriton-X2630G:~/Downloads/opencv-3.4.2/build$ sudo make install
    [sudo] password for administrator:
    [ 0%] Built target gen-pkgconfig
    [ 3%] Built target libjpeg-turbo
    [ 5%] Built target libtiff
    [ 10%] Built target libwebp
    [ 11%] Built target libjasper
    [ 14%] Built target IlmImf
    [ 18%] Built target libprotobuf
    [ 19%] Built target ittnotify
    [ 19%] Built target opencv_ts_pch_dephelp
    [ 19%] Built target pch_Generate_opencv_ts
    [ 19%] Built target opencv_core_pch_dephelp
    [ 19%] Built target pch_Generate_opencv_core
    [ 23%] Built target opencv_core
    [ 23%] Built target opencv_imgproc_pch_dephelp
    [ 24%] Built target pch_Generate_opencv_imgproc
    [ 28%] Built target opencv_imgproc
    [ 28%] Built target opencv_imgcodecs_pch_dephelp
    [ 28%] Built target pch_Generate_opencv_imgcodecs
    [ 29%] Built target opencv_imgcodecs
    [ 29%] Built target opencv_videoio_pch_dephelp
    [ 29%] Built target pch_Generate_opencv_videoio
    [ 29%] Built target opencv_videoio
    [ 29%] Built target opencv_highgui_pch_dephelp
    [ 29%] Built target pch_Generate_opencv_highgui
    [ 29%] Built target opencv_highgui
    [ 30%] Built target opencv_ts
    [ 30%] Built target opencv_test_core_pch_dephelp
    [ 30%] Built target pch_Generate_opencv_test_core
    [ 32%] Built target opencv_test_core
    [ 32%] Built target opencv_perf_core_pch_dephelp
    [ 32%] Built target pch_Generate_opencv_perf_core
    [ 34%] Built target opencv_perf_core
    [ 34%] Built target opencv_flann_pch_dephelp
    [ 34%] Built target pch_Generate_opencv_flann
    [ 34%] Built target opencv_flann
    [ 34%] Built target opencv_test_flann_pch_dephelp
    [ 34%] Built target pch_Generate_opencv_test_flann
    [ 34%] Built target opencv_test_flann
    [ 34%] Built target opencv_test_imgproc_pch_dephelp
    [ 35%] Built target pch_Generate_opencv_test_imgproc
    [ 37%] Built target opencv_test_imgproc
    [ 37%] Built target opencv_perf_imgproc_pch_dephelp
    [ 37%] Built target pch_Generate_opencv_perf_imgproc
    [ 39%] Built target opencv_perf_imgproc
    [ 39%] Built target opencv_ml_pch_dephelp
    [ 39%] Built target opencv_test_ml_pch_dephelp
    [ 39%] Built target pch_Generate_opencv_test_ml
    [ 39%] Built target pch_Generate_opencv_ml
    [ 40%] Built target opencv_ml
    [ 40%] Built target opencv_test_ml
    [ 40%] Built target opencv_objdetect_pch_dephelp
    [ 40%] Built target pch_Generate_opencv_objdetect
    [ 41%] Built target opencv_objdetect
    [ 41%] Built target opencv_test_objdetect_pch_dephelp
    [ 41%] Built target pch_Generate_opencv_test_objdetect
    [ 42%] Built target opencv_test_objdetect
    [ 42%] Built target opencv_perf_objdetect_pch_dephelp
    [ 42%] Built target pch_Generate_opencv_perf_objdetect
    [ 43%] Built target opencv_perf_objdetect
    [ 43%] Built target opencv_test_phase_unwrapping_pch_dephelp
    [ 43%] Built target pch_Generate_opencv_test_phase_unwrapping
    [ 43%] Built target opencv_phase_unwrapping_pch_dephelp
    [ 43%] Built target pch_Generate_opencv_phase_unwrapping
    [ 44%] Built target opencv_phase_unwrapping
    [ 44%] Building CXX object modules/phase_unwrapping/CMakeFiles/opencv_test_phase_unwrapping.dir/test/test_unwrapping.cpp.o
    In file included from /home/administrator/Downloads/opencv-3.4.2/modules/ts/include/opencv2/ts.hpp:77:0,
    from /home/administrator/Downloads/opencv-3.4.2/build/modules/phase_unwrapping/test_precomp.hpp:12:
    /home/administrator/Downloads/opencv_contrib-3.4.0/modules/phase_unwrapping/test/test_unwrapping.cpp: In member function ‘virtual void HistogramPhaseUnwrapping_unwrapPhaseMap_Test::TestBody()’:
    /home/administrator/Downloads/opencv-3.4.2/modules/ts/include/opencv2/ts/ts_ext.hpp:20:34: error: ‘required_opencv_test_namespace’ was not declared in this scope
    #define CV__TEST_NAMESPACE_CHECK required_opencv_test_namespace = true;
    ^
    /home/administrator/Downloads/opencv-3.4.2/modules/ts/include/opencv2/ts/ts_ext.hpp:26:5: note: in expansion of macro ‘CV__TEST_NAMESPACE_CHECK’
    CV__TEST_NAMESPACE_CHECK \
    ^
    /home/administrator/Downloads/opencv-3.4.2/modules/ts/include/opencv2/ts/ts_ext.hpp:35:11: note: in expansion of macro ‘CV__TEST_INIT’
    CV__TEST_INIT \
    ^
    /home/administrator/Downloads/opencv-3.4.2/modules/ts/include/opencv2/ts/ts_ext.hpp:69:72: note: in expansion of macro ‘CV__TEST_BODY_IMPL’
    void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() BODY_IMPL(
    ^
    /home/administrator/Downloads/opencv-3.4.2/modules/ts/include/opencv2/ts/ts_ext.hpp:72:41: note: in expansion of macro ‘TEST_’
    #define TEST(test_case_name, test_name) TEST_(test_case_name, test_name, CV__TE
    ^
    /home/administrator/Downloads/opencv_contrib-3.4.0/modules/phase_unwrapping/test/test_unwrapping.cpp:99:1: note: in expansion of macro ‘TEST’
    TEST( HistogramPhaseUnwrapping, unwrapPhaseMap )
    ^
    /home/administrator/Downloads/opencv-3.4.2/modules/ts/include/opencv2/ts/ts_ext.hpp:20:34: note: suggested alternative:
    #define CV__TEST_NAMESPACE_CHECK required_opencv_test_namespace = true;
    ^
    /home/administrator/Downloads/opencv-3.4.2/modules/ts/include/opencv2/ts/ts_ext.hpp:26:5: note: in expansion of macro ‘CV__TEST_NAMESPACE_CHECK’
    CV__TEST_NAMESPACE_CHECK \
    ^
    /home/administrator/Downloads/opencv-3.4.2/modules/ts/include/opencv2/ts/ts_ext.hpp:35:11: note: in expansion of macro ‘CV__TEST_INIT’
    CV__TEST_INIT \
    ^
    /home/administrator/Downloads/opencv-3.4.2/modules/ts/include/opencv2/ts/ts_ext.hpp:69:72: note: in expansion of macro ‘CV__TEST_BODY_IMPL’
    void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody() BODY_IMPL(
    ^
    /home/administrator/Downloads/opencv-3.4.2/modules/ts/include/opencv2/ts/ts_ext.hpp:72:41: note: in expansion of macro ‘TEST_’
    #define TEST(test_case_name, test_name) TEST_(test_case_name, test_name, CV__TE
    ^
    /home/administrator/Downloads/opencv_contrib-3.4.0/modules/phase_unwrapping/test/test_unwrapping.cpp:99:1: note: in expansion of macro ‘TEST’
    TEST( HistogramPhaseUnwrapping, unwrapPhaseMap )
    ^
    In file included from /home/administrator/Downloads/opencv-3.4.2/build/modules/phase_unwrapping/test_precomp.hpp:12:0:
    /home/administrator/Downloads/opencv-3.4.2/modules/ts/include/opencv2/ts.hpp:840:13: note: ‘opencv_test::required_opencv_test_namespace’
    extern bool required_opencv_test_namespace; // compilation check for non-refac
    ^
    modules/phase_unwrapping/CMakeFiles/opencv_test_phase_unwrapping.dir/build.make:62: recipe for target ‘modules/phase_unwrapping/CMakeFiles/opencv_test_phase_unwrapping.dir/test/test_unwrapping.cpp.o’ failed
    make[2]: *** [modules/phase_unwrapping/CMakeFiles/opencv_test_phase_unwrapping.dir/test/test_unwrapping.cpp.o] Error 1
    CMakeFiles/Makefile2:4090: recipe for target ‘modules/phase_unwrapping/CMakeFiles/opencv_test_phase_unwrapping.dir/all’ failed
    make[1]: *** [modules/phase_unwrapping/CMakeFiles/opencv_test_phase_unwrapping.dir/all] Error 2
    Makefile:160: recipe for target ‘all’ failed
    make: *** [all] Error 2

  2. I had opencv 3.4.1 uninstalled it completely, and installed it from here from source but it always installing 3.4.1 but i want 3.4.0 version only.
    NOT WORKING for specific version

    • Build opencv as described in tutorial for required version and create a python virtual environment then copy cv2.so file from build dir to your virtual environment sitepackages dir.

  3. Hi – After installing Python 2.7 and OpenCV 3.4.1 on an Ubuntu 18.04 LTS server, I’m testing a cron job for facial recognition. It almost works, but:

    Traceback (most recent call last):
    File “/var/www/html/www.zmmarchive.com/pages/tools/../../lib/facial_recognition/faceRecognizerTrainer.py”, line 36, in
    model = cv2.createLBPHFaceRecognizer(2, 10, 10, 10)
    AttributeError: ‘module’ object has no attribute ‘createLBPHFaceRecognizer’

    Do I need to uninstall and re-install either OpenCV or Python?

2 Trackbacks & Pingbacks

  1. CUDA 8.0 + Opencv 3.3 + cudnn 7.0 + Qt5 – Yu Chen's Life
  2. AMD製GPU + ROCm + OpenCV + TensorFlow でGPUコンピューティング環境をつくる - YUEDY

Leave a Reply

Your email address will not be published.




This site uses Akismet to reduce spam. Learn how your comment data is processed.