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)