我尝试从人脸数据库中提取面部特征,但我认识到Viola-Jones算法在两种情况下不起作用:
- 当我试图单独检测眼睛时,
- 当我试图检测口腔时。
*不正常:检测图像的不同部分,如眼睛或嘴巴。或者有时发现几个,这是一个不可能的情况。我使用的图片有纯绿色背景,包含一个人的正面。
Detection.cpp:
#include "Detection.h"
Detection::Detection(const char* imagePath, const char* detectorType)
{
pImage_ = cvLoadImage(imagePath, CV_LOAD_IMAGE_COLOR);
pStorage_ = cvCreateMemStorage(0);
pCascade_ = (CvHaarClassifierCascade* ) cvLoad(detectorType,0,0,0);
if(!pImage_ || !pStorage_ || !pCascade_)
{
std::cout << "Problem with Loading Image" << std::endl;
exit(-1);
}
// Detect Faces in Image
pFaceRectSeq_ = cvHaarDetectObjects(pImage_, pCascade_, pStorage_, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING, cvSize(0,0));
// Create a Window To Display Detected Faces
cvNamedWindow("Detected", CV_WINDOW_AUTOSIZE);
// Draw a Rectengular Outline Around Each Detection
for(int i = 0; i < (pFaceRectSeq_ ? pFaceRectSeq_->total : 0); i++)
{
CvRect* r = (CvRect*) cvGetSeqElem(pFaceRectSeq_,0);
CvPoint pt1 = { r->x, r->y };
CvPoint pt2 = { r->x + r->width, r->y + r->height };
cvRectangle(pImage_, pt1, pt2, CV_RGB(0,255,0), 3, 4, 0);
}
// r can be saved to a file/database as feature set
cvShowImage("Detected", pImage_);
cvWaitKey(0);
}
Detection::~Detection()
{
cvDestroyWindow("Detected");
cvReleaseImage(&pImage_);
if(pCascade_)
cvReleaseHaarClassifierCascade(&pCascade_);
if(pStorage_)
cvReleaseMemStorage(&pStorage_);
}
void Detection::SaveFaceFeatures(char* fileName)
{
}
main.cpp:
#include <iostream>
#include "Detection.h"
const char* imagePath = "C:/1.jpg";
const char* faceDetector = "C:/opencv/data/haarcascades/haarcascade_frontalface_default.xml";
const char* eyesDetector = "C:/opencv/data/haarcascades/haarcascade_mcs_eyepair_big.xml";
const char* righteyeDetector = "C:/opencv/data/haarcascades/haarcascade_mcs_righteye.xml";
const char* leftEyeDetector = "C:/opencv/data/haarcascades/haarcascade_mcs_lefteye.xml";
const char* noseDetector = "C:/opencv/data/haarcascades/haarcascade_mcs_nose.xml";
const char* mouthDetector = "C:/opencv/data/haarcascades/haarcascade_mcs_mouth.xml";
int main(int argc, char* argv[])
{
Detection *face = new Detection(imagePath, faceDetector);
//face->SaveFaceFeatures("01-1mFeatures.txt");
Detection *eyes = new Detection(imagePath, eyesDetector);
//Detection *rightEye = new Detection(imagePath, righteyeDetector);
//Detection *leftEye = new Detection(imagePath, leftEyeDetector);
Detection *nose = new Detection(imagePath, noseDetector);
//Detection *mouth = new Detection(imagePath, mouthDetector);
return 0;
}
我使用,
- haarcascade_mcs_righye .xml右眼检测,
- haarcascade_mcs_lefteye.xml左眼检测
- haarcascade_mcs_mouth.xml用于口腔检测。
我必须用我的数据库训练算法吗?有没有可能在不训练算法的情况下解决这个问题?如果不是,我怎么用AdaBoost训练它?
最好在检测到的人脸区域中搜索眼睛(和其他人脸元素),而不是整个图像。您可以在opencv的文件夹中找到示例:
opencv samples cpp tutorial_code objectDetection
我建议你使用新的API。您使用的API已过时,将来将不支持
Viola-Jones算法不是一种可靠的方法。有时它检测到,有时它不会,就像这样。但由于你使用的是正面人脸图像,你不会有大问题,因为那些分类器是在正面人脸图像上训练的。检测过程很大程度上取决于照片的质量(光线不好会导致问题)。