The Wayback Machine - https://web.archive.org/web/20241008071619/https://www.geeksforgeeks.org/how-to-install-opencv-in-c-on-linux/
Open In App

How to Install opencv in C++ on Linux?

Last Updated : 27 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.



Similar Reads

How to Install OpenCV for Python in Linux?
Prerequisite: Python Language Introduction OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human.
2 min read
How to Install Opencv 4 on MacOS?
In this article, we will learn how to install Opencv 4 in Python on MacOS. OpenCV (Open Source Computer Vision Library) is a library of programming functions mainly aimed at real-time computer vision. Installation:Method 1: Using pip to install Opencv 4 Package Follow the below steps to install the Opencv 4 package on macOS using pip: Step 1: Insta
2 min read
How to Install OpenCV for C++ on MacOS?
OpenCV stands for Open Source Computer and Vision Library. It is a huge open-source library with more than 2500 algorithms that are optimized to perform various operations like computer vision, image processing, and machine learning in real-time. It performs all these operations in real-time which gives it an edge over all other similar libraries.
4 min read
How to Install OpenCV in Ubuntu?
OpenCV is a powerful and versatile library widely used for real-time computer vision tasks. It's commonly used in applications like image and video processing, object detection, facial recognition, and more. Recent versions of Ubuntu come with pre-built OpenCV packages available through the apt package manager. Table of Content 1. Install OpenCV in
4 min read
Install OpenCV that was built with CMAKE build option to venv
OpenCV (Open Source Computer Vision Library) is a powerful tool for image processing and computer vision tasks. Often, we may need to customize the OpenCV installation using CMake to include specific modules or optimizations. This guide will walk us through the process of building OpenCV from the source using CMake and installing it into the Python
2 min read
How to Install OpenCV for Python on Windows?
Prerequisite: Python Language Introduction   OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human
4 min read
How to Install GD module on Linux?
GD is a python library. It provides various features and an async-ready API wrapper for the geometry dash. It is easy to use. It is used to implement the geometry dash API and is also very handy to work with object-oriented design. Installing GD module on Linux Method 1: Using pip to install GD Package Follow the below steps to install the GD packa
2 min read
How to Install Kali Linux on Windows?
Kali Linux is an open-source Linux distribution based on Debian, designed for sophisticated penetration testing and security auditing. Kali Linux includes hundreds of tools for diverse information security activities such as penetration testing, security research, computer forensics, and reverse engineering. This multi-platform solution is accessib
2 min read
How to install and configure Docker on Arch-based Linux Distributions(Manjaro) ?
In this article, we are going to see how to install and configure Docker on Arch-based Linux Distributions. Docker is an open-source containerization platform used for building, running, and managing applications in an isolated environment. A container is isolated from another and bundles its software, libraries, and configuration files. Containers
2 min read
Install Pytorch on Linux
In this article, we are going to see how you can install PyTorch in the Linux system. We are using Ubuntu 20 LTS you can use any other one. To successfully install PyTorch in your Linux system, follow the below procedure: First, check if you are using python’s latest version or not. Because PyGame requires python 3.7 or a higher version, make sure
2 min read
How to Install Deepin on Linux?
Deepin is the top Linux distribution from China, it provides a beautiful, easy-to-use, safe, and reliable operating system for global users. Deepin is based on Debian's stable branch. The feature that sets Deepin aside from the rest of Linux distributions is its desktop environment known as DDE Deepin Desktop Environment, which was described as "th
3 min read
How to Install NuGet from Command Line on Linux?
NuGet is the package manager for the .NET framework. It's very much useful for developers as developers can create, publish and consume packages. The package format of NuGet consists of a single ZIP file with the extension of .nupkg and which is the DLL compiled code like the package’s version number. A NuGet package manager is a cross-platformed m
2 min read
How to Install VirtualBox in Linux?
Virtual Machine abstracts the hardware of our personal computers such as CPU, disk drives, memory, NIC (Network Interface Card), etc., into many different execution environments as per our requirements, hence giving us a feeling that each execution environment is a single computer. For example, VirtualBox. We can create a virtual machine for severa
3 min read
How to Install Git on Windows Subsystem for Linux?
Git is an open-source distributed version control system developed to handle everything from simple to complex projects with performance and speed. Git software allows a team or group of developers to work together, with all using the same files and folders. It gradually reduces the confusion that tends to happen when multiple people are editing th
2 min read
How to Install Linux on Windows PowerShell Subsystem?
There are several ways to Install a Linux subsystem on your Windows PC Powershell Environment. It is good for learners, but it is recommended using original Linux OS if you are a developer as the Subsystem lacks the pre-installed Linux tools. Before we begin installing a Linux subsystem, we need to first enable a Windows feature for Subsystems. Ena
2 min read
How to Install Lazy Script in Kali Linux?
Kali Linux is one of the most advanced hacking OS systems from Linux family. Kali Linux is filled with many hacking tools and supporting learners and hackers worldwide. There are many versions of Kali Linux which provides a good user interface and desired environment. The Lazy Script is designed to help many users to save time and work. The script
2 min read
How to install Virtual Box in Kali Linux
We all have seen that we find lots of installation walkthroughs about how to install a virtual box in windows and run kali Linux on that. But in this installation guide, we are going to show you how you can install Virtual box in the Kali Linux Operating system. Let's start. How to install Virtual Box in Kali Linux Step 1: Launch your kali Linux en
2 min read
How to Download and Install Python Latest Version on Linux?
Python is a widely-used general-purpose, high-level programming language. This article will serve as a complete tutorial on How to download and install Python latest version on Linux Operating Systems. On every Linux system including following OS, Ubuntu Linux Mint Debian openSUSE CentOS Fedora and my favourite one, Arch Linux. You will find Python
3 min read
How to Install Z Shell(zsh) on Linux?
The ZSH or Z Shell is a Unix based shell or can be called as a command-line interpreter. It is an extended version of the good old Bourne shell (sh). It does have some features form Bash with lots of added features like automatic cd (Change Directory), spell check, path expansion, and many more. It also supports plugins and themes. Installing ZSH o
2 min read
How to Install Perl on Linux?
Prerequisite: Introduction to Perl Before, we start with the process of Installing Perl on our System. We must have first-hand knowledge of What the Perl Language is and what it actually does?. Perl is a general-purpose, high level interpreted and dynamic programming language. Perl was originally developed for text processing like extracting the re
3 min read
How to Install Code Blocks for C++ on Linux?
Code::Blocks is a free IDE( an integrated development environment), for C/C++ and FORTRAN languages. It is a cross-platform IDE and available for Windows, Mac, and Linux, In this article, we are going to discuss various methods using which we can install Code Blocks on Linux.: Installation Code Blocks for C++ on Linux:Method 1: Using apt install co
2 min read
How to install Jupyter Notebook in Linux?
Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. Uses include data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more. Jupyter has support for over 40 differ
2 min read
How to Install Python Pycharm on Linux?
Prerequisite: Python Language Introduction Python is a widely-used general-purpose, high-level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code
2 min read
How to Install Python-USPP on Linux?
Python-USPP is a multi-platform Python library that allows communication between Python programs and USPP devices. This library is written in Python itself. It supports Windows, Linux, and MacOS. In this article, we will learn how to install this library in the Linux operating system. Python USPP installation on Linux The python uspp has been devel
3 min read
How to Install and Configure Apache Subversion(SVN) In Linux?
Apache Subversion (SVN), is a version control system like Git. it is distributed under an open-source license. SVN was created by CollabNet Inc. but now it is developed as a project of the Apache Software Foundation. Downloading and Installing Apache SVN Apache Subversion(SVN) can be easily downloaded and installed with the use of the command-line.
2 min read
How to Install Jmespath in Python in Linux?
Jmespath is known as the JSON query language for Python. It is used to find or extract the needed data from the JSON document or dictionary very quickly. It is also available in other languages also. In this article, we will be looking at the stepwise procedure to install the Jmespath for Python in Linux Operating System. Requirements: Python3Pytho
2 min read
How to Install Numdifftools in Python on Linux?
Numdifftools is a Python-based toolkit that solves numerical differentiation issues in one or more variables automatically. To produce the most accurate outcome, finite differences are exploited adaptively in conjunction with a Richardson extrapolation mechanism. This library is cross-platformed, so it can work with Windows, Linux, and even with ma
2 min read
How to Install Apache Pig in Linux?
Pig is a high-level platform or tool which is used to process large datasets. It provides a high-level of abstraction for processing over the MapReduce. It provides a high-level scripting language, known as Pig Latin which is used to develop the data analysis codes. In order to install Apache Pig, you must have Hadoop and Java installed on your sys
2 min read
How to Install GNU Octave in Linux?
Octave is open-source, free available for many of the platforms. It is actually a High-level Language. It comes up with a text interface along with an experimental graphical interface. It is also used for various Machine Learning algorithms for solving various numeric problems. You can say that it is similar to MATLAB but slower than MATLAB. We can
2 min read
How to Install TeamViewer on Linux/Ubuntu?
TeamViewer is one of the best cross-platform remote support application. Its available for Windows, Mac, Chrome OS, Raspberry Pi, iOS, Android, and Linux Operating Systems. TeamViewer is used for: Controlling Remote Computer via GUI (Graphical User Interface).File Transfer. If your mode of usage is for personal and non-commercial its completely fre
2 min read