【发布时间】: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<int>或库中的某个类)。 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