PCA如何在相机捕获的图像上实现? [英] how is PCA implemented on a camera captured image?

查看:124
本文介绍了PCA如何在相机捕获的图像上实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的人脸识别项目中成功实现了人脸检测部分。现在我有一个图像中的矩形区域的面部。现在我必须实现PCA在这个检测到的矩形区域提取重要的功能。我已经使用示例实现PCA在面部数据库上。我想知道我们如何通过我们的检测到的面部功能实现PCA?是我们通过矩形框架吗?
这是我的面部检测的代码。

  #includecv.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>


//创建一个包含精确级联名的字符串
const char * cascade_name =
haarcascade_frontalface_alt.xml;
/ *haarcascade_profileface.xml; * /


//从图像中检测和绘制对象的函数原型
void detect_and_draw(IplImage * image) ;

//主函数,定义程序的入口点。
int main(int argc,char ** argv)
{

//创建示例图像
IplImage * img = cvLoadImage(Image018.jpg) ;
if(!img)
{
printf(could not load image);
return -1;
}

//调用函数来检测和绘制面部位置
detect_and_draw(img);

//在退出程序之前等待用户输入
cvWaitKey();

//释放图像
cvReleaseImage(& img);

//销毁以前用文件名:result创建的窗口
cvDestroyWindow(result);

//返回0表示程序成功执行
return 0;
}

//检测和绘制图像中存在的任何面的函数
void detect_and_draw(IplImage * img)
{

//为计算创建内存
static CvMemStorage * storage = 0;

//创建一个新的Haar分类器
static CvHaarClassifierCascade * cascade = 0;

int scale = 1;

//基于输入图像创建新图像
IplImage * temp = cvCreateImage(cvSize(img-> width / scale,img-> height / scale),8, 3);

//创建两个点来表示面部位置
CvPoint pt1,pt2;
int i;

//加载HaarClassifierCascade
cascade =(CvHaarClassifierCascade *)cvLoad(cascade_name,0,0,0);

//检查级联是否已成功加载。 Else报告和错误并退出
if(!cascade)
{
fprintf(stderr,ERROR:Could not load classifier cascade\\\
);
return;
}

//分配内存存储
storage = cvCreateMemStorage(0);

//创建一个带有标题的新命名窗口:result
cvNamedWindow(result,1);

//清除在
之前使用的内存存储cvClearMemStorage(storage);

//查找级联是否加载,以找到面。如果是,则:
if(cascade)
{

//图像中可以有多个面。所以创建一个可扩展的面序列。
//检测对象并将它们存储在序列中
CvSeq * faces = cvHaarDetectObjects(img,cascade,storage,
1.1,2,CV_HAAR_DO_CANNY_PRUNING,
cvSize ));

//循环找到的面数。
for(i = 0; i <(faces?faces-> total:0); i ++)
{
//为绘制面创建一个新矩形
CvRect * r =(CvRect *)cvGetSeqElem(faces,i);

//找到面部的尺寸,必要时缩放
pt1.x = r-> x * scale;
pt2.x =(r-> x + r-> width)* scale;
pt1.y = r-> y * scale;
pt2.y =(r-> y + r-> height)* scale;

//在输入图像中绘制矩形
cvRectangle(img,pt1,pt2,CV_RGB(255,0,0),3,8,0);
}
}

//在名为result的窗口中显示图像
cvShowImage(result,img);

//释放创建的临时映像。
cvReleaseImage(& temp);
}


解决方案



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





原始帖子:



我假设你的问题是以下。您使用了OpenCV提供的Cascade分类器 cv :: CascadeClassifier 检测和提取图像中的面部。现在,您要对图像执行人脸识别。



您要使用Eigenfaces进行人脸识别。所以你要做的第一件事是从你收集的图像学习Eigenfaces。我重写了 Eigenfaces类,以使其更简单。要学习特征面简单地传递一个矢量与你的脸部图像和相应的标签(主题)要么 Eigenfaces :: Eigenfaces Eigenfaces :: compute 。确保所有图片大小相同,您可以使用 cv :: resize 来确保这一点。



计算Eigenfaces后,可以从模型中获取预测。只需在计算的 Eigenfaces :: predict 上模型。 main.cpp 向您展示如何使用类及其方法(用于图像的预测,投影,重建),这里是如何获取图像的预测



现在我看到你的问题在哪里。您正在使用旧的OpenCV C API。这使得很难与我的代码写入的新的OpenCV2 C ++ API接口。不是冒犯,但是如果你想与我的代码接口,最好使用OpenCV2 C ++ API。我不能在这里学习C ++和OpenCV2 API的指南,OpenCV有很多文档。一个好的开始是OpenCV C ++作弊表(也可以从 http://opencv.willowgarage.com/ 获得)或OpenCV参考手册。



为了识别来自级联检测器的图像,我重复一次:首先学习Eigenfaces模型与你想要识别的人,与我的代码。然后,您需要获得感兴趣区域(ROI),即面部,级联检测器输出的矩形。最后,你可以从Eigenfaces模型(你已经计算了它的上面)得到一个ROI的预测,它显示在我的代码的例子。你可能必须将你的图像转换为灰度,但这是所有。这是怎么做的。


I have successfully implemented face detection part in my Face Recognition project.Now i have a rectangular region of face in an image.Now i have to implement PCA on this detected rectangular region to extract important features.I have used examples of implementing PCA on face databases.I want to know how we can pass our detected face to function implementing PCA?Is it that we pass the rectangle frame? This is the code for my face detection.

#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 );
}

解决方案

Edit:

Just to notify anyone visiting this site. I have written some sample code to perform face recognition in videos using my libfacerec library:

Original post:

I assume your problem is the following. You've used the Cascade Classifier cv::CascadeClassifier coming with OpenCV to detect and extract faces from images. Now you want to perform a face recognition on the images.

You want to use the Eigenfaces for face recognition. So the first thing you have to do is to learn the Eigenfaces from the images you've gathered. I rewrote the Eigenfaces class for you to make it simpler. To learn the eigenfaces simply pass a vector with your face images and the corresponding labels (the subject) either to Eigenfaces::Eigenfaces or Eigenfaces::compute. Make sure all your images have the same size, you can use cv::resize to ensure this.

Once you have computed the Eigenfaces, you can get predictions from your model. Simply call Eigenfaces::predict on a computed model. The main.cpp shows you how to use the class and its methods (for prediction, projection, reconstruction of images), here's how to get a prediction for an image.

Now I see where your problem is. You are using the old OpenCV C API. That makes it's hard to interface with the new OpenCV2 C++ API my code is written in. Not to be offending, but if you want to interface with my code you better use the OpenCV2 C++ API. I can't give a guide on learning C++ and the OpenCV2 API here, there's a lot of documentation coming with OpenCV. A good start is the OpenCV C++ Cheat Sheet (also available at http://opencv.willowgarage.com/) or the OpenCV Reference Manual.

For recognizing images from the Cascade Detector, I repeat: First learn the Eigenfaces model with the persons you want to recognize, it's shown in the example coming with my code. Then you need to get the Region Of Interest (ROI), that's the face, the Rectangle the Cascade Detector outputs. Finally you can get a prediction for the ROI from the Eigenfaces model (you have computed it above), it's shown in the example coming with my code. You probably have to convert your image to grayscale, but that's all. That's how it's done.

这篇关于PCA如何在相机捕获的图像上实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆