December 17, 2021
How to use OpenCV Library to create C++ applications
Azhan Mohammed
A good AI model’s full potential is utilized only when it can be used by others who have no prior knowledge of how it works. Building an end-to-end solution is as important as getting an AI system that is accurate and reliable. There are numerous ways to deploy a machine learning or deep learning model, desktop applications happen to be the easiest of them all and take least requirements. Python applications are easier to build but quite slow when compared to C++ applications. Irrespective of the project one basic necessity for every AI project that uses images is OpenCV library, an open-source library written for both Python and C++ which allows numerous functionalities to read, manipulate and write images. Setting up OpenCV library for a desktop application can be quite tricky as the libraries need to work for the client-side as well.


An easy way to ensure that the application runs smoothly everywhere is to build it statically at a path which remains constant on everywhere irrespective of the machine.

For Linux systems this path happens to be usr/local/ which can be found on every system. In this blog we shall discuss how to set up a C++ application that uses OpenCV statically so that the user does not need to build it again and again. The installation process is quite simple, we first set up some libraries which are needed to run the OpenCV library statically, and then build the OpenCV statically.

Once it is done, the user can ship this application folder to different users who can run the application smoothly, provided they place the folder in the correct directory. Setting up the application folder.

In the Linux terminal cd into /usr/local/ and use mkdir application name to create the application directory. This shall be the root of our application, where all our files related to the application should be stored.

We now install the pre-requisites for the application before we build the OpenCV library. Installing dependencies and libraries. Once we have the folder set up, we now install CMake and other libraries which are required to build and use OpenCV library. In the Linux terminal start with:
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:george-edison55/cmake-3.x
sudo apt-get update
sudo apt-get install cmake

CMake is used to build libraries like OpenCV. We shall now install the libraries OpenCV needs. This can be done by running:
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev
libdc1394-22-dev libxine2-dev libv4l-dev libatlas-base-dev libfaac-dev
libmp3lame-dev libtheora-dev libvorbis-dev libxvidcore-dev libopencore-am-
rnb-dev libopencore-amrwb-dev x264 v4l-utils libsm6 libxext6
sudo apt -y install libgtk2.0-dev
sudo apt install unzip

We now have all the pre-requisites to build OpenCV, so let’s get started with it.
Installing and building OpenCV Library
We now create another sub-directory named src which will contain
the OpenCV folders and our main.cpp source file for the application. To build OpenCV use the following commands:

mkdir src && cd src
wget -O
mkdir -p opencv && cd opencvcmake --DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -
DBUILD_opencv_apps=OFF ../opencv-master
cmake —-build . -j12

This builds OpenCV inside the OpenCV folder and we can now test using it for our application.
Minimalistic OpenCV Library Application
Now that we have OpenCV set up, the next step is to build the application and try out if OpenCV is installed correctly. You can set up a simple main.cpp file in the src folder that reads image and shows the output in a window. To get this done, create a main.cpp file and enter the following code there:

#include <opencv2/opencv.hpp>
using namespace std;
int main()
Mat image = cv::imread (“Enter the address of image to read”,
// Error Handling
if (image.empty()) {
cout<<"Image file not found"<< endl;
//wait for any key press
return -1;
cv::imshow(“Trial Application", image);
// Wait for any keystroke
return 0;

To build this application we need to configure a CMake for the application, to do that create a CMakeLists.txt file in the src folder and enter the following piece of code there:

cmake_minimum_required(VERSION 3.1)
find_package(OpenCV REQUIRED) add_executable(opencv_example main.cpp)
# Link your application with OpenCV libraries
target_link_libraries(opencv_example “{OpenCV_LIBS}”)

We now need to build the application using this CMake file. To do this open a terminal window in the application’s root folder and enter the following commands:
cd src
mkdir -p build && cd build
cmake ..

This would output an executable file in the directory which can be used to view images given their path.