
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to count the number of faces in OpenCV using C++?
Counting the number of faces located in an image is easy. The program we wrote in the previous section already have the information of number of faces in 'faces.size()'. This code-'faces.size()' gives an integer value.
For example, if we write 'int x = faces.size()', the 'x' will contain the number of faces.
The following program calculates the number of faces from a given image and shows it in console window.
Example
#include<iostream> #include<opencv2/highgui/highgui.hpp> #include<opencv2/objdetect/objdetect.hpp> //This header includes the definition of Cascade Classifier// #include<string> using namespace std; using namespace cv; int main(int argc, char** argv){ Mat image_with_humanface;//Declaring a matrix to load image with human faces// image_with_humanface = imread("friends.jpg");//loading an image that contains human face in it// string trained_classifier_location = "C:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml";//Defining the location our XML Trained Classifier in a string// CascadeClassifier faceDetector;//Declaring an object named 'face detector' of CascadeClassifier class// faceDetector.load(trained_classifier_location);//loading the XML trained classifier in the object// vector<Rect>faces;//Declaring a rectangular vector named faces// faceDetector.detectMultiScale(image_with_humanface, faces, 1.1, 4, CASCADE_SCALE_IMAGE, Size(20, 20));//Detecting the faces in 'image_with_humanfaces' matrix// int x = faces.size();//Calculating the number of faces and storing the integer value in x// cout << "Number of face(s)in the image=" << x << endl;//Displaying the value of x in the console window// system("pause");//Pausing the system to visualize the result// return 0; }
Output
- Related Articles
- How to crop the detected faces in OpenCV using C++?
- How to count the total number of frames in OpenCV using C++?
- How to blur faces in an image using OpenCV Python?
- How to detect faces in an image using Java OpenCV library?
- How to detect human faces in real-time in OpenCV using C++?
- How to detect cat faces in an image in OpenCV using Python?
- How to crop and save the detected faces in OpenCV Python?
- How to calculate the number of channels of an image in OpenCV using C++?
- PHP – How to count the number of substring using mb_substr_count()?
- How to Count the Number of Vowels in a string using Python?
- How to count the number of words in a text file using Java?
- How to count the number of lines in a text file using Java?
- A polyhedron has 32 edges and the number of its faces is 5 less than twice the number of its vertices. What is the number of its faces?
- How to count the number of times a button is clicked using JavaScript?
- How to track the position of the face in OpenCV using C++?

Advertisements