How to Install OpenCV in Ubuntu 16.04 LTS for C / C++




In this post we will see How to Install OpenCV in Ubuntu for C/C++
OpenCV is an image processing library created by Intel and later supported by Willow Garage and now maintained by Itseez. OpenCV means Intel® Open Source Computer Vision Library. It is a collection of C functions and a few C++ classes that implement some popular Image Processing and Computer Vision algorithms. OpenCV is Available on Mac, Windows, Linux (Terminal environment).

Step 1 – Updating Ubuntu

Step 2 – Install dependencies

Step 3 –  Get OpenCV

Step 4 – build and install OpenCV

Now to check if OpenCV is installed on a machine, run the following commands

We will get the opencv version installed
in this case it’s 3.2.x


Online Course – OpenCV Python Tutorial For Beginners. At the end of this course, you will have a firm grasp of Computer Vision techniques using OpenCV libraries. This course will be your gateway to the world of data science.


Create a C++ program

Follow the commands

The above command will create a folder called cpp_test and create a main.cpp file inside it
Now place any .jpeg image inside the cpp_test folder.
So Now your cpp_test folder will contain two files as follows
.
├── sample.jpeg
└── main.cpp

Now open the main.cpp and add the following code

Now compile your code with the following command

Now run the C++ program with the following command

press ESC to exit


Video instruction – How to Install OpenCV in Ubuntu 16.04 LTS for C / C++


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com


  • Raul Alexander

    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc’
    to the PKG_CONFIG_PATH environment variable
    No package ‘opencv’ found

  • Raul Alexander

    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc’
    to the PKG_CONFIG_PATH environment variable
    No package ‘opencv’ found https://uploads.disquscdn.com/images/e079f0ae50f33017e24db8d8690d3d29ce58a8b2fb44994835f2df22ec0a1306.png

    • Roy

      Hello! I am having the same error. Did you find any solution for this?

      • sadek mostafa

        try this one it works with me

        sudo apt-get install libopencv-dev

  • nipunasudha

    Thank you, this really helped!

  • Prashant Singh

    Thanks. Can you also show how to use opencv in python using a simple example.
    Does this build also work for use in python.

  • Prashant Singh

    I followed the procedure above. For c++ it’s running fine but for python its throwing error.
    What mistake I am doing. Please solve this.
    https://uploads.disquscdn.com/images/10592378160c3560490f804150b29de092d1c4e3921549418d4d5803e7690724.png

  • Thanks for sharing~ It success on my computer.

  • Chinmesh Manjrekar

    In step 4, 5th command onwards, the current working directory is /opt/opencv/releases, and not /opt/opencv

    Its missleading, please correct it.

    • Yasmine Sidhom

      thanks. that was really helpful

    • Hassan Bakrim

      In order to use #include and #include
      in opencv project, we need to install
      libvtk5-dev and libeigen3-dev:
      sudo apt-get isntall libvtk5-dev
      sudo apt-get isntall libeigen3-dev

      and turn on the build composantes in cmake. Also, the path /opt/opencv/ stil correct:

      /opt/opencv/release$ cmake … -D WITH_EIGEN=ON -D WITH_V4L=ON … /opt/opencv/

  • Amel

    My Installation was successful but now I have a big Problem, my Computer isn´t working anymore.
    When I´m starting the login comes up on my Ubuntu 16.04, then I type the right Datas.
    It shows me then only quick an Blackscreen with an error message(/dev/nvme0n1p2: clean, 359022/60407808 files, 9919453/241604864 blocks) and then it goes back to the login.
    I think something with the screen resolution is wrong. Here I have another error messages:

    X Error of failed request: BadWindow (invalid Window parameter)
    Major opcode of failed request: 155 (NV-GLX)
    Minor opcode of failed request: 4 ()
    Resource id in failed request: 0x1400003
    Serial number of failed request: 40
    Current serial number in output stream: 40
    gnome-session-check-accelerated: Helper exited with code 256
    gnome-session-binary[25119]: CRITICAL: We failed, but the fail whale is dead. Sorry….
    CRITICAL: We failed, but the fail whale is dead. Sorry….

    Hopefully can someone help me with my Problem.

  • Manish Kumar
  • Nic Lorenzo

    Thanks bro!

  • Shivam Gupta

    Hi. I followed all the steps mentioned above and it was completed successfully.
    But I am facing this error now..
    Anyone knows the solution for this?

    https://uploads.disquscdn.com/images/a4cca9f1ff04bc913b3d0029b14458886c1fb71e94032d5c09e6ded46c1c89bb.png

    Please help! 🙂

  • Trịnh Minh Toàn
    • Diego Carlos do Brasil

      You have to declare you opencv install system variables.

  • hello

    https://uploads.disquscdn.com/images/d44241ab84e000378f7d6450e711d83972c8589ce192f87e61cc9c034f63984d.png

    Why make a mistake?????????????????????????????????
    My Linux version: 18.04 LTS Ubuntu
    I failed to install libjasper-dev and python3.5-dev too.
    Attached Files CMakeOutput.log and CMakeError.log.

    http://mihanfarm.com/c/CMakeOutput.log

    http://mihanfarm.com/c/CMakeError.log

  • Emad Mojtahedifar

    for openvc Version 4.0.0 (v4.0) , you must use -std=c++14:
    g++ main.cpp -o output pkg-config --cflags --libs opencv -std=c++14
    and change ‘CV_LOAD_IMAGE_COLOR ‘ to cv::IMAGE_COLOR

  • LB

    when i’m trying “sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev”, the terminal says:
    Lecture des listes de paquets… Fait
    Construction de l’arbre des dépendances
    Lecture des informations d’état… Fait
    Aucune version du paquet pkg-config n’est disponible, mais il existe dans la base
    de données. Cela signifie en général que le paquet est manquant, qu’il est devenu obsolète
    ou qu’il n’est disponible que sur une autre source

    E: Impossible de trouver le paquet libgtk2.0-dev
    E: Impossible de trouver de paquet correspondant à l’expression rationnelle « libgtk2.0-dev »
    E: Impossible de trouver de paquet correspondant à l’expression rationnelle « libgtk2.0-dev »
    E: Le paquet « pkg-config » n’a pas de version susceptible d’être installée
    E: Impossible de trouver le paquet libavcodec-dev
    E: Impossible de trouver le paquet libavformat-dev
    E: Impossible de trouver le paquet libswscale-dev

  • Abanoub Mamdouh

    When I type the cmake command, it says : CMake Error: The source directory "/opt/opencv" does not exist.
    Specify --help for usage, or press the help button on the CMake GUI.

    I created the directory and I am typing the command from inside it, so it’s really strange!

    • Diego Carlos do Brasil

      You must type inside /release bro

  • Pablo Fonovich

    hi, I’m getting this error in ubuntu xenial:

    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc’
    to the PKG_CONFIG_PATH environment variable
    No package ‘opencv’ found

    Any hints?

    • mariano REDAELLI

      Install the development package as:
      apt install libopencv-dev

      • Promod Sampath Elvitigala

        Thanks That resolved issue!

        • Anas Abdulla

          but it shows installed version is a bit older.2.4.9.any way to upgrade that

          • The Gravel

            Me too

      • Ali Rojas

        Thanks, that works! but every time I compile, I get this message:

        OpenCV: FFMPEG: tag 0x67706a6d/’mjpg’ is not supported with codec id 8 and format ‘mp4 / MP4 (MPEG-4 Part 14)’
        OpenCV: FFMPEG: fallback to use tag 0x7634706d/’mp4v’

  • Hải Phạm

    Hi everyone,
    I’m getting this error when trying to compile above code.

    $ pkg-config –modversion opencv
    > 3.4.5

    $ g++ main.cpp -o main pkg-config --cflags --libs opencv
    > /tmp/cc4vbYSm.o: In function main':
    main.cpp:(.text+0x64): undefined reference to
    cv::imread(cv::String const&, int)’
    main.cpp:(.text+0xfd): undefined reference to cv::namedWindow(cv::String const&, int)'
    main.cpp:(.text+0x154): undefined reference to
    cv::imshow(cv::String const&, cv::_InputArray const&)’
    /tmp/cc4vbYSm.o: In function cv::String::String(char const*)':
    main.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x54): undefined reference to
    cv::String::allocate(unsigned long)’
    /tmp/cc4vbYSm.o: In function cv::String::~String()':
    main.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to
    cv::String::deallocate()’
    collect2: error: ld returned 1 exit status

    Please help.

  • Rutvik Rathod
  • Promod Sampath Elvitigala

    Thanks working as mentioned

  • The Gravel
  • The Gravel

    I have very slow get clone, and failed suddenly

    • The Gravel

      I used make -j1. That was worked for me

  • The Gravel

    g++ main.cpp -o output pkg-config --cflags --libs opencv
    main.cpp:1:31: fatal error: opencv2/highgui.hpp: No such file or directory
    compilation terminated.

    • Manu

      if you are using opencv 4 try this g++ main.cpp -o output pkg-config --cflags --libs opencv4 -std=c++11

    • Mati Haucke

      hi, i have the same problem, i solved changing the first include by this

      #include “highgui.h”

      why? i check the includes directory and actually there is not any opencv2/highgui.hpp file, instead there is an highgui.h file. and i used that and works great

  • KOUCHA

    hello,I’m getting this error in debian gnu linux :
    user@DESKTOP-M1UBACJ:~/cpp_test$ g++ -L/usr/local/lib main.cpp -o output pkg-config --cflags --libs opencv
    /usr/bin/ld: warning: libopencv_core.so.3.2, needed by /usr/local/lib/libopencv_viz.so, may conflict with libopencv_core.so.4.1
    /usr/bin/ld: /tmp/ccdH6K1Z.o: undefined reference to symbol ‘_ZN2cv6String10deallocateEv’
    //usr/local/lib/libopencv_core.so.3.2: error adding symbols: DSO missing from command line
    collect2: error: ld returned 1 exit status
    please help me.

  • Ali Rojas

    I have followed all steps, and they seemed to be ok. But when I try to verify that OpenCV has been installed (with “pkg-config –modversion opencv”) this apears:

    pkg-config –modversion opencv
    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc’
    to the PKG_CONFIG_PATH environment variable
    No package ‘opencv’ found

    Please, could you help me?

    • Ali Rojas

      I could solve my problems making this chages:

      Step 2
      *2nd command: change “python3.5-dev” by “python3.6-dev” (or the version of python that points ubuntu when you use “python3 -V”)
      *3th command: delete “libjasper-dev”

      Step 4
      *After all commands and before cheking if OpenCV is installed, use this command:
      sudo apt-get install libopencv-dev

    • Ali Rojas

      Despide de solution I have written, every time I compile using, for example:
      $ g++ main.cpp -o main pkg-config --cflags --libs opencv

      I get this message:
      “OpenCV: FFMPEG: tag 0x67706a6d/’mjpg’ is not supported with codec id 8 and format ‘mp4 / MP4 (MPEG-4 Part 14)’
      OpenCV: FFMPEG: fallback to use tag 0x7634706d/’mp4v'”

      Do you know why that happens and what can I do to avoid it?