কম্পিউটার

কিভাবে C++ ব্যবহার করে OpenCV-তে সবচেয়ে বড় মুখ সনাক্ত করবেন?


আমরা শিখব কিভাবে শুধুমাত্র সবচেয়ে বড় মুখ সনাক্ত করতে হয়। এই টপিকটি আগের টপিকের মতই। একমাত্র পার্থক্য হল সবচেয়ে বড় মুখ শনাক্ত করতে আমরা একটি অতিরিক্ত 'রেক্ট' গঠন এবং একটি 'ফর লুপ' ব্যবহার করেছি।

এই ফাংশনের প্রকৃত বিন্যাস −

Mat faceROI = image_with_humanface(maxRect)

maxRect-এ চিত্রটিতে অবস্থিত বৃহত্তম মুখের এলাকা এবং অবস্থানের তথ্য রয়েছে। উপরের লাইনটি একই স্থানে maxRect-এ সংরক্ষিত একই জায়গা ক্রপ করছে যেখানে চিত্রটিতে সবচেয়ে বড় মুখটি অবস্থিত এবং 'faceROI' ম্যাট্রিক্সে সংরক্ষণ করা হচ্ছে।

নিম্নলিখিত প্রোগ্রামটি স্থির ছবি থেকে সবচেয়ে বড় মুখ সনাক্ত করে −

উদাহরণ

#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
//This header includes definition of 'rectangle()' function//
#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("person.jpg");//loading an image that contains human face in it//
   namedWindow("Face Detection");//Declaring an window to show the result//  
   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_FIND_BIGGEST_OBJECT);//Detecting the faces in 'image_with_humanfaces' matrix//
   Rect maxRect;//Declaring a rectangle. By default the size is zero pixel//
   for (int i = 0; i < faces.size(); i++){ //Initiating for loop to detect the largest face//
      if (faces[i].area() > maxRect.area()){ //Calculating the area of face and comparing it with area of maxRect//
         maxRect = faces[i];//storing the largest rectangle in MaxRect according to size of largest face//
      }
   }
   for (size_t i = 0; i < faces.size(); i++){ //Loop to draw rectangle around the faces//
      Mat faceROI = image_with_humanface(maxRect);//Storing the face in a matrix having size equal to maxRect//
      int x = maxRect.x;//Getting the initial row value of face rectangle's starting point//
      int y = maxRect.y;//Getting the initial column value of face rectangle's starting point//
      int h = y + maxRect.height;//Calculating the height of the rectangle//
      int w = x + maxRect.width;//Calculating the width of the rectangle//
      rectangle(image_with_humanface, Point(x, y), Point(w, h), Scalar(255, 0, 255), 3, 6, 0);//Drawing a rectangle using around the largest//
   }
   imshow("Face Detection",image_with_humanface);//Showing the largest face face//  
   waitKey(0);//To wait for keystroke to terminate the program
   return 0;
}

আউটপুট

কিভাবে C++ ব্যবহার করে OpenCV-তে সবচেয়ে বড় মুখ সনাক্ত করবেন?


  1. সি++ ব্যবহার করে ওপেনসিভিতে একটি নির্দিষ্ট পিক্সেলের মান কীভাবে পাবেন?

  2. কিভাবে C++ ব্যবহার করে OpenCV-এ একটি উপবৃত্ত আঁকবেন?

  3. কিভাবে C++ ব্যবহার করে OpenCV-এ একটি লাইন আঁকবেন?

  4. C++ ব্যবহার করে ওপেনসিভিতে একটি ছবির চ্যানেলের সংখ্যা কীভাবে গণনা করা যায়?