OpenCV face detection results in "debug assertion failed"

asked 2019-08-09 14:55:25 -0600

I am trying to use OpenCV 4.1.1 on Visual Studio 2013 to perform facial detection. The program works fine for 2 frames and then I get this error:

Debug Assertion Failed!
Program:
filepath\program.exe
File: f:\dd\vctools\crt\crtw32\misc\dbgheap.c
Line:1424

This is my code:

#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
void detectAndDisplay(Mat frame);
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;

int main(int argc, const char** argv)
{
face_cascade.load(filepath);
eyes_cascade.load(filepath);
VideoCapture cap(0); 
if (!cap.isOpened())
    return -1;
Mat frame;
while (cap.read(frame))
{
    if (frame.empty())
    {
        cout << "--(!) No captured frame -- Break!\n";
        break;
    }
        detectAndDisplay(frame);
    if (waitKey(10) == 27)
    {
        break; // escape
    }
}
return 0;
}
void detectAndDisplay(Mat frame)
{
Mat frame_gray;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
vector<Rect> faces;
face_cascade.detectMultiScale(frame_gray, faces);
for (size_t i = 0; i < faces.size(); i++)
{
    Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
    ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4);

}
imshow("Capture - Face detection", frame);

}

The program works as expected for 2 frames only. It is past two frames that aforementioned error occurs. I have confirmed this by adding a counter to the following code:

int i=0;
while (cap.read(frame))
{
    if (i < 2){
        detectAndDisplay(frame);
}
i++;

I think it might have something to do with how memory is allocated within the detectAndDisplay(Mat frame) function, particularly with the frame and vector, however my understanding is that OpenCV should automatically free the previous frame to make space for the new one. I also know the cascade classifiers load correctly.

edit retag flag offensive close merge delete

Comments

Can you check if adding an explicit clone after the frame grabbing, so within the loop, solves the issue? Like a hard local copy of your mat?

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-20 08:58:03 -0600 )edit