একটি ছবিতে অবস্থিত মুখের সংখ্যা গণনা করা সহজ। পূর্ববর্তী বিভাগে আমরা যে প্রোগ্রামটি লিখেছিলাম তাতে ইতিমধ্যে 'faces.size()'-এ মুখের সংখ্যার তথ্য রয়েছে। এই কোড-'faces.size()' একটি পূর্ণসংখ্যা মান দেয়।
উদাহরণস্বরূপ, যদি আমরা 'int x =faces.size()' লিখি, তাহলে 'x'-এ মুখের সংখ্যা থাকবে।
নিম্নলিখিত প্রোগ্রামটি একটি প্রদত্ত চিত্র থেকে মুখের সংখ্যা গণনা করে এবং এটি কনসোল উইন্ডোতে দেখায়৷
উদাহরণ
#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;
} আউটপুট
