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

$ sudo apt-get update

$ sudo apt-get upgrade

Step 2 – Install dependencies

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

$ sudo apt-get install python3.5-dev python3-numpy libtbb2 libtbb-dev

$ sudo apt-get install libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev

Step 3 –  Get OpenCV

$ sudo -s

$ cd /opt

/opt$ git clone https://github.com/Itseez/opencv.git

/opt$ git clone https://github.com/Itseez/opencv_contrib.git

Step 4 – build and install OpenCV

/opt$ cd opencv

/opt/opencv$ mkdir release

/opt/opencv$ cd release

/opt/opencv/release$ cmake -D BUILD_TIFF=ON -D WITH_CUDA=OFF -D ENABLE_AVX=OFF -D WITH_OPENGL=OFF -D WITH_OPENCL=OFF -D WITH_IPP=OFF -D WITH_TBB=ON -D BUILD_TBB=ON -D WITH_EIGEN=OFF -D WITH_V4L=OFF -D WITH_VTK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib/modules /opt/opencv/

/opt/opencv/release$ make -j4

/opt/opencv/release$ make install

/opt/opencv/release$ ldconfig

/opt/opencv/release$ exit

/opt/opencv/release$ cd ~

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

$ pkg-config --modversion opencv
3.2.x

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

$ mkdir cpp_test

$ cd cpp_test

$ touch main.cpp

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

#include <opencv2/highgui.hpp>
#include <iostream>

int main( int argc, char** argv ) {
  
  cv::Mat image;
  image = cv::imread("sample.jpeg" , CV_LOAD_IMAGE_COLOR);
  
  if(! image.data ) {
      std::cout <<  "Could not open or find the image" << std::endl ;
      return -1;
    }
  
  cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
  cv::imshow( "Display window", image );
  
  cv::waitKey(0);
  return 0;
}

Now compile your code with the following command

g++ main.cpp -o output `pkg-config --cflags --libs opencv`

Now run the C++ program with the following command

$ ./output

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





47 Comments

  1. 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

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

    Its missleading, please correct it.

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

  3. 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.

  4. 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

  5. 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

  6. 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!

  7. 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?

  8. 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.

  9. 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.

    • 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

  10. 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.

  11. 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?

    • 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

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

  12. pkg-config –modversion opencv
    the above code needs

    ‘libopencv-dev’ to be installed.

    So try ‘sudo apt-get install libopencv-dev’ before checking the version.

  13. i am facing Segmentation fault because of imshow(“Display window”,image) function.
    I have tried looking at almost everything, but can’t find the issue. Any help would be appreciated.

Leave a Reply

Your email address will not be published.


*