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 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 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 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 g
4 min read