I get cv::Exception at memory location 0x003faf00 when I run the following code, noting that it ran once or twice perfectly

StackOverflow https://stackoverflow.com/questions/20751605

Вопрос

#include <opencv2\objdetect\objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

int main(int argc, const char** argv)
{
//create the cascade classifier object used for the face detection
CascadeClassifier face_cascade;
CascadeClassifier face_cascade2;
CascadeClassifier face_cascade3;

//use the xml files for features detection
face_cascade.load("C:\\opencv247\\sources\\data\\haarcascades\\eyes.xml"); //haarcascade_eye_tree_eyeglasses.xml Eyes only capture straight foroward eyes
face_cascade2.load("C:\\opencv247\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml");
face_cascade3.load("C:\\opencv247\\sources\\data\\haarcascades\\newnose.xml");


VideoCapture cap(0); // open the default camera
if(!cap.isOpened())  // check if we succeeded
    return -1;


//setup image files used in the capture process

Mat gray_image;
//create a window to present the results
namedWindow("outputCapture", 1);


//create a loop to capture and find faces
while(1)
{   
    //Capture
    Mat image;
    waitKey(40);
    cap >> image; // get a new frame from camera
    //Convert image to grayscale
    cvtColor(image, gray_image, CV_BGR2GRAY);


    //create a vector array to store the features found
    std::vector<Rect> eyes;
    std::vector<Rect> faces;
    std::vector<Rect> nose;




    //find features and store them in the vector array
    face_cascade.detectMultiScale(gray_image, eyes, 1.2, 3, CV_HAAR_FIND_BIGGEST_OBJECT|CV_HAAR_SCALE_IMAGE, Size(30,30));
    face_cascade2.detectMultiScale(gray_image, faces, 1.2, 3, CV_HAAR_FIND_BIGGEST_OBJECT|CV_HAAR_SCALE_IMAGE, Size(30,30));
    face_cascade3.detectMultiScale(gray_image, nose, 1.2, 6, CV_HAAR_FIND_BIGGEST_OBJECT|CV_HAAR_SCALE_IMAGE, Size(30,30));

    //draw a rectangle for all found the Eyes in the vector array on the original image


    //Faces

    for(int i = 0; i < faces.size(); i++)
    {


        Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
        Point pt2(faces[i].x, faces[i].y);
        rectangle(image, pt1, pt2, cvScalar(0, 255, 0, 0), 1, 8, 0);

        //eyes

        for(int j = 0; j < eyes.size(); j++)
        {


            Point pt1(eyes[i].x + eyes[j].width, eyes[i].y + eyes[j].height);
            Point pt2(eyes[i].x, eyes[i].y);
            rectangle(image, pt1, pt2, cvScalar(0, 100, 0, 0), 1, 8, 0);


        }
        //Nose

        for(int z = 0; z < nose.size(); z++)
        {


            Point pt1(nose[i].x + nose[z].width, nose[i].y + nose[z].height);
            Point pt2(nose[i].x, nose[i].y);
            rectangle(image, pt1, pt2, cvScalar(100, 200, 0, 0), 1, 8, 0);


        }


    }

    //print the output
    imshow("outputCapture", image);
    //pause for 33ms
      waitKey(33);

}
if(cap.isOpened())
{
    image.release();
    gray_image.release();
    cap.release();
}

return 0;
}

The problem is now It sometimes run but sometime it gives me that memory exception error , in my humbled programing opinion the pointers used or something must be destroyed or the image is getting declared more than one time , I am just stuck and its infuriating because it sometimes ACTUALLY work , but then it crashes... I do understand the code is a bit untidy and i apologize for that.

EDIT: Now it works only when I remove the camera and plug it back in the USB , but on the second run it gives the same error.

EDIT2: After walking behind some debugs, I discovered that when it crashes its right after the line when I call the cvtcolor function , so the problem might be with the allocations of the images not being deconstructed properly. EDIT3 : Added the part at the end of the code where I try to release the cap and the images but the problem is that I realized it never reaches that line of code

Это было полезно?

Решение

One problem I see is that you are using the index of the faces to index the eyes, nose etc.

e.g.,

Point pt1(eyes[i].x + eyes[j].width, eyes[i].y + eyes[j].height);

This is wrong. You should draw the eyes and nose separately outside of the first "for" loop for faces. i.e.,

Point pt1(eyes[j].x + eyes[j].width, eyes[j].y + eyes[j].height);

This could be the reason for your memory error as the number of detected eyes and nose could be fewer than the number of detected faces, hence leading to your code trying to acces a memory location outside the bounds of your arrays for eyes and nose.

Do note that the detectors are not going to be able to detect with 100% accuracy. Even if it was able to detect a face/eye/nose in the previous frame, there is no guarantee that it can detect the same object in subsequent frames. This is because of noise in the frames and illumination and pose changes. The detector will miss some of the objects and wrongly detect some objects.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top