Search code examples
c++opencvface-detection

std::algorithm Read access violation


I'm setting up the face detection with delay camera in opencv/c++. How can I do it without errors?

For detection I'm used CascadeClassifier.Detectmultiscale.

void detectAndDraw(Mat& img, CascadeClassifier& cascade,

    double scale)
{
    vector<Rect> faces;
    Mat gray;

    cvtColor(img, gray, COLOR_BGR2GRAY); // Convert to Gray Scale 


    // Resize the Grayscale Image  

    equalizeHist(gray, gray);

    // Detect faces of different sizes using cascade classifier  
    cascade.detectMultiScale(gray, faces);

    // Draw circles around the faces 
for(int i = 0; i<=faces.size();i++){
//and cout of x,y,width,height
}

I have expexted the details but I have a error with Access Reading Memory in algorithm.

Photo:

This is error


Solution

  • Looks like you have an off-by-one error in this loop:

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

    That should probably be a < rather than <=, since otherwise on the last iteration your value of i will be out of bounds.