1

顔認識プロジェクトで顔検出部分を正常に実装しました。これで、画像に顔の長方形の領域ができました。重要な特徴を抽出するには、この検出された長方形の領域にPCAを実装する必要があります。顔にPCAを実装する例を使用しました。検出された顔をPCAを実装する機能に渡す方法を知りたいですか?長方形のフレームを渡すのですか?これは私の顔検出のコードです。

#include "cv.h"
#include "highgui.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>


// Create a string that contains the exact cascade name
const char* cascade_name =
    "haarcascade_frontalface_alt.xml";
/*    "haarcascade_profileface.xml";*/


// Function prototype for detecting and drawing an object from an image
void detect_and_draw( IplImage* image );

// Main function, defines the entry point for the program.
int main( int argc, char** argv )
{

    // Create a sample image
    IplImage *img = cvLoadImage("Image018.jpg");
    if(!img)
    {
        printf("could not load image");
        return -1;
    }

    // Call the function to detect and draw the face positions
    detect_and_draw(img);

    // Wait for user input before quitting the program
    cvWaitKey();

    // Release the image
    cvReleaseImage(&img);

    // Destroy the window previously created with filename: "result"
    cvDestroyWindow("result");

    // return 0 to indicate successfull execution of the program
    return 0;
}

// Function to detect and draw any faces that is present in an image
void detect_and_draw( IplImage* img )
{

    // Create memory for calculations
    static CvMemStorage* storage = 0;

    // Create a new Haar classifier
    static CvHaarClassifierCascade* cascade = 0;

    int scale = 1;

    // Create a new image based on the input image
    IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );

    // Create two points to represent the face locations
    CvPoint pt1, pt2;
    int i;

    // Load the HaarClassifierCascade
    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );

    // Check whether the cascade has loaded successfully. Else report and error and quit
    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        return;
    }

    // Allocate the memory storage
    storage = cvCreateMemStorage(0);

    // Create a new named window with title: result
    cvNamedWindow( "result", 1 );

    // Clear the memory storage which was used before
    cvClearMemStorage( storage );

    // Find whether the cascade is loaded, to find the faces. If yes, then:
    if( cascade )
    {

        // There can be more than one face in an image. So create a growable sequence of faces.
        // Detect the objects and store them in the sequence
        CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
                                            1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
                                            cvSize(40, 40) );

        // Loop the number of faces found.
        for( i = 0; i < (faces ? faces->total : 0); i++ )
        {
           // Create a new rectangle for drawing the face
            CvRect* r = (CvRect*)cvGetSeqElem( faces, i );

            // Find the dimensions of the face,and scale it if necessary
            pt1.x = r->x*scale;
            pt2.x = (r->x+r->width)*scale;
            pt1.y = r->y*scale;
            pt2.y = (r->y+r->height)*scale;

            // Draw the rectangle in the input image
            cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
        }
    }

    // Show the image in the window named "result"
    cvShowImage( "result", img );

    // Release the temp image created.
    cvReleaseImage( &temp );
}
4

1 に答える 1

4

編集:

このサイトにアクセスする人に通知するためだけに。libfacerec ライブラリを使用して、ビデオで顔認識を実行するサンプル コードをいくつか作成しました。

元の投稿:

あなたの問題は次のとおりだと思います。OpenCV に付属している Cascade Classifier cv::CascadeClassifierを使用して、画像から顔を検出して抽出しました。次に、画像に対して顔認識を実行します。

顔認識に Eigenfaces を使用したいと考えています。したがって、最初に行う必要があるのは、収集した画像から固有顔を学習することです。より簡単にするために、 Eigenfaces クラスを書き直しました。固有顔を学習するには、顔画像と対応するラベル (サブジェクト) を含むベクトルをEigenfaces::EigenfacesまたはEigenfaces::computeに渡すだけです。cv::resizeを使用して、すべての画像が同じサイズであることを確認してください。

固有顔を計算したら、モデルから予測を取得できます。計算されたモデルでEigenfaces::predictを呼び出すだけです。main.cppは、クラスとそのメソッド (予測、投影、画像の再構成) の使用方法を示しています。画像の予測を取得する方法は次のとおりです。

今、私はあなたの問題がどこにあるかがわかります。古い OpenCV C API を使用しています。そのため、私のコードが記述されている新しい OpenCV2 C++ API とのインターフェイスが難しくなります。気分を害するわけではありませんが、私のコードとインターフェイスしたい場合は、OpenCV2 C++ API を使用することをお勧めします。ここで C++ と OpenCV2 API の学習に関するガイドを提供することはできません。OpenCV には多くのドキュメントが付属しています。OpenCV C++ チート シート ( http://opencv.willowgarage.com/でも入手可能) または OpenCV リファレンス マニュアルから始めるとよいでしょう。

Cascade Detector からの画像を認識するために、繰り返します。まず、認識したい人物の Eigenfaces モデルを学習します。これは、私のコードに付属の例に示されています。次に、関心領域 (ROI) を取得する必要があります。これは面であり、Cascade Detector が出力する Rectangle です。最後に、Eigenfaces モデル (上記で計算済み) から ROI の予測を取得できます。これは、私のコードに付属の例に示されています。おそらく画像をグレースケールに変換する必要がありますが、それだけです。それがその方法です。

于 2012-01-30T15:25:19.317 に答える