কম্পিউটার

সি++ ব্যবহার করে ওপেনসিভিতে সনাক্ত করা মুখগুলি কীভাবে ক্রপ করবেন?


আমরা জানব কিভাবে OpenCV-তে সনাক্ত করা মুখগুলি ক্রপ করতে হয়। শনাক্ত করা মুখগুলি ক্রপ করতে, আমাদের একাধিক ম্যাট্রিক্সের প্রয়োজন৷ সবচেয়ে উপযুক্ত উপায় হল একটি ইমেজ অ্যারে ব্যবহার করা। নিম্নলিখিত দুটি লাইন ব্যবহার করে এই প্রোগ্রামে, আমরা দুটি চিত্র ম্যাট্রিক্স −

ঘোষণা করেছি
  • ম্যাট ক্রপড_ফেস[4];
  • ম্যাট faceROI[4];

প্রথম ম্যাট্রিক্স হল ক্রপ করা ছবি সংরক্ষণ করা, এবং দ্বিতীয় ম্যাট্রিক্স হল আগ্রহের অঞ্চলকে সংজ্ঞায়িত করা। সনাক্তকরণ প্রক্রিয়ায়, প্রথমে, প্রোগ্রামটি মুখগুলি সনাক্ত করে এবং সেগুলিকে ভেক্টরে সংরক্ষণ করে। আমাদের প্রোগ্রামে, ভেক্টরের নাম 'মুখ' ভেক্টর একাধিক উপাদান থাকতে পারে।

নিম্নলিখিত দুটি লাইন ব্যবহার করে, আমরা ভেক্টর সনাক্ত করি এবং চিত্রে তাদের অবস্থান সনাক্ত করি এবং অবশেষে 'faceROI[i]' ম্যাট্রিক্সে মুখের অঞ্চলটি ক্রপ করি।

  • faceROI[]=image_with_humanface(faces[i]);
  • cropped_faces[i]=faceROI[i];

প্রথম লাইনটি 'image_with_humanface' নামের ইমেজে মুখ সম্বলিত ভেক্টরটি সনাক্ত করে এবং এটিকে ক্রপ করে 'faceROI[i]' নামের ম্যাট্রিক্সে সংরক্ষণ করে। দ্বিতীয় লাইনে, ক্রপ করা ছবিগুলো অন্য ম্যাট্রিক্স অ্যারেতে পাঠানো হচ্ছে। এই ম্যাট্রিক্স অ্যারেটি ক্রপ করা ছবিগুলি দেখানোর জন্য ব্যবহার করা হয়েছে৷

নিম্নলিখিত প্রোগ্রাম সনাক্ত করা মুখগুলি ক্রপ করে এবং আলাদা উইন্ডোতে দেখায়৷

উদাহরণ

#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//
   Mat cropped_faces[3];//Declaring an array of matrix of 4 elements to show the cropped faces//
   Mat faceROI[3];//Declaring an array of matrix of 4 elements to hold the cropped faces//
   image_with_humanface = imread("friends3.jpg");//loading an image that contains human face in it//
   namedWindow("Face1");//Declaring an window to show 1st cropped face//
   namedWindow("Face2");//Declaring an window to show 2nd cropped face//
   namedWindow("Face3");//Declaring an window to show 3rd cropped face//  
   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//
   vector<Rect>boundary;//Declaring a rectangular vector named rectangle//
   faceDetector.detectMultiScale(image_with_humanface, faces, 1.1, 4, CASCADE_SCALE_IMAGE, Size(20, 20));//Detecting the faces in 'image_with_humanfaces' matrix//
   for (size_t i = 0; i < faces.size(); i++){ //Loop to draw rectangle around the faces//
      faceROI[i] = image_with_humanface(faces[i]);
      cropped_faces[i] = faceROI[i];
      int x = faces[i].x;//Getting the initial row value of face rectangle's starting point//
      int y = faces[i].y;//Getting the initial column value of face rectangle's starting point//
      int h = y + faces[i].height;//Calculating the height of the rectangle//
      int w = x + faces[i].width;//Calculating the width of the rectangle//
      rectangle(image_with_humanface, Point(x, y), Point(w, h), Scalar(255, 0, 255), 2, 8, 0);//Drawing a rectangle using around the faces//
   }
   imshow("Face1", cropped_faces[0]);//Showing the 1st cropped face//
   imshow("Face2", cropped_faces[1]);//Showing the 2nd cropped face//
   imshow("Face3", cropped_faces[2]);//Showing the 3rd cropped face//
   waitKey(0);//To wait for a keystroke to terminate the program
   return 0;
}

আউটপুট

সি++ ব্যবহার করে ওপেনসিভিতে সনাক্ত করা মুখগুলি কীভাবে ক্রপ করবেন?


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

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

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

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