Face Detection using OpenCV and C++ part 1

Today we are going to see how we can use OpenCV with C++ for face detection. Well, I know most of you heard about Opencv as it is a very popular library when it comes to Computer Vision. But for those who are not familiar with it, let me introduce you to a little bit about OpenCV.


OpenCV (Open-Source Computer Vision Library) is a free software library for computer vision and machine learning. The library was created to provide a common infrastructure for computer vision applications and to help commercial goods incorporate machine perception more quickly. As it is a BSD-licensed product, it is simple for businesses to use and alter the code. More than 2500 optimized algorithms are in the library, which contains a comprehensive mix of both classic and cutting-edge computer vision and machine learning techniques.

These algorithms can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects, extract 3D models of objects, produce 3D point clouds from stereo cameras, stitch images together to produce a high-resolution image of an entire scene, remove red eyes from images taken with flash, follow eye movements, recognize scenery, and establish markers to overlay. It has a user group of about 47 thousand members and an estimated number of downloads of over 18 million.

Setting up the project.

Let’s get started by setting up the project. I am using Visual Studio 2019 for this tutorial; you can find the download link here. https://visualstudio.microsoft.com/downloads/

Next, we download the library. Go to the following page and download Open CV. https://opencv.org/releases/

Once it downloaded, open it and extract it to the directory you want.  

Next, where you have extracted open cv, go to that folder then build, then x64 and then vc14 and then finally in bin, copy the path from window explorer.

In my case it is: C:\Users\malih\Downloads\opencv\build\x64\vc14\bin

Go to Environment Variables and then system variables and add new path.

Create another empty project in Visual Studio 2019.  

And we will add source.cpp file by right click on source files and then clicking on CPP File, name the file and click ok. 

We need to add open-cv library to our project.

Right Click on Project Name and click on properties

Then Click on C/C++ and then on General

Click on Additional Include Directories and then on edit and finally paste path to include directory of open cv (path where you extracted open cv)

In my case it is C:\Users\malih\Downloads\opencv\build\include

  • Now lick on Preprocessor on the left panel and then on Preprocessor Definitions, click on edit and then add the following line
  • Now Click on Linker -> General -> Additional Library Directories and then click on Edit and add the path to opencv\build\x64\vc14\lib
  • Finally, click on Input under Linker and then Additional Dependencies and now add the library name in it.
    • For lib name go to opencv\build\x64\vc14\lib and copy the name of the following library

We have set up OpenCV with c++ project. In the next tutorial, we will write code for the detection of faces using Open CV and C++. Meanwhile, you can explore more about Open CV at https://opencv.org/releases/. Keep supporting us and Thanks for reading. 

Scroll to Top