问题
I try to extract facial features of a face database but I recognized that Viola-Jones algorithm is not working well* in two cases:
- When I try to detect the eyes singly,
- When I try to detect the mouth.
*Not working well: Detects different parts of the image as eyes or mouth. Or sometimes detects several of them which is an impossible case. The images I'm using have pure green background and contains a person's frontal face.
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;
}
I'm using,
- haarcascade_mcs_righteye.xml for right eye detection,
- haarcascade_mcs_lefteye.xml for left eye detection
- haarcascade_mcs_mouth.xml for mouth detection.
Do I have to train the algorithm with my database? Is it possible to fix this problem without training the algorithm? If it is not how can I train it with AdaBoost?
回答1:
It'll be better to search for eyes (and other face elements) in the detected face region, not whole image. You can find example in opencv's folder:
opencv\samples\cpp\tutorial_code\objectDetection\
And I recommend you go to new API. The API you use is obsoleted and will not be supported in future.
回答2:
Viola-Jones algorithm is not a reliable method. Sometimes it detects and some other times it wont,it's like that. But since you are using frontal face images,you wont have major problems because those classifiers are trained on frontal face images. The detection process greatly depends on the quality of your pictures(bad lighting will cause problems).
来源:https://stackoverflow.com/questions/22431348/do-i-have-to-train-viola-jones-algorithm-with-my-database-to-obtain-accurate-res