PCA是如何在相机捕获的图像上实现的
how is PCA implemented on a camera captured image?
我已经成功地在我的人脸识别项目中实现了人脸检测部分。现在我在图像中有一个矩形的面部区域。现在我必须在这个检测到的矩形区域上实现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的预测(您已经在上面计算了它),它在我的代码附带的示例中显示。您可能需要将图像转换为灰度,但仅此而已。事情就是这样做的。
- 如果没有malloc,链表实现将失败
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 是否有彩色图像的Surf_cuda实现
- 在C 或MATLAB中用于图像的CRF实现的代码
- 与C 的图像插值实现
- 如何在Python中实现ITK映像和SimpleItk图像之间的转换
- C++ 模板复杂图像读取类执行时间慢,声明和实现分离
- C# 中的 pHash 图像比较实现
- C++中的快速图像(或矩阵)转置实现
- PCA是如何在相机捕获的图像上实现的
- 实现图像轮廓绘制的基本思想
- 用c++实现图像的高度和宽度
- 如何在不使用准备好的OpenCv函数的情况下在c++中实现对图像的不锐利遮罩
- 我应该如何去实现算法与黑白和彩色图像使用
- 使用c++代码实现图像的颜色切换
- 图像处理中非局部均值降噪算法的实现
- 如何实现网格/平铺图像视图与滚动在BB10