Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

CascadeClassifier.detectMultiScale takes a minute! Why?

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;