Ask Your Question

CascadeClassifier.detectMultiScale takes a minute! Why?

asked 2012-11-29 10:25:52 -0600

Dženan gravatar image

I am trying to create a vertebra detector. After manually creating a small set of training and background images, trained a classifier, and am now trying to use it. Results are nonsense, but that I expected. The problem I am having is very long running time ~ 1 minute for one image, instead of some ~ 100ms, for release build.

What am I doing wrong?


detection time = 65799.5 ms


#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main(int argc, char* argv[])
    CascadeClassifier cascade;

    cv::Mat img = cv::imread("D:/img.png",0);

    int i = 0;
    double t = 0;
    double xres=0.625, yres=0.625;
    vector<Rect> faces;

    equalizeHist(img, img);
    t = (double)cvGetTickCount();
    cascade.detectMultiScale( img, faces, 1.1, 2, 0,
        Size(10/xres, 10/yres), Size(55/xres, 55/yres) ); //min 10mm, max 55mm
    t = (double)cvGetTickCount() - t;
    printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
    for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
        Point center;
        Scalar color = CV_RGB(255,255,255);
        int radius;
        center.x = cvRound((r->x + r->width*0.5));
        center.y = cvRound((r->y + r->height*0.5));
        radius = cvRound((r->width + r->height)*0.25);
        circle( img, center, radius, color, 3, 8, 0 );
    cv::imshow( "result", img );

    return 0;
edit retag flag offensive close merge delete


It is possible that a too high res could slow the detector. Your image is in which resolution? Also try to increase the number of neighbors. Anyway, the cascade training maybe a bit unpredictable, it does not guarantees a good detector in all the situations.

icedecker gravatar imageicedecker ( 2012-11-29 12:56:20 -0600 )edit

Image resolution is 512x512. Increasing the number of neighbors to 4 changes nearly nothing. Number of detections is about the same, and execution time is the same.

Dženan gravatar imageDženan ( 2012-11-30 04:49:58 -0600 )edit

1 answer

Sort by » oldest newest most voted

answered 2012-11-29 15:38:24 -0600

venky gravatar image

Hi, Looks like you have too many detections, and you have not mentioned what is the cascade architecture and how many features you are using at each stage. Probably you need to change the threshold of the classifier. You wont realise, since grouping of rectangles will finally give you only some detection windows Thanks Venky

edit flag offensive delete link more


I realized that grouping detected rectangles takes a long time, longer than the detection itself. With better detector it will be less of a problem.

Dženan gravatar imageDženan ( 2012-11-30 08:21:36 -0600 )edit

With better classifier, detection time is 639 ms. So number of detections was the problem.

Dženan gravatar imageDženan ( 2012-12-04 08:01:12 -0600 )edit

Nice to hear that, so you have solved the problem now.

venky gravatar imagevenky ( 2012-12-05 06:14:07 -0600 )edit

The long running time problem, yes. I have left to solve the good classifier problem, mostly meaning to assemble good positive/negative data.

Dženan gravatar imageDženan ( 2012-12-05 06:23:08 -0600 )edit

Hey! I am having a similar problem. I trained a classifier for hand detection, but detectMultiScale(..) is way to slow!! It's taking too long to detect the hand in every frame. How many positives and negatives did you use to get a better detector?

prerna1 gravatar imageprerna1 ( 2013-06-18 08:08:23 -0600 )edit

I have made several different detector cascades, the most recent one with these parameters: -w 16 -h 16 -featureType HAAR -mode ALL -numPos 7500 -numNeg 7500 -numStages 30 -minHitRate 0.998

Such high number of positive samples come from having 11 rotated versions of each real sample. Real positive sample count was 864. Assembling the training data is the biggest part of the job of creating a detection cascade.

Dženan gravatar imageDženan ( 2013-06-18 08:19:27 -0600 )edit

Question Tools


Asked: 2012-11-29 10:25:52 -0600

Seen: 3,548 times

Last updated: Nov 29 '12