我尝试提取面部数据库的面部特征,但我认识到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;
}
我正在使用,
我是否必须使用我的数据库训练算法?是否可以在不训练算法的情况下解决此问题?如果不是我怎么用AdaBoost训练呢?
答案 0 :(得分:0)
最好在检测到的面部区域中搜索眼睛(和其他面部元素),而不是整个图像。您可以在opencv的文件夹中找到示例:
的OpenCV \样品\ CPP \ tutorial_code \ objectDetection \
我建议你去新的API。您使用的API已过时,将来不再受支持。
答案 1 :(得分:0)
Viola-Jones算法不是一种可靠的方法。有时它会检测到,有时它会检测到,就像那样。但是,由于您正在使用正面图像,因此您不会遇到重大问题,因为这些分类器是在正面图像上进行训练的。检测过程在很大程度上取决于图片的质量(光线不好会导致问题)。