1 | initial version |
you did not post all your code. i think it is modified from this code
i did a small correction on your code as below, please try it. if the problem persist please post all your code.
int main()
{
CascadeClassifier faceCascade;
CascadeClassifier eyeCascade1;
CascadeClassifier eyeCascade2;
Rect faceRect;
VideoCapture videoCapture ("nn.mp4");
cout << "Face Detection ." << endl;
cout << "Realtime face detection using LBP " << endl;
cout << "Compiled with OpenCV version " << CV_VERSION << endl << endl;
// Load the face and 1 or 2 eye detection XML classifiers.
initDetectors(faceCascade,eyeCascade2);
Mat thresh, gray;
while (1)
{
Mat frame;
videoCapture>> frame;
detectLargestObject(frame, faceCascade, faceRect);
if (faceRect.width > 0)
{ // modification is begin here
Mat faceImg = frame(faceRect);
rectangle(frame, faceRect, CV_RGB(255, 0, 0), 2, CV_AA);
Mat gray;
if (faceImg.channels() == 3) {
cvtColor(faceImg, gray, CV_BGR2GRAY);
}
else if (faceImg.channels() == 4) {
cvtColor(faceImg, gray, CV_BGRA2GRAY);
}
else {
// Access the input image directly, since it is already grayscale.
gray = faceImg;
}
Point leftEye, rightEye;
Rect searchedLeftEye, searchedRightEye;
detectBothEyes(gray, eyeCascade1, eyeCascade2, leftEye, rightEye, & searchedLeftEye, & searchedRightEye);
rectangle(faceImg, searchedLeftEye,Scalar(0, 255, 0), 2, 8, 0);
rectangle(faceImg, searchedRightEye, Scalar(0, 255, 0), 2, 8, 0);
searchedRightEye.y += searchedRightEye.height / 3;
searchedRightEye.height -= searchedRightEye.height / 3;
Mat eye_region=faceImg(searchedRightEye);
cvtColor(eye_region, gray, CV_BGR2GRAY);
threshold(gray, thresh, 60, 255, THRESH_BINARY);
imshow("eye_ video", thresh);
} // modification end
imshow("video", frame);
// Press 'c' to escape
if (waitKey(30) == 'c') break;
}
return 0;
}
2 | No.2 Revision |
EDIT:
i have made some corrections to your code and tested.
final code is here
you can see the changes that i have made here
INITIAL ANSWER
you did not post all your code. i think it is modified from this code
i did a small correction on your code as below, please try it. if the problem persist please post all your code.
int main()
{
CascadeClassifier faceCascade;
CascadeClassifier eyeCascade1;
CascadeClassifier eyeCascade2;
Rect faceRect;
VideoCapture videoCapture ("nn.mp4");
cout << "Face Detection ." << endl;
cout << "Realtime face detection using LBP " << endl;
cout << "Compiled with OpenCV version " << CV_VERSION << endl << endl;
// Load the face and 1 or 2 eye detection XML classifiers.
initDetectors(faceCascade,eyeCascade2);
Mat thresh, gray;
while (1)
{
Mat frame;
videoCapture>> frame;
detectLargestObject(frame, faceCascade, faceRect);
if (faceRect.width > 0)
{ // modification is begin here
Mat faceImg = frame(faceRect);
rectangle(frame, faceRect, CV_RGB(255, 0, 0), 2, CV_AA);
Mat gray;
if (faceImg.channels() == 3) {
cvtColor(faceImg, gray, CV_BGR2GRAY);
}
else if (faceImg.channels() == 4) {
cvtColor(faceImg, gray, CV_BGRA2GRAY);
}
else {
// Access the input image directly, since it is already grayscale.
gray = faceImg;
}
Point leftEye, rightEye;
Rect searchedLeftEye, searchedRightEye;
detectBothEyes(gray, eyeCascade1, eyeCascade2, leftEye, rightEye, & searchedLeftEye, & searchedRightEye);
rectangle(faceImg, searchedLeftEye,Scalar(0, 255, 0), 2, 8, 0);
rectangle(faceImg, searchedRightEye, Scalar(0, 255, 0), 2, 8, 0);
searchedRightEye.y += searchedRightEye.height / 3;
searchedRightEye.height -= searchedRightEye.height / 3;
Mat eye_region=faceImg(searchedRightEye);
cvtColor(eye_region, gray, CV_BGR2GRAY);
threshold(gray, thresh, 60, 255, THRESH_BINARY);
imshow("eye_ video", thresh);
} // modification end
imshow("video", frame);
// Press 'c' to escape
if (waitKey(30) == 'c') break;
}
return 0;
}
3 | No.3 Revision |
EDIT:
i have made some corrections to your code and tested.
final code is here
EDIT 1 : you can see the changes that i have made here
EDIT 2: you can see the changes that i have made here
final code is here
INITIAL ANSWER
you did not post all your code. i think it is modified from this code
i did a small correction on your code as below, please try it. if the problem persist please post all your code.
int main()
{
CascadeClassifier faceCascade;
CascadeClassifier eyeCascade1;
CascadeClassifier eyeCascade2;
Rect faceRect;
VideoCapture videoCapture ("nn.mp4");
cout << "Face Detection ." << endl;
cout << "Realtime face detection using LBP " << endl;
cout << "Compiled with OpenCV version " << CV_VERSION << endl << endl;
// Load the face and 1 or 2 eye detection XML classifiers.
initDetectors(faceCascade,eyeCascade2);
Mat thresh, gray;
while (1)
{
Mat frame;
videoCapture>> frame;
detectLargestObject(frame, faceCascade, faceRect);
if (faceRect.width > 0)
{ // modification is begin here
Mat faceImg = frame(faceRect);
rectangle(frame, faceRect, CV_RGB(255, 0, 0), 2, CV_AA);
Mat gray;
if (faceImg.channels() == 3) {
cvtColor(faceImg, gray, CV_BGR2GRAY);
}
else if (faceImg.channels() == 4) {
cvtColor(faceImg, gray, CV_BGRA2GRAY);
}
else {
// Access the input image directly, since it is already grayscale.
gray = faceImg;
}
Point leftEye, rightEye;
Rect searchedLeftEye, searchedRightEye;
detectBothEyes(gray, eyeCascade1, eyeCascade2, leftEye, rightEye, & searchedLeftEye, & searchedRightEye);
rectangle(faceImg, searchedLeftEye,Scalar(0, 255, 0), 2, 8, 0);
rectangle(faceImg, searchedRightEye, Scalar(0, 255, 0), 2, 8, 0);
searchedRightEye.y += searchedRightEye.height / 3;
searchedRightEye.height -= searchedRightEye.height / 3;
Mat eye_region=faceImg(searchedRightEye);
cvtColor(eye_region, gray, CV_BGR2GRAY);
threshold(gray, thresh, 60, 255, THRESH_BINARY);
imshow("eye_ video", thresh);
} // modification end
imshow("video", frame);
// Press 'c' to escape
if (waitKey(30) == 'c') break;
}
return 0;
}