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 – Machine Learning A-Z: Hands-On Python & R In Data Science https://bit.ly/2qTCNDO. Learn to create Machine Learning Algorithms in Python and R from two Data Science experts. Code templates included.


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
  • 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

    • Mkadem Imen

      hey same mistake ,how did you solve it?

  • 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!