# Adds a variant of detectMultiScale that outputs weights of detected objects, i.e. a measure of
# confidence for each one.
# To apply the patch, cd to OpenCV source root (containing the directory "modules") and execute the
# following:
# 	patch -p 1 < <path to patch>/cascade_classifier.patch
diff -ru /modules/objdetect/include/opencv2/objdetect/objdetect.hpp modules/objdetect/include/opencv2/objdetect/objdetect.hpp
--- /modules/objdetect/include/opencv2/objdetect/objdetect.hpp	2012-10-20 02:55:11.000000000 +0400
+++ modules/objdetect/include/opencv2/objdetect/objdetect.hpp	2013-06-03 21:09:47.425708483 +0400
@@ -382,6 +382,16 @@
                                    Size minSize=Size(),
                                    Size maxSize=Size() );
 
+    CV_WRAP virtual void detectMultiScale( const Mat& image,
+                                   CV_OUT vector<Rect>& objects,
+                                   vector<int>& weights,
+                                   double scaleFactor=1.1,
+                                   int minNeighbors=3, int flags=0,
+                                   Size minSize=Size(),
+                                   Size maxSize=Size() );
+
     CV_WRAP virtual void detectMultiScale( const Mat& image,
                                    CV_OUT vector<Rect>& objects,
                                    vector<int>& rejectLevels,
@@ -390,7 +400,8 @@
                                    int minNeighbors=3, int flags=0,
                                    Size minSize=Size(),
                                    Size maxSize=Size(),
-                                   bool outputRejectLevels=false );
+                                   bool outputRejectLevels=false,
+                                   bool outputWeights=false );
 
 
     bool isOldFormatCascade() const;
diff -ru /modules/objdetect/src/cascadedetect.cpp modules/objdetect/src/cascadedetect.cpp
--- /modules/objdetect/src/cascadedetect.cpp	2012-09-26 02:55:08.000000000 +0400
+++ modules/objdetect/src/cascadedetect.cpp	2013-06-03 21:27:16.289661720 +0400
@@ -1022,6 +1022,7 @@
 };
 
 struct getRect { Rect operator ()(const CvAvgComp& e) const { return e.rect; } };
+struct getNeighbors { int operator ()(const CvAvgComp& e) const { return e.neighbors; } };
 
 
 bool CascadeClassifier::detectSingleScale( const Mat& image, int stripCount, Size processingRectSize,
@@ -1091,11 +1092,12 @@
                                           vector<double>& levelWeights,
                                           double scaleFactor, int minNeighbors,
                                           int flags, Size minObjectSize, Size maxObjectSize,
-                                          bool outputRejectLevels )
+                                          bool outputRejectLevels, bool outputWeights )
 {
     const double GROUP_EPS = 0.2;
 
     CV_Assert( scaleFactor > 1 && image.depth() == CV_8U );
+    CV_Assert( !(outputRejectLevels && outputWeights) );
 
     if( empty() )
         return;
@@ -1110,6 +1112,11 @@
         Seq<CvAvgComp>(_objects).copyTo(vecAvgComp);
         objects.resize(vecAvgComp.size());
         std::transform(vecAvgComp.begin(), vecAvgComp.end(), objects.begin(), getRect());
+        if (outputWeights) {
+            rejectLevels.resize(vecAvgComp.size());
+            std::transform(vecAvgComp.begin(), vecAvgComp.end(), rejectLevels.begin(),
+                    getNeighbors());
+        }
         return;
     }
 
@@ -1187,6 +1194,10 @@
     {
         groupRectangles( objects, rejectLevels, levelWeights, minNeighbors, GROUP_EPS );
     }
+    else if( outputWeights )
+    {
+        groupRectangles( objects, rejectLevels, minNeighbors, GROUP_EPS );
+    }
     else
     {
         groupRectangles( objects, minNeighbors, GROUP_EPS );
@@ -1203,6 +1214,16 @@
         minNeighbors, flags, minObjectSize, maxObjectSize, false );
 }
 
+void CascadeClassifier::detectMultiScale( const Mat& image, CV_OUT vector<Rect>& objects,
+                                          vector<int>& weights, double scaleFactor,
+                                          int minNeighbors, int flags, Size minObjectSize,
+                                          Size maxObjectSize )
+{
+    vector<double> fakeLevelWeights;
+    detectMultiScale( image, objects, weights, fakeLevelWeights, scaleFactor,
+            minNeighbors, flags, minObjectSize, maxObjectSize, false, true );
+}
+
 bool CascadeClassifier::Data::read(const FileNode &root)
 {
     static const float THRESHOLD_EPS = 1e-5f;
