How to Install opencv in C++ on Linux?
Last Updated :
27 Jun, 2022
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 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 de
3 min read
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 ob
2 min read
How to Install Turbo C++ on Linux?
In this article, we will look into how to install Turbo C++ on Linux. Turbo C++ is the free and Open-Source Software, it is a development tool for writing programs in the C/C++ language. Turbo C++ is a compiler and IDE (Integrated development environment) originally from Borland. Prerequisites: To r
2 min read
How to Install cx_oracle in Python on Linux?
The cx_oracle package is used to connect with the Oracle database using python. In this, article, we will look into the process of installing the cx_oracle package on Linux. Pre-requisites: The only thing that you need for installing Numpy on Windows are: PythonPIP or Conda (Depending upon the user
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 i
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
2 min read
How to Install NuPIC on Linux?
NuPIC (short for "Numenta Platform for Intelligent Computing") is an open-source platform for machine learning and artificial intelligence. It was developed by Numenta, a company founded by Jeff Hawkins, the creator of the PalmPilot and the co-founder of Handspring. NuPIC is based on the theory of t
4 min read
How to Install Numpy on Linux?
Python NumPy is a general-purpose array processing package that provides tools for handling n-dimensional arrays. It provides various computing tools such as comprehensive mathematical functions, linear algebra routines. NumPy provides both the flexibility of Python and the speed of well-optimized c
2 min read
How to Install Packages in Python on Linux?
To install a package in python, we use pip. The pip is a python package manager. In this tutorial, we will be discussing how we can install packages in python on a Linux system. To install packages in python on Linux, we must have python and pip installed on our Linux machine. As python comes preins
2 min read
How to Install Pillow on Linux?
In this article, we will look into the various methods of installing the PIL package on a Linux machine. Python Imaging Library (expansion of PIL) is the de facto image processing package for Python language. It incorporates lightweight image processing tools that aid in editing, creating, and savin
2 min read