How to get detection score from OpenCV cascade classifier

The OpenCV cascade classifier makes it easy to train and test detection of faces and other objects in images. I have already written about how to train this classifier here. To get the objects detected during the testing stage, OpenCV provides this function:

void CascadeClassifier::detectMultiScale(
    const Mat& image,
    vector<Rect>& objects,
    double scaleFactor=1.1,
    int minNeighbors=3,
    int flags=0,
    Size minSize=Size(),
    Size maxSize=Size()
);

This returns a list of rectangles in the image where the classifier thinks it has detected the object. However, there seems to be no way to rank the detected locations. There is no detection or confidence weight or score associated with each rectangle to help you prioritize the detected locations. This is surprising since such a score should be present in the classifier, it is just that this function is not exposing that information.

Thankfully, there is an alternative to this function that returns the weight and reject level associated with each detected rectangle. Surprisingly, this function does not yet appear in the documentation. You can find it declared in modules/objdetect/include/opencv2/objdetect/objdetect.hpp as:

CV_WRAP virtual void detectMultiScale(
    const Mat& image,
    CV_OUT vector<Rect>& objects,
    vector<int>& rejectLevels,
    vector<double>& levelWeights,
    double scaleFactor=1.1,
    int minNeighbors=3,
    int flags=0,
    Size minSize=Size(),
    Size maxSize=Size(),
    bool outputRejectLevels=false
);

You can also hack the code a bit to get other types of scores with each detected location as demonstrated in this post.

Tried with: OpenCV 2.4.9 and Ubuntu 14.04

One thought on “How to get detection score from OpenCV cascade classifier”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s