Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OpenCV CascadeClassifier Segmentation Fault

This is same question I asked in StackOverflow

I am using OpenCV to detect faces in video. Currently I am using CascadeClassifier class, Haar classifier. When I run cascade.detectmultiscale, my program will run normally and suddenly crash with segmentation fault. I debug using gdb and found out thatProgram received signal SIGSEGV, Segmentation fault.

[Switching to Thread 0xb1841460 (LWP 995)]
0x00099280 in cvRunHaarClassifierCascadeSum(CvHaarClassifierCascade const*, CvPoint, double&, int) () there is problem in cvRunHaarClassifierCascadeSum

I tried using the C version of cascade classifier ( the CvHaarClassifierCascade class ), same problem also happens when I run it and debug it. This is the code in case you want to see my implementation:

Detector class

class faceDetector {
    ...
    cv::CascadeClassifier cascade;
    ...
};

Initialization

int initDetector(faceDetector* detect) {
    detect->cascade.load( CASCADE_XML_PATH );
    detect->flags = CV_HAAR_DO_CANNY_PRUNING;
    detect->maxR = 23;
    detect->minNeighbours = 3;
    detect->minSize = cvSize(10, 10);
    detect->maxSize = cvSize(100, 100);
    detect->scaleFactor = 1.1;
    cout << "initDetection finished " << endl;
    return 1;
}

Detection, called in a while loop

int faceDetector::detectFaces(IplImage* img, CvRect** regions) {
    int nFaces = 0; // number of detection considered as face
    IplImage* gray = cvCreateImage( cvSize(img->width, img->height), 8, 1 );
    IplImage* smallImg = cvCreateImage( cvSize( cvRound (img->width/scaleFactor),
                                            cvRound (img->height/scaleFactor)), 8, 1 );
    cvCvtColor(img, gray, CV_BGR2GRAY);
    cvResize(gray, smallImg, CV_INTER_LINEAR);
    cvEqualizeHist(smallImg, smallImg);
    cvClearMemStorage(storage);
    int nx1, nx2, ny1, ny2; // coordinate of detection rectangles

    std::vector<cv::Rect> faces; // list of detection
    cv::Mat msmallimg(smallImg);
    cascade.detectMultiScale( msmallimg, faces, scaleFactor, minNeighbours, flags, minSize, maxSize );

    CvRect* nRects;
    nRects = (CvRect*) malloc(faces.size() * sizeof(CvRect));

    std::vector<cv::Rect>::const_iterator i;

    for (i=faces.begin(); i!=faces.end() ; ++i) {
        if ((i->width <= maxSize.width) && (i->height <= maxSize.height) && (i->width >= minSize.width) && (i->height >= minSize.height) ) {
            nx1 = cvRound( i->x * scaleFactor);
            ny1 = cvRound( i->y * scaleFactor);
            nx2 = cvRound( (i->x + i->width) * scaleFactor);
            ny2 = cvRound( (i->y + i->height) * scaleFactor);           
            nRects[nFaces] = cvRect(nx1, ny1, nx2-nx1, ny2-ny1);
            nFaces++;
        }
    }

    *regions = nRects;
    cvReleaseImage(&gray);
    cvReleaseImage(&smallImg);
    return nFaces;
}

Is this known bugs in OpenCV cascade classifier or I have a bad code? Thanks for your help!

P.S I use OpenCV 2.4.5 compiled for ARM board (Odroid X2, processor Samsung Exynos) using arm gcc linaro compiler (gcc-linaro-arm-linux-gnueabihf-4.7-2013.01-20130125_linux). Same problem also happens in my host PC (linux ubuntu 12.04, openCV 2.4.5, intel i5 processor using gcc 4.6.3)

OpenCV CascadeClassifier Segmentation Fault

This is same question I asked in StackOverflow

I am using OpenCV to detect faces in video. Currently I am using CascadeClassifier class, Haar classifier. When I run cascade.detectmultiscale, my program will run normally and suddenly crash with segmentation fault. I debug using gdb and found out thatProgram received signal SIGSEGV, Segmentation fault.

[Switching to Thread 0xb1841460 (LWP 995)]
0x00099280 in cvRunHaarClassifierCascadeSum(CvHaarClassifierCascade const*, CvPoint, double&, int) () there is problem in cvRunHaarClassifierCascadeSum

Running backtrace, seems like gdb cannot finish the process

#0  0x00099470 in cvRunHaarClassifierCascadeSum(CvHaarClassifierCascade const*, CvPoint, double&, int) ()
#1  0x0009a8be in cvRunHaarClassifierCascade ()
#2  0x0009b28e in cv::HaarDetectObjects_ScaleCascade_Invoker::operator()(cv::Range const&) const ()
#3  0x00175928 in tbb::interface6::internal::start_for<tbb::blocked_range<int>, (anonymous namespace)::ProxyLoopBody, tbb::auto_partitioner con)
()
#4  0x00502f5e in tbb::internal::custom_scheduler<tbb::internal::IntelSchedulerTraits>::local_wait_for_all(tbb::task&, tbb::task*) ()
#5  0x005037d0 in tbb::internal::arena::process(tbb::internal::generic_scheduler&) ()
#6  0x00506c60 in tbb::internal::market::process(rml::job&) ()
#7  0x005078fe in tbb::internal::rml::private_worker::run() ()
#8  0x00507c3a in tbb::internal::rml::private_worker::thread_routine(void*) ()
#9  0xb6fb1cb0 in start_thread () from /usr/lib/libpthread.so.0
#10 0xb6dd6828 in ?? () from /usr/lib/libc.so.6
#11 0xb6dd6828 in ?? () from /usr/lib/libc.so.6
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

I tried using the C version of cascade classifier ( the CvHaarClassifierCascade class ), same problem also happens when I run it and debug it. This is the code in case you want to see my implementation:

Detector class

class faceDetector {
    ...
    cv::CascadeClassifier cascade;
    ...
};

Initialization

int initDetector(faceDetector* detect) {
    detect->cascade.load( CASCADE_XML_PATH );
    detect->flags = CV_HAAR_DO_CANNY_PRUNING;
    detect->maxR = 23;
    detect->minNeighbours = 3;
    detect->minSize = cvSize(10, 10);
    detect->maxSize = cvSize(100, 100);
    detect->scaleFactor = 1.1;
    cout << "initDetection finished " << endl;
    return 1;
}

Detection, called in a while loop

int faceDetector::detectFaces(IplImage* img, CvRect** regions) {
    int nFaces = 0; // number of detection considered as face
    IplImage* gray = cvCreateImage( cvSize(img->width, img->height), 8, 1 );
    IplImage* smallImg = cvCreateImage( cvSize( cvRound (img->width/scaleFactor),
                                            cvRound (img->height/scaleFactor)), 8, 1 );
    cvCvtColor(img, gray, CV_BGR2GRAY);
    cvResize(gray, smallImg, CV_INTER_LINEAR);
    cvEqualizeHist(smallImg, smallImg);
    cvClearMemStorage(storage);
    int nx1, nx2, ny1, ny2; // coordinate of detection rectangles

    std::vector<cv::Rect> faces; // list of detection
    cv::Mat msmallimg(smallImg);
    cascade.detectMultiScale( msmallimg, faces, scaleFactor, minNeighbours, flags, minSize, maxSize );

    CvRect* nRects;
    nRects = (CvRect*) malloc(faces.size() * sizeof(CvRect));

    std::vector<cv::Rect>::const_iterator i;

    for (i=faces.begin(); i!=faces.end() ; ++i) {
        if ((i->width <= maxSize.width) && (i->height <= maxSize.height) && (i->width >= minSize.width) && (i->height >= minSize.height) ) {
            nx1 = cvRound( i->x * scaleFactor);
            ny1 = cvRound( i->y * scaleFactor);
            nx2 = cvRound( (i->x + i->width) * scaleFactor);
            ny2 = cvRound( (i->y + i->height) * scaleFactor);           
            nRects[nFaces] = cvRect(nx1, ny1, nx2-nx1, ny2-ny1);
            nFaces++;
        }
    }

    *regions = nRects;
    cvReleaseImage(&gray);
    cvReleaseImage(&smallImg);
    return nFaces;
}

The error always happened after I call

cascade.detectMultiScale( msmallimg, faces, scaleFactor, minNeighbours, flags, minSize, maxSize );

Is this known bugs in OpenCV cascade classifier or I have a bad code? I am confused how to debug further on this problem because it is inside my own code. Cannot use Valgrind too, which I usually use for segmentation fault problem. Valgrind cannot understand some ARM instruction set inside my binary.

Thanks for your help!

P.S I use OpenCV 2.4.5 compiled for ARM board (Odroid X2, processor Samsung Exynos) using arm gcc linaro compiler (gcc-linaro-arm-linux-gnueabihf-4.7-2013.01-20130125_linux). Same problem also happens in my host PC (linux ubuntu 12.04, openCV 2.4.5, intel i5 processor using gcc 4.6.3)