How to start with OpenCV on Ubuntu Linux

OpenCV is a computer vision library that can be used in the development of applications programmed in C++ and C. It is relatively easy to get started with OpenCV, but you will need some basic C programming skills and the prerequisite packages installed on your system. In this tutorial, we will go through the step by step instructions of installing everything required for OpenCV on an Ubuntu Linux system. Then, we will compile a basic program to get you started with using OpenCV.

In this tutorial you will learn:

  • How to install OpenCV prerequisite packages on Ubuntu Linux
  • How to use OpenCV to compile an example program
How to start with OpenCV on Ubuntu Linux
How to start with OpenCV on Ubuntu Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu Linux
Software OpenCV
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Getting started with OpenCV and Computer Vision on Ubuntu Linux step by step instructions




Follow the step by step instructions below to get the prerequisite packages for OpenCV Computer Vision installed on Ubuntu Linux, and then begin using the programming library to create and compile a C++ program that utilizes OpenCV.

  1. Let’s start by installing the packages we will need to work with OpenCV on our Ubuntu system:
    $ sudo apt update
    $ sudo apt install libopencv-dev libavcodec-dev libavformat-dev libavutil-dev libavutil-dev pkg-config g++ cmake
    
  2. Once the installation is done, let’s try compiling an example file. The following code can be used to display the contents of an image file:
    #include <stdio.h>
    #include <opencv2/opencv.hpp>
    using namespace cv;
    int main(int argc, char** argv )
    {
        if ( argc != 2 )
        {
            printf("usage: DisplayImage.out <Image_Path>\n");
            return -1;
        }
        Mat image;
        image = imread( argv[1], IMREAD_COLOR );
        if ( !image.data )
        {
            printf("No image data \n");
            return -1;
        }
        namedWindow("Display Image", WINDOW_AUTOSIZE );
        imshow("Display Image", image);
        waitKey(0);
        return 0;
    }

    Now it is time to save this code into file. For example let us save it into DisplayImage.cpp file.

  3. In this same directory as the C++ file you just saved, we must now create a cmake file named CMakeLists.txt with the following content:
    cmake_minimum_required(VERSION 2.8)
    project( DisplayImage )
    find_package( OpenCV REQUIRED )
    include_directories( ${OpenCV_INCLUDE_DIRS} )
    add_executable( DisplayImage DisplayImage.cpp )
    target_link_libraries( DisplayImage ${OpenCV_LIBS} )
    



  4. Next, run cmake and make in the directory to turn your .cpp file into an executable:
    $ cmake .
    $ make
    
  5. Once the program is done compiling, we can use it to open up and display an image by running the program and specifying the path to the image that we want to open:
    $ ./DisplayImage mountain.png
    

    Using our OpenCV program to display an image on Ubuntu
    Using our OpenCV program to display an image on Ubuntu

Closing Thoughts

In this tutorial, we saw how to install the OpenCV programming library on an Ubuntu Linux system. We then saw how to get started with using OpenCV in C++ by creating a small but powerful program that can be used as an image viewer. It will display any image file that we specify.