Ask Your Question
0

unhandled memory exception problem

asked 2015-12-29 16:49:05 -0600

tofi gravatar image

hi

I'm using this code for face and eye detection :

when I run it it throws memory exception and in the cmd this message dispalyed :

image description

why this happening can you help me please

Thanks

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)
     {
         rectangle(frame, faceRect, CV_RGB(255, 0, 0), 2, CV_AA);
     }

     Mat faceImg = frame(faceRect);

     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);

     imshow("video", frame);
     // Press 'c' to escape
     if (waitKey(30) == 'c') break;
}

return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-12-30 12:13:56 -0600

updated 2015-12-31 16:14:06 -0600

i have made some corrections to your code and tested.

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;
}
edit flag offensive delete link more

Comments

@sturkment , thanks very much for you efforts it works now I have two questions :

  1. what the following coordinates mean :

    enter code here

       const float EYE_SX = 0.10f;
      const float EYE_SY = 0.19f;
      const float EYE_SW = 0.40f;
      const float EYE_SH = 0.36f;
    
  2. If I want to width of the of searchedRightEye like this : image

because it sometimes show hair (if women video ) or background when head moves , how this can be done ?

elp14sma gravatar imageelp14sma ( 2015-12-30 12:43:45 -0600 )edit

the best way to thank for an answer is by marking it as the correct answer :). Only you as owner of the question can do that.

sturkmen gravatar imagesturkmen ( 2015-12-31 11:14:38 -0600 )edit

what about the questions should I open another question ? or you can answer me Thanks

tofi gravatar imagetofi ( 2015-12-31 14:17:58 -0600 )edit

see updated answer.

searchedLeftEye =shrinkRect( searchedLeftEye, 50, 50 );// reduces width and height %50

you can try different values like

searchedLeftEye = shrinkRect( searchedLeftEye, 100, 33 ); // reduces only height %33
sturkmen gravatar imagesturkmen ( 2015-12-31 16:18:44 -0600 )edit

I have tried the code on ubuntu , using video file and webcam , the detected eye seems to be zoomed , why this is happening ? output image

tofi gravatar imagetofi ( 2016-01-02 15:39:10 -0600 )edit

is it output of this code ? i think no. please post your final code.

sturkmen gravatar imagesturkmen ( 2016-01-02 16:02:28 -0600 )edit

yes , this is the code

tofi gravatar imagetofi ( 2016-01-02 18:21:24 -0600 )edit

you did not understand usage of shrinkRect

searchedRightEye = shrinkRect(searchedRightEye, 50, val); // using variable is not good here
sturkmen gravatar imagesturkmen ( 2016-01-02 18:57:56 -0600 )edit

@sturkmen , thanks very much for help , my best wishes to you

tofi gravatar imagetofi ( 2016-01-03 08:29:16 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-29 16:49:05 -0600

Seen: 540 times

Last updated: Dec 31 '15