I am an absolutely newcomer to OpenCV and Haar classifiers. I have copied the following code and it basically works fine, but it only returns one face and one eye. What about the second eye and another face?
const char[] eyesCascadeFilename = "haarcascade_eye.xml";
const char[] faceCascadeFilename = "haarcascade_frontalface_alt2.xml";
const int HaarOptions = CV_HAAR_DO_ROUGH_SEARCH;
// prepare the image for fast processing
Mat grayImage;
cvtColor(colorImage, grayImage, CV_BGR2GRAY);
equalizeHist(grayImage, grayImage);
// detect the faces on the image
std::vector<cv::Rect> faces;
faceCascade.detectMultiScale(grayImage, faces, 1.1, 2, HaarOptions, cv::Size(60, 60));
for (int i = 0; i < faces.size(); i++) {
// visualize the faces
cv::Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
cv::Point pt2(faces[i].x, faces[i].y);
cv::rectangle(colorImage, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8 ,0);
// detect the eyes within the facial roi
cv::Rect rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
cv::Mat roi = grayImage(rect);
std::vector<cv::Rect> eyes;
// here's a problem ...:
eyesCascade.detectMultiScale(roi, eyes, 1.1, 2, HaarOptions, cv::Size(30, 30));
//eyesCascade.detectMultiScale(roi, eyes);
for (int i = 0; i < eyes.size(); i++) {
// visualize the eyes
cv::Point pt1(faces[i].x + eyes[i].x + eyes[i].width, faces[i].y + eyes[i].y + eyes[i].height);
cv::Point pt2(faces[i].x + eyes[i].x, faces[i].y + eyes[i].y);
cv::rectangle(colorImage, pt1, pt2, cvScalar(0, 210, 255, 0), 1, 8 ,0);
}
}
Here's what it looks like: