【问题标题】:how is PCA implemented on a camera captured image?PCA 如何在相机捕获的图像上实现?
【发布时间】:2012-02-14 19:54:24
【问题描述】:

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

【问题讨论】:

  • 你如何在程序中用人脸来表示你的图像和区域?
  • @BjörnPollex-我使用了相机的帧并直接从文件中读取图像。面部是一个矩形对象,绘制在图像上。
  • 您的问题缺少很多信息。您如何存储帧(数组、vector&lt;int&gt; 或库中的某个类)。 PCA 的代码是什么?请尽量具体。
  • @BjörnPollex-对于面部检测,我使用了这里给出的代码opencv.willowgarage.com/wiki/FaceRecognition.I 没有为我的项目实现 PCA。我已经阅读了github.com/bytefish/opencv/blob/master/eigenfaces/… 给出的代码,并且在该代码中它直接使用AT&T 数据库中的人脸图像。那张脸是 pgm 格式的。所以我将检测到的人脸作为可以转换为矩阵的矩形区域。但是我们可以使用该矩阵而不是该 pgm 格式图像吗?
  • @BjörnPollex-Sir 我已经更新了我的问题,给出了确切的代码。你能帮忙吗?

标签: c++ face-recognition


【解决方案1】:

编辑:

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

原帖:

我认为您的问题如下。您已经使用 OpenCV 附带的 Cascade Classifier cv::CascadeClassifier 从图像中检测和提取人脸。现在您要对图像执行人脸识别。

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

计算完特征脸后,您就可以从模型中获得预测。只需在计算模型上调用Eigenfaces::predictmain.cpp 向您展示了如何使用该类及其方法(用于图像的预测、投影、重建),这里是 how to get a prediction for an image

现在我知道你的问题出在哪里了。您正在使用旧的 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 输出的矩形。最后,您可以从 Eigenfaces 模型(您已经在上面计算过)获得对 ROI 的预测,它显示在我的代码附带的示例中。您可能必须将图像转换为灰度,但仅此而已。就是这样完成的。

【讨论】:

  • @bytefish-感谢您的回答:) 我在github.com/bytefish/opencv/tree/master/eigenfaces.In 使用了您的特征脸代码,此代码您传递了一个包含来自 ORL 人脸数据库的人脸的文本文件。我可以更改该文本文件吗并通过我提取的脸?
  • 没问题。是的,当然,你可以使用任何你想要的cv::Mat 向量,只要图像具有相同的大小。 CSV 文件只是一个如何加载数据的示例,您当然可以编辑此文件以传递您自己的图像。如果您仍然有问题,请编辑您的帖子并粘贴一些代码。正如@BjörnPollex 指出的那样,没有细节很难做出假设。顺便说一句,如果这是您问题的答案,请接受。
  • 它仍在进行中,但我已经开始制作人脸识别指南:bytefish.de/pdf/facerec.pdf。代码示例位于github.com/bytefish/facerecognition_guide
  • @bytefish-我在github上用过两个版本的代码。前一个有点简单,因为它只包含eigenfaces.h,eigenfaces.cpp和main.cpp。但是新的还有这两个文件 helper.hpp 和 helper.cpp。我尝试运行它并显示错误 helper.cpp(77): error C2572: 'cv::argsort_' : redefinition of default parameter : parameter 2。跨度>
  • 为了接受答案,我正在努力解决我的问题。一旦我完成了,我一定会这样做:)
猜你喜欢
  • 1970-01-01
  • 2016-07-26
  • 2013-02-22
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
相关资源
最近更新 更多