PCA是如何在相机捕获的图像上实现的

how is PCA implemented on a camera captured image?

本文关键字:图像 实现 相机 PCA      更新时间:2023-10-16

我已经成功地在我的人脸识别项目中实现了人脸检测部分。现在我在图像中有一个矩形的面部区域。现在我必须在这个检测到的矩形区域上实现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 cascaden" );
        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 );
}

编辑:

只是为了通知任何访问此网站的人。我已经编写了一些示例代码,使用我的libfacerec库在视频中执行人脸识别:

  • https://github.com/bytefish/libfacerec/blob/master/samples/facerec_video.cpp

原帖:

我想你的问题如下。您使用了OpenCV附带的级联分类器cv::CascadeClassifier来检测和提取图像中的人脸。现在您要对图像执行人脸识别。

您希望使用特征人脸进行人脸识别。所以你要做的第一件事就是从你收集的图像中学习特征面。我为您重写了Eigenfaces类,使其更简单。要学习本征面,只需将带有人脸图像和相应标签(主题)的向量传递给本征面::本征面或本征面计算。确保你所有的图像都有相同的大小,你可以使用cv::resize来确保这一点。

一旦计算出特征面,就可以从模型中获得预测。简单地调用Eigenfaces::在计算模型上预测。main.cpp向您展示了如何使用该类及其方法(用于图像的预测、投影、重建),下面是如何获得图像的预测。

现在我知道你的问题在哪里了。你正在使用旧的OpenCV C API。这使得我的代码很难与新的OpenCV2 C++API接口。这并不是冒犯,但如果你想与我的代码接口,你最好使用OpenCV2 C++nenenebb API。我不能在这里给出学习C++和OpenCV2 API的指南,OpenCV附带了很多文档。一个良好的开端是OpenCV C++备忘单(也可在http://opencv.willowgarage.com/)或OpenCV参考手册。

对于从级联检测器识别图像,我重复一遍:首先学习你想识别的人的特征面模型,它在我的代码附带的示例中显示。然后你需要得到感兴趣区域(ROI),也就是人脸,级联检测器输出的矩形。最后,您可以从Eigenfaces模型中获得ROI的预测(您已经在上面计算了它),它在我的代码附带的示例中显示。您可能需要将图像转换为灰度,但仅此而已。事情就是这样做的。