Install OpenCV on Ubuntu 18.04 Bionic Beaver Linux

Objective

The objective is to Install OpenCV on Ubuntu 18.04 Bionic Beaver Linux and provide a simple OpenCV example

Operating System and Software Versions

  • Operating System: – Ubuntu 18.04 Bionic Beaver
  • Software: – OpenCV 3.2 or higher

Requirements

Privileged access to your Ubuntu System as root or via sudo command is required.

Difficulty

EASY

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

Install OpenCV on Ubuntu

OpenCV is a multi-platform open source Computer vision library. OpenCV is part of Ubuntu’s universe package repository.

Ubuntu OpenCV with Python

There are three choices for OpenCV install with python bindings. You can choose between Python 2 version or Python 3 version or both.

To install OpenCV on Ubuntu 18.04 with python 2 bindings open up terminal and enter:

$ sudo apt -y install python-opencv

Confirm a correct OpenCV installation by loading the appropriate cv2 library :

$ python
Python 2.7.14+ (default, Feb  6 2018, 19:12:18) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2._version__
'3.2.0'
>>>

For installation of OpenCV on Ubuntu 18.04 with Python 3 bindings execute:

$ sudo apt -y install python3-opencv

Confirm a correct OpenCV installation by loading the appropriate cv2 library :

$ python3
Python 3.6.4+ (default, Feb 12 2018, 08:25:03) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'
>>> 


Example Opencv Python Test

Let’s perform a sample Opencv Python test to denoise image sample. Save the following code within a new denoise.py file within your home directory:

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('gray_DSC00931.png')
b,g,r = cv2.split(img)           # get b,g,r
rgb_img = cv2.merge([r,g,b])     # switch it to rgb

# Denoising
dst = cv2.fastNlMeansDenoisingColored(img,None,10,10,7,21)

b,g,r = cv2.split(dst)           # get b,g,r
rgb_dst = cv2.merge([r,g,b])     # switch it to rgb

plt.subplot(211),plt.imshow(rgb_img)
plt.subplot(212),plt.imshow(rgb_dst)
plt.show()

Install Python matplotlib which is required by the above code. If using Python 3 version make sure to suffix python keyword with digit 3:

$ sudo apt install python3-matplotlib

Obtain a sample image using wget command:

$ wget -O ~/opencv-sample.png https://linuxconfig.org/images/opencv-sample.png

Lastly, execute the the above OpenCV python code:

$ python3 denoise.py
Python OpenCV on Ubuntu 18.04

Python OpenCV on Ubuntu 18.04. Example test successful.



Ubuntu OpenCV with C++

The following linux command will install OpenCV on Ubuntu 18.04 with C++ libraries:

$ sudo apt install libopencv-dev

The OpenCV libraries are now installed within /usr/include/opencv2 directory.

C++ Opencv Examples

Store the below code into a new img-display.cpp file within your home directory:

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

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                // Show our image inside it.

    waitKey(0);             // Wait for a keystroke in the window
    return 0;
}

Once ready, compile the above code to produce img-display executable binary:

$ g++ img-display.cpp -o img-display `pkg-config --cflags --libs opencv`

Download sample image:

$ wget -O ~/linuxconfig_logo.png https://linuxconfig.org/images/linuxconfig_logo.png

Display the image using the newly compiled img-display executable binary:

$ ./img-display linuxconfig_logo.png
C++ OpenCV on Ubuntu 18.04

C++ OpenCV on Ubuntu 18.04. Example test successful.