首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在摄像头捕获的图像上实现PCA?

如何在摄像头捕获的图像上实现PCA?
EN

Stack Overflow用户
提问于 2012-01-20 15:45:09
回答 1查看 1.1K关注 0票数 1

我已经成功地在我的人脸识别project.Now中实现了人脸检测部分我在image.Now中有一个矩形的人脸区域我必须在这个检测到的矩形区域上实现PCA来提取重要的特征我用过在人脸数据库上实现PCA的例子我想知道我们如何将检测到的人脸传递给实现PCA的函数?我们是否通过矩形框架?这是我的人脸检测代码。

代码语言:javascript
复制
#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 );
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-30 23:25:19

编辑:

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

原文:

我假设你的问题是下面的。您已经使用了OpenCV附带的级联分类器cv::CascadeClassifier来检测和提取图像中的人脸。现在,您希望对图像执行人脸识别。

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

一旦你计算了特征面,你就可以从你的模型中得到预测。只需在计算模型上调用Eigenfaces::predict即可。how to get a prediction for an image中的main.cpp向您展示了如何使用该类及其方法(用于预测、投影、重建图像)。

现在我知道你的问题所在了。您正在使用旧的OpenCV C应用编程接口。这使得我的代码所用到的新的OpenCV2 C++ API很难接口。无意冒犯,但如果您想与我的代码交互,最好使用OpenCV2 C++应用程序接口。我不能在这里给出学习C++和OpenCV2应用编程接口的指南,OpenCV附带了很多文档。一个很好的开始是OpenCV C++小抄(也可以在http://opencv.willowgarage.com/上找到)或OpenCV参考手册。

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8938207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档