C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

C ++を使用してOpenCVでリアルタイムで人間の顔を検出する方法は?


リアルタイムで顔を検出することは、静止画で顔を検出することに似ています。唯一の違いはリアルタイムの顔検出であり、コンピューターのビデオストリームを取得する必要があります。このプログラムでは、「VideoCapture()」関数を使用しました。この機能は、他のカメラからビデオをキャプチャし、割り当てられたマトリックスにフレームを一時的に保存します。ここで、この関数はデフォルトのカメラからビデオをキャプチャし、フレームを「real_time」マトリックスに一時的に保存します。

次のプログラムは、人間の顔をリアルタイムで検出します-

#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 video_stream;//Declaring a matrix hold frames from video stream//
   VideoCapture real_time(0);//capturing video from default webcam//
   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//
   while (true) {
      faceDetector.detectMultiScale(video_stream, faces, 1.1, 4, CASCADE_SCALE_IMAGE, Size(30, 30));//Detecting the faces in 'image_with_humanfaces' matrix//
      real_time.read(video_stream);// reading frames from camera and loading them in 'video_stream' Matrix//
      for (int i = 0; i < faces.size(); i++){ //for locating the face
         Mat faceROI = video_stream(faces[i]);//Storing face in the matrix//
         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(video_stream, Point(x, y), Point(w, h), Scalar(255, 0, 255), 2, 8, 0);//Drawing a rectangle using around the faces//
      }
      imshow("Face Detection", video_stream);
      //Showing the detected face//
      if (waitKey(10) == 27){ //wait time for each frame is 10 milliseconds//
         break;
      }
   }
   return 0;
}

出力

C ++を使用してOpenCVでリアルタイムで人間の顔を検出する方法は?


  1. C ++を使用してOpenCVでバイナリイメージを作成するにはどうすればよいですか?

    バイナリイメージは、白黒の2色を表す単なるデジタルイメージです。画像処理の観点から、バイナリ画像には、0と1の2つの可能な値を持つピクセルが含まれています。ピクセルの値が0の場合、それは純粋な黒色を表します。ピクセルの値が1の場合、それは純粋な白色を意味します。 グレースケール画像では、それぞれに256の異なる可能な値があります。しかし、バイナリイメージでは、可能な値は2つだけです。バイナリイメージには、さまざまなタイプのアプリケーションがあります。たとえば、形態学的変換には2値画像が必要であり、背景からのオブジェクト形状の抽出には2値画像が必要です。OpenCVを使用すると、画像を2値画像

  2. newを使用してC++で2D配列を宣言するにはどうすればよいですか

    動的2D配列は、基本的に配列へのポインターの配列です。これは、寸法が3x4の2D配列の図です。 アルゴリズム Begin    Declare dimension of the array.    Dynamic allocate 2D array a[][] using new.    Fill the array with the elements.    Print the array.    Clear the memory by deleting it. End サンプルコード