d62e529d4ae287ede5f2610783cd745e91a56d27
[platform/upstream/opencv.git] / modules / objdetect / include / opencv2 / objdetect.hpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                          License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43
44 #ifndef __OPENCV_OBJDETECT_HPP__
45 #define __OPENCV_OBJDETECT_HPP__
46
47 #include "opencv2/core.hpp"
48
49 typedef struct CvLatentSvmDetector CvLatentSvmDetector;
50 typedef struct CvHaarClassifierCascade CvHaarClassifierCascade;
51
52 namespace cv
53 {
54
55 ///////////////////////////// Object Detection ////////////////////////////
56
57 /*
58  * This is a class wrapping up the structure CvLatentSvmDetector and functions working with it.
59  * The class goals are:
60  * 1) provide c++ interface;
61  * 2) make it possible to load and detect more than one class (model) unlike CvLatentSvmDetector.
62  */
63 class CV_EXPORTS LatentSvmDetector
64 {
65 public:
66     struct CV_EXPORTS ObjectDetection
67     {
68         ObjectDetection();
69         ObjectDetection( const Rect& rect, float score, int classID = -1 );
70         Rect rect;
71         float score;
72         int classID;
73     };
74
75     LatentSvmDetector();
76     LatentSvmDetector( const std::vector<String>& filenames, const std::vector<String>& classNames = std::vector<String>() );
77     virtual ~LatentSvmDetector();
78
79     virtual void clear();
80     virtual bool empty() const;
81     bool load( const std::vector<String>& filenames, const std::vector<String>& classNames = std::vector<String>() );
82
83     virtual void detect( const Mat& image,
84                          std::vector<ObjectDetection>& objectDetections,
85                          float overlapThreshold = 0.5f,
86                          int numThreads = -1 );
87
88     const std::vector<String>& getClassNames() const;
89     size_t getClassCount() const;
90
91 private:
92     std::vector<CvLatentSvmDetector*> detectors;
93     std::vector<String> classNames;
94 };
95
96 // class for grouping object candidates, detected by Cascade Classifier, HOG etc.
97 // instance of the class is to be passed to cv::partition (see cxoperations.hpp)
98 class CV_EXPORTS SimilarRects
99 {
100 public:
101     SimilarRects(double _eps) : eps(_eps) {}
102     inline bool operator()(const Rect& r1, const Rect& r2) const
103     {
104         double delta = eps*(std::min(r1.width, r2.width) + std::min(r1.height, r2.height))*0.5;
105         return std::abs(r1.x - r2.x) <= delta &&
106             std::abs(r1.y - r2.y) <= delta &&
107             std::abs(r1.x + r1.width - r2.x - r2.width) <= delta &&
108             std::abs(r1.y + r1.height - r2.y - r2.height) <= delta;
109     }
110     double eps;
111 };
112
113 CV_EXPORTS   void groupRectangles(std::vector<Rect>& rectList, int groupThreshold, double eps = 0.2);
114 CV_EXPORTS_W void groupRectangles(CV_IN_OUT std::vector<Rect>& rectList, CV_OUT std::vector<int>& weights,
115                                   int groupThreshold, double eps = 0.2);
116 CV_EXPORTS   void groupRectangles(std::vector<Rect>& rectList, int groupThreshold,
117                                   double eps, std::vector<int>* weights, std::vector<double>* levelWeights );
118 CV_EXPORTS   void groupRectangles(std::vector<Rect>& rectList, std::vector<int>& rejectLevels,
119                                   std::vector<double>& levelWeights, int groupThreshold, double eps = 0.2);
120 CV_EXPORTS   void groupRectangles_meanshift(std::vector<Rect>& rectList, std::vector<double>& foundWeights,
121                                             std::vector<double>& foundScales,
122                                             double detectThreshold = 0.0, Size winDetSize = Size(64, 128));
123
124 template<> CV_EXPORTS void DefaultDeleter<CvHaarClassifierCascade>::operator ()(CvHaarClassifierCascade* obj) const;
125
126 enum { CASCADE_DO_CANNY_PRUNING    = 1,
127        CASCADE_SCALE_IMAGE         = 2,
128        CASCADE_FIND_BIGGEST_OBJECT = 4,
129        CASCADE_DO_ROUGH_SEARCH     = 8
130      };
131
132 class CV_EXPORTS_W BaseCascadeClassifier : public Algorithm
133 {
134 public:
135     virtual ~BaseCascadeClassifier();
136     virtual bool empty() const = 0;
137     virtual bool load( const String& filename ) = 0;
138     virtual void detectMultiScale( InputArray image,
139                            CV_OUT std::vector<Rect>& objects,
140                            double scaleFactor,
141                            int minNeighbors, int flags,
142                            Size minSize, Size maxSize ) = 0;
143
144     virtual void detectMultiScale( InputArray image,
145                            CV_OUT std::vector<Rect>& objects,
146                            CV_OUT std::vector<int>& numDetections,
147                            double scaleFactor,
148                            int minNeighbors, int flags,
149                            Size minSize, Size maxSize ) = 0;
150
151     virtual void detectMultiScale( InputArray image,
152                                    CV_OUT std::vector<Rect>& objects,
153                                    CV_OUT std::vector<int>& rejectLevels,
154                                    CV_OUT std::vector<double>& levelWeights,
155                                    double scaleFactor,
156                                    int minNeighbors, int flags,
157                                    Size minSize, Size maxSize,
158                                    bool outputRejectLevels ) = 0;
159
160     virtual bool isOldFormatCascade() const = 0;
161     virtual Size getOriginalWindowSize() const = 0;
162     virtual int getFeatureType() const = 0;
163     virtual void* getOldCascade() = 0;
164
165     class CV_EXPORTS MaskGenerator
166     {
167     public:
168         virtual ~MaskGenerator() {}
169         virtual Mat generateMask(const Mat& src)=0;
170         virtual void initializeMask(const Mat& /*src*/) { }
171     };
172     virtual void setMaskGenerator(const Ptr<MaskGenerator>& maskGenerator) = 0;
173     virtual Ptr<MaskGenerator> getMaskGenerator() = 0;
174 };
175
176 class CV_EXPORTS_W CascadeClassifier
177 {
178 public:
179     CV_WRAP CascadeClassifier();
180     CV_WRAP CascadeClassifier(const String& filename);
181     ~CascadeClassifier();
182     CV_WRAP bool empty() const;
183     CV_WRAP bool load( const String& filename );
184     CV_WRAP bool read( const FileNode& node );
185     CV_WRAP void detectMultiScale( InputArray image,
186                           CV_OUT std::vector<Rect>& objects,
187                           double scaleFactor = 1.1,
188                           int minNeighbors = 3, int flags = 0,
189                           Size minSize = Size(),
190                           Size maxSize = Size() );
191
192     CV_WRAP_AS(detectMultiScale2) void detectMultiScale( InputArray image,
193                           CV_OUT std::vector<Rect>& objects,
194                           CV_OUT std::vector<int>& numDetections,
195                           double scaleFactor=1.1,
196                           int minNeighbors=3, int flags=0,
197                           Size minSize=Size(),
198                           Size maxSize=Size() );
199
200     CV_WRAP_AS(detectMultiScale3) void detectMultiScale( InputArray image,
201                                   CV_OUT std::vector<Rect>& objects,
202                                   CV_OUT std::vector<int>& rejectLevels,
203                                   CV_OUT std::vector<double>& levelWeights,
204                                   double scaleFactor = 1.1,
205                                   int minNeighbors = 3, int flags = 0,
206                                   Size minSize = Size(),
207                                   Size maxSize = Size(),
208                                   bool outputRejectLevels = false );
209
210     CV_WRAP bool isOldFormatCascade() const;
211     CV_WRAP Size getOriginalWindowSize() const;
212     CV_WRAP int getFeatureType() const;
213     void* getOldCascade();
214
215     CV_WRAP static bool convert(const String& oldcascade, const String& newcascade);
216
217     void setMaskGenerator(const Ptr<BaseCascadeClassifier::MaskGenerator>& maskGenerator);
218     Ptr<BaseCascadeClassifier::MaskGenerator> getMaskGenerator();
219
220     Ptr<BaseCascadeClassifier> cc;
221 };
222
223 CV_EXPORTS Ptr<BaseCascadeClassifier::MaskGenerator> createFaceDetectionMaskGenerator();
224
225 //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////
226
227 // struct for detection region of interest (ROI)
228 struct DetectionROI
229 {
230    // scale(size) of the bounding box
231    double scale;
232    // set of requrested locations to be evaluated
233    std::vector<cv::Point> locations;
234    // vector that will contain confidence values for each location
235    std::vector<double> confidences;
236 };
237
238 struct CV_EXPORTS_W HOGDescriptor
239 {
240 public:
241     enum { L2Hys = 0
242          };
243     enum { DEFAULT_NLEVELS = 64
244          };
245
246     CV_WRAP HOGDescriptor() : winSize(64,128), blockSize(16,16), blockStride(8,8),
247         cellSize(8,8), nbins(9), derivAperture(1), winSigma(-1),
248         histogramNormType(HOGDescriptor::L2Hys), L2HysThreshold(0.2), gammaCorrection(true),
249         free_coef(-1.f), nlevels(HOGDescriptor::DEFAULT_NLEVELS)
250     {}
251
252     CV_WRAP HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride,
253                   Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1,
254                   int _histogramNormType=HOGDescriptor::L2Hys,
255                   double _L2HysThreshold=0.2, bool _gammaCorrection=false,
256                   int _nlevels=HOGDescriptor::DEFAULT_NLEVELS)
257     : winSize(_winSize), blockSize(_blockSize), blockStride(_blockStride), cellSize(_cellSize),
258     nbins(_nbins), derivAperture(_derivAperture), winSigma(_winSigma),
259     histogramNormType(_histogramNormType), L2HysThreshold(_L2HysThreshold),
260     gammaCorrection(_gammaCorrection), free_coef(-1.f), nlevels(_nlevels)
261     {}
262
263     CV_WRAP HOGDescriptor(const String& filename)
264     {
265         load(filename);
266     }
267
268     HOGDescriptor(const HOGDescriptor& d)
269     {
270         d.copyTo(*this);
271     }
272
273     virtual ~HOGDescriptor() {}
274
275     CV_WRAP size_t getDescriptorSize() const;
276     CV_WRAP bool checkDetectorSize() const;
277     CV_WRAP double getWinSigma() const;
278
279     CV_WRAP virtual void setSVMDetector(InputArray _svmdetector);
280
281     virtual bool read(FileNode& fn);
282     virtual void write(FileStorage& fs, const String& objname) const;
283
284     CV_WRAP virtual bool load(const String& filename, const String& objname = String());
285     CV_WRAP virtual void save(const String& filename, const String& objname = String()) const;
286     virtual void copyTo(HOGDescriptor& c) const;
287
288     CV_WRAP virtual void compute(InputArray img,
289                          CV_OUT std::vector<float>& descriptors,
290                          Size winStride = Size(), Size padding = Size(),
291                          const std::vector<Point>& locations = std::vector<Point>()) const;
292
293     //with found weights output
294     CV_WRAP virtual void detect(const Mat& img, CV_OUT std::vector<Point>& foundLocations,
295                         CV_OUT std::vector<double>& weights,
296                         double hitThreshold = 0, Size winStride = Size(),
297                         Size padding = Size(),
298                         const std::vector<Point>& searchLocations = std::vector<Point>()) const;
299     //without found weights output
300     virtual void detect(const Mat& img, CV_OUT std::vector<Point>& foundLocations,
301                         double hitThreshold = 0, Size winStride = Size(),
302                         Size padding = Size(),
303                         const std::vector<Point>& searchLocations=std::vector<Point>()) const;
304
305     //with result weights output
306     CV_WRAP virtual void detectMultiScale(InputArray img, CV_OUT std::vector<Rect>& foundLocations,
307                                   CV_OUT std::vector<double>& foundWeights, double hitThreshold = 0,
308                                   Size winStride = Size(), Size padding = Size(), double scale = 1.05,
309                                   double finalThreshold = 2.0,bool useMeanshiftGrouping = false) const;
310     //without found weights output
311     virtual void detectMultiScale(InputArray img, CV_OUT std::vector<Rect>& foundLocations,
312                                   double hitThreshold = 0, Size winStride = Size(),
313                                   Size padding = Size(), double scale = 1.05,
314                                   double finalThreshold = 2.0, bool useMeanshiftGrouping = false) const;
315
316     CV_WRAP virtual void computeGradient(const Mat& img, CV_OUT Mat& grad, CV_OUT Mat& angleOfs,
317                                  Size paddingTL = Size(), Size paddingBR = Size()) const;
318
319     CV_WRAP static std::vector<float> getDefaultPeopleDetector();
320     CV_WRAP static std::vector<float> getDaimlerPeopleDetector();
321
322     CV_PROP Size winSize;
323     CV_PROP Size blockSize;
324     CV_PROP Size blockStride;
325     CV_PROP Size cellSize;
326     CV_PROP int nbins;
327     CV_PROP int derivAperture;
328     CV_PROP double winSigma;
329     CV_PROP int histogramNormType;
330     CV_PROP double L2HysThreshold;
331     CV_PROP bool gammaCorrection;
332     CV_PROP std::vector<float> svmDetector;
333     UMat oclSvmDetector;
334     float free_coef;
335     CV_PROP int nlevels;
336
337
338     // evaluate specified ROI and return confidence value for each location
339     virtual void detectROI(const cv::Mat& img, const std::vector<cv::Point> &locations,
340                                    CV_OUT std::vector<cv::Point>& foundLocations, CV_OUT std::vector<double>& confidences,
341                                    double hitThreshold = 0, cv::Size winStride = Size(),
342                                    cv::Size padding = Size()) const;
343
344     // evaluate specified ROI and return confidence value for each location in multiple scales
345     virtual void detectMultiScaleROI(const cv::Mat& img,
346                                                        CV_OUT std::vector<cv::Rect>& foundLocations,
347                                                        std::vector<DetectionROI>& locations,
348                                                        double hitThreshold = 0,
349                                                        int groupThreshold = 0) const;
350
351     // read/parse Dalal's alt model file
352     void readALTModel(String modelfile);
353     void groupRectangles(std::vector<cv::Rect>& rectList, std::vector<double>& weights, int groupThreshold, double eps) const;
354 };
355
356
357 CV_EXPORTS_W void findDataMatrix(InputArray image,
358                                  CV_OUT std::vector<String>& codes,
359                                  OutputArray corners = noArray(),
360                                  OutputArrayOfArrays dmtx = noArray());
361
362 CV_EXPORTS_W void drawDataMatrixCodes(InputOutputArray image,
363                                       const std::vector<String>& codes,
364                                       InputArray corners);
365 }
366
367 #include "opencv2/objdetect/linemod.hpp"
368 #include "opencv2/objdetect/erfilter.hpp"
369
370 #endif