Open In App

How to Install opencv in C++ on Linux?

Last Updated : 27 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

OpenCV stands for open-source Computer Vision Library. It is a library that provides infrastructure for computer vision and machine learning applications. It has more than 2500 Computer vision and machine learning algorithms. They can be used to track objects, recognize faces and objects, stitch images, etc. It can be used with c, c++ python, and java. In this article, we will install OpenCV in c++ on Linux.

Installing OpenCV

To install OpenCV, we will need to compile and install the package from the official repository. 

Step 1: Installing required packages and tools

We need a c++ compiler to compile OpenCV, git to clone the sources from official repositories, and CMake, and make to execute the build system and some other dependencies. Enter the following command to get all these.

sudo apt install -y g++ cmake make git libgtk2.0-dev pkg-config

 

Step 2: Download the source. 

We need to clone the latest version of OpenCV. To find the latest version, visit this website and click on the GitHub icon of the latest version. 

 

On the GitHub page, copy the HTTPS link from the code button.

 

Open the terminal and type the following command and paste the link using the shortcut key Ctrl + shift + v

git clone url

 

Step 3: Build the source

First, create the build directory and go into it

mkdir -p build && cd build

 

Next, generate the build scripts using cmake

cmake ../opencv

 

At last, build the source using make

make -j4

 

It will take some time depending upon your CPU power. 

Step 4: Install the OpenCV package

After the build process is completed, install the package. You will need sudo privileges to do so. 

sudo make install

 

That’s it. If you did not encounter any error then OpenCV is installed successfully on your Linux system. The header files are at the location 

/usr/local/include/opencv4

Verifying the OpenCV Installation

To verify our installation, let’s create a test project. We will create a program that displays the image if we pass the location of that image as a parameter to the program. First, create a test folder and move into it.

mkdir test && cd test

Then, create a Program named DisplayImage.cpp

nano DisplayImage.cpp

 

Paste the following code into this file using Ctrl + shift + v and press Ctrl + Y then Y and then Enter to save and exit.

C++




#include <opencv2/opencv.hpp>
#include <stdio.h>
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], 1);
    if (!image.data) {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE);
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}


Now, we will use cmake to generate a build file for this. Create a CMakeLists.txt file.

nano CMakeLists.txt

Paste the following code into this file using Ctrl + shift + v and press Ctrl + Y then Y and then Enter to save and exit.

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} )

Now, to generate build files using cmake, execute the following command.

cmake .

 

Finally, build the program using make with the following command

make

 

This will create the final executable file named “DisplayImage”. Now, execute this file along with the path of an image that you wish to display.

./DisplayImage path_of_the_image

 

You should see the image that you specified.

 

This verifies that our installation is successful.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads