objdetect.hpp
Go to the documentation of this file.
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 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 
43 #ifndef __OPENCV_OBJDETECT_HPP__
44 #define __OPENCV_OBJDETECT_HPP__
45 
46 #include "opencv2/core/core.hpp"
47 
48 #ifdef __cplusplus
49 #include <map>
50 #include <deque>
51 
52 extern "C" {
53 #endif
54 
55 /****************************************************************************************\
56 * Haar-like Object Detection functions *
57 \****************************************************************************************/
58 
59 #define CV_HAAR_MAGIC_VAL 0x42500000
60 #define CV_TYPE_NAME_HAAR "opencv-haar-classifier"
61 
62 #define CV_IS_HAAR_CLASSIFIER( haar ) \
63  ((haar) != NULL && \
64  (((const CvHaarClassifierCascade*)(haar))->flags & CV_MAGIC_MASK)==CV_HAAR_MAGIC_VAL)
65 
66 #define CV_HAAR_FEATURE_MAX 3
67 
68 typedef struct CvHaarFeature
69 {
70  int tilted;
71  struct
72  {
74  float weight;
75  } rect[CV_HAAR_FEATURE_MAX];
77 
78 typedef struct CvHaarClassifier
79 {
80  int count;
82  float* threshold;
83  int* left;
84  int* right;
85  float* alpha;
87 
88 typedef struct CvHaarStageClassifier
89 {
90  int count;
91  float threshold;
93 
94  int next;
95  int child;
96  int parent;
98 
100 
102 {
103  int flags;
104  int count;
107  double scale;
111 
112 typedef struct CvAvgComp
113 {
116 } CvAvgComp;
117 
118 /* Loads haar classifier cascade from a directory.
119  It is obsolete: convert your cascade to xml and use cvLoad instead */
120 CVAPI(CvHaarClassifierCascade*) cvLoadHaarClassifierCascade(
121  const char* directory, CvSize orig_window_size);
122 
123 CVAPI(void) cvReleaseHaarClassifierCascade( CvHaarClassifierCascade** cascade );
124 
125 #define CV_HAAR_DO_CANNY_PRUNING 1
126 #define CV_HAAR_SCALE_IMAGE 2
127 #define CV_HAAR_FIND_BIGGEST_OBJECT 4
128 #define CV_HAAR_DO_ROUGH_SEARCH 8
129 
130 //CVAPI(CvSeq*) cvHaarDetectObjectsForROC( const CvArr* image,
131 // CvHaarClassifierCascade* cascade, CvMemStorage* storage,
132 // CvSeq** rejectLevels, CvSeq** levelWeightds,
133 // double scale_factor CV_DEFAULT(1.1),
134 // int min_neighbors CV_DEFAULT(3), int flags CV_DEFAULT(0),
135 // CvSize min_size CV_DEFAULT(cvSize(0,0)), CvSize max_size CV_DEFAULT(cvSize(0,0)),
136 // bool outputRejectLevels = false );
137 
138 
139 CVAPI(CvSeq*) cvHaarDetectObjects( const CvArr* image,
141  double scale_factor CV_DEFAULT(1.1),
142  int min_neighbors CV_DEFAULT(3), int flags CV_DEFAULT(0),
143  CvSize min_size CV_DEFAULT(cvSize(0,0)), CvSize max_size CV_DEFAULT(cvSize(0,0)));
144 
145 /* sets images for haar classifier cascade */
146 CVAPI(void) cvSetImagesForHaarClassifierCascade( CvHaarClassifierCascade* cascade,
147  const CvArr* sum, const CvArr* sqsum,
148  const CvArr* tilted_sum, double scale );
149 
150 /* runs the cascade on the specified window */
151 CVAPI(int) cvRunHaarClassifierCascade( const CvHaarClassifierCascade* cascade,
152  CvPoint pt, int start_stage CV_DEFAULT(0));
153 
154 
155 /****************************************************************************************\
156 * Latent SVM Object Detection functions *
157 \****************************************************************************************/
158 
159 // DataType: STRUCT position
160 // Structure describes the position of the filter in the feature pyramid
161 // l - level in the feature pyramid
162 // (x, y) - coordinate in level l
163 typedef struct CvLSVMFilterPosition
164 {
165  int x;
166  int y;
167  int l;
169 
170 // DataType: STRUCT filterObject
171 // Description of the filter, which corresponds to the part of the object
172 // V - ideal (penalty = 0) position of the partial filter
173 // from the root filter position (V_i in the paper)
174 // penaltyFunction - vector describes penalty function (d_i in the paper)
175 // pf[0] * x + pf[1] * y + pf[2] * x^2 + pf[3] * y^2
176 // FILTER DESCRIPTION
177 // Rectangular map (sizeX x sizeY),
178 // every cell stores feature vector (dimension = p)
179 // H - matrix of feature vectors
180 // to set and get feature vectors (i,j)
181 // used formula H[(j * sizeX + i) * p + k], where
182 // k - component of feature vector in cell (i, j)
183 // END OF FILTER DESCRIPTION
184 typedef struct CvLSVMFilterObject{
186  float fineFunction[4];
187  int sizeX;
188  int sizeY;
190  float *H;
192 
193 // data type: STRUCT CvLatentSvmDetector
194 // structure contains internal representation of trained Latent SVM detector
195 // num_filters - total number of filters (root plus part) in model
196 // num_components - number of components in model
197 // num_part_filters - array containing number of part filters for each component
198 // filters - root and part filters for all model components
199 // b - biases for all model components
200 // score_threshold - confidence level threshold
201 typedef struct CvLatentSvmDetector
202 {
207  float* b;
209 }
211 
212 // data type: STRUCT CvObjectDetection
213 // structure contains the bounding box and confidence level for detected object
214 // rect - bounding box for a detected object
215 // score - confidence level
216 typedef struct CvObjectDetection
217 {
219  float score;
221 
223 
224 
225 /*
226 // load trained detector from a file
227 //
228 // API
229 // CvLatentSvmDetector* cvLoadLatentSvmDetector(const char* filename);
230 // INPUT
231 // filename - path to the file containing the parameters of
232  - trained Latent SVM detector
233 // OUTPUT
234 // trained Latent SVM detector in internal representation
235 */
236 CVAPI(CvLatentSvmDetector*) cvLoadLatentSvmDetector(const char* filename);
237 
238 /*
239 // release memory allocated for CvLatentSvmDetector structure
240 //
241 // API
242 // void cvReleaseLatentSvmDetector(CvLatentSvmDetector** detector);
243 // INPUT
244 // detector - CvLatentSvmDetector structure to be released
245 // OUTPUT
246 */
247 CVAPI(void) cvReleaseLatentSvmDetector(CvLatentSvmDetector** detector);
248 
249 /*
250 // find rectangular regions in the given image that are likely
251 // to contain objects and corresponding confidence levels
252 //
253 // API
254 // CvSeq* cvLatentSvmDetectObjects(const IplImage* image,
255 // CvLatentSvmDetector* detector,
256 // CvMemStorage* storage,
257 // float overlap_threshold = 0.5f,
258 // int numThreads = -1);
259 // INPUT
260 // image - image to detect objects in
261 // detector - Latent SVM detector in internal representation
262 // storage - memory storage to store the resultant sequence
263 // of the object candidate rectangles
264 // overlap_threshold - threshold for the non-maximum suppression algorithm
265  = 0.5f [here will be the reference to original paper]
266 // OUTPUT
267 // sequence of detected objects (bounding boxes and confidence levels stored in CvObjectDetection structures)
268 */
269 CVAPI(CvSeq*) cvLatentSvmDetectObjects(IplImage* image,
272  float overlap_threshold CV_DEFAULT(0.5f),
273  int numThreads CV_DEFAULT(-1));
274 
275 #ifdef __cplusplus
276 }
277 
278 CV_EXPORTS CvSeq* cvHaarDetectObjectsForROC( const CvArr* image,
280  std::vector<int>& rejectLevels, std::vector<double>& levelWeightds,
281  double scale_factor CV_DEFAULT(1.1),
282  int min_neighbors CV_DEFAULT(3), int flags CV_DEFAULT(0),
283  CvSize min_size CV_DEFAULT(cvSize(0,0)), CvSize max_size CV_DEFAULT(cvSize(0,0)),
284  bool outputRejectLevels = false );
285 
286 namespace cv
287 {
288 
290 
291 /*
292  * This is a class wrapping up the structure CvLatentSvmDetector and functions working with it.
293  * The class goals are:
294  * 1) provide c++ interface;
295  * 2) make it possible to load and detect more than one class (model) unlike CvLatentSvmDetector.
296  */
297 class CV_EXPORTS LatentSvmDetector
298 {
299 public:
300  struct CV_EXPORTS ObjectDetection
301  {
302  ObjectDetection();
303  ObjectDetection( const Rect& rect, float score, int classID=-1 );
305  float score;
306  int classID;
307  };
308 
310  LatentSvmDetector( const vector<string>& filenames, const vector<string>& classNames=vector<string>() );
311  virtual ~LatentSvmDetector();
312 
313  virtual void clear();
314  virtual bool empty() const;
315  bool load( const vector<string>& filenames, const vector<string>& classNames=vector<string>() );
316 
317  virtual void detect( const Mat& image,
318  vector<ObjectDetection>& objectDetections,
319  float overlapThreshold=0.5f,
320  int numThreads=-1 );
321 
322  const vector<string>& getClassNames() const;
323  size_t getClassCount() const;
324 
325 private:
326  vector<CvLatentSvmDetector*> detectors;
327  vector<string> classNames;
328 };
329 
330 // class for grouping object candidates, detected by Cascade Classifier, HOG etc.
331 // instance of the class is to be passed to cv::partition (see cxoperations.hpp)
332 class CV_EXPORTS SimilarRects
333 {
334 public:
335  SimilarRects(double _eps) : eps(_eps) {}
336  inline bool operator()(const Rect& r1, const Rect& r2) const
337  {
338  double delta = eps*(std::min(r1.width, r2.width) + std::min(r1.height, r2.height))*0.5;
339  return std::abs(r1.x - r2.x) <= delta &&
340  std::abs(r1.y - r2.y) <= delta &&
341  std::abs(r1.x + r1.width - r2.x - r2.width) <= delta &&
342  std::abs(r1.y + r1.height - r2.y - r2.height) <= delta;
343  }
344  double eps;
345 };
346 
347 CV_EXPORTS void groupRectangles(CV_OUT CV_IN_OUT vector<Rect>& rectList, int groupThreshold, double eps=0.2);
348 CV_EXPORTS_W void groupRectangles(CV_OUT CV_IN_OUT vector<Rect>& rectList, CV_OUT vector<int>& weights, int groupThreshold, double eps=0.2);
349 CV_EXPORTS void groupRectangles( vector<Rect>& rectList, int groupThreshold, double eps, vector<int>* weights, vector<double>* levelWeights );
350 CV_EXPORTS void groupRectangles(vector<Rect>& rectList, vector<int>& rejectLevels,
351  vector<double>& levelWeights, int groupThreshold, double eps=0.2);
352 CV_EXPORTS void groupRectangles_meanshift(vector<Rect>& rectList, vector<double>& foundWeights, vector<double>& foundScales,
353  double detectThreshold = 0.0, Size winDetSize = Size(64, 128));
354 
355 
356 class CV_EXPORTS FeatureEvaluator
357 {
358 public:
359  enum { HAAR = 0, LBP = 1, HOG = 2 };
360  virtual ~FeatureEvaluator();
361 
362  virtual bool read(const FileNode& node);
363  virtual Ptr<FeatureEvaluator> clone() const;
364  virtual int getFeatureType() const;
365 
366  virtual bool setImage(const Mat& img, Size origWinSize);
367  virtual bool setWindow(Point p);
368 
369  virtual double calcOrd(int featureIdx) const;
370  virtual int calcCat(int featureIdx) const;
371 
372  static Ptr<FeatureEvaluator> create(int type);
373 };
374 
375 template<> CV_EXPORTS void Ptr<CvHaarClassifierCascade>::delete_obj();
376 
377 enum
378 {
383 };
384 
385 class CV_EXPORTS_W CascadeClassifier
386 {
387 public:
388  CV_WRAP CascadeClassifier();
389  CV_WRAP CascadeClassifier( const string& filename );
390  virtual ~CascadeClassifier();
391 
392  CV_WRAP virtual bool empty() const;
393  CV_WRAP bool load( const string& filename );
394  virtual bool read( const FileNode& node );
395  CV_WRAP virtual void detectMultiScale( const Mat& image,
396  CV_OUT vector<Rect>& objects,
397  double scaleFactor=1.1,
398  int minNeighbors=3, int flags=0,
399  Size minSize=Size(),
400  Size maxSize=Size() );
401 
402  CV_WRAP virtual void detectMultiScale( const Mat& image,
403  CV_OUT vector<Rect>& objects,
404  vector<int>& rejectLevels,
405  vector<double>& levelWeights,
406  double scaleFactor=1.1,
407  int minNeighbors=3, int flags=0,
408  Size minSize=Size(),
409  Size maxSize=Size(),
410  bool outputRejectLevels=false );
411 
412 
413  bool isOldFormatCascade() const;
414  virtual Size getOriginalWindowSize() const;
415  int getFeatureType() const;
416  bool setImage( const Mat& );
417 
418 protected:
419  //virtual bool detectSingleScale( const Mat& image, int stripCount, Size processingRectSize,
420  // int stripSize, int yStep, double factor, vector<Rect>& candidates );
421 
422  virtual bool detectSingleScale( const Mat& image, int stripCount, Size processingRectSize,
423  int stripSize, int yStep, double factor, vector<Rect>& candidates,
424  vector<int>& rejectLevels, vector<double>& levelWeights, bool outputRejectLevels=false);
425 
426 protected:
427  enum { BOOST = 0 };
428  enum { DO_CANNY_PRUNING = 1, SCALE_IMAGE = 2,
429  FIND_BIGGEST_OBJECT = 4, DO_ROUGH_SEARCH = 8 };
430 
431  friend class CascadeClassifierInvoker;
432 
433  template<class FEval>
434  friend int predictOrdered( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);
435 
436  template<class FEval>
437  friend int predictCategorical( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);
438 
439  template<class FEval>
440  friend int predictOrderedStump( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);
441 
442  template<class FEval>
443  friend int predictCategoricalStump( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);
444 
445  bool setImage( Ptr<FeatureEvaluator>& feval, const Mat& image);
446  virtual int runAt( Ptr<FeatureEvaluator>& feval, Point pt, double& weight );
447 
448  class Data
449  {
450  public:
451  struct CV_EXPORTS DTreeNode
452  {
454  float threshold; // for ordered features only
455  int left;
456  int right;
457  };
458 
459  struct CV_EXPORTS DTree
460  {
462  };
463 
464  struct CV_EXPORTS Stage
465  {
466  int first;
467  int ntrees;
468  float threshold;
469  };
470 
471  bool read(const FileNode &node);
472 
474 
479 
480  vector<Stage> stages;
481  vector<DTree> classifiers;
482  vector<DTreeNode> nodes;
483  vector<float> leaves;
484  vector<int> subsets;
485  };
486 
490 
491 public:
492  class CV_EXPORTS MaskGenerator
493  {
494  public:
495  virtual ~MaskGenerator() {}
496  virtual cv::Mat generateMask(const cv::Mat& src)=0;
497  virtual void initializeMask(const cv::Mat& /*src*/) {};
498  };
499  void setMaskGenerator(Ptr<MaskGenerator> maskGenerator);
500  Ptr<MaskGenerator> getMaskGenerator();
501 
502  void setFaceDetectionMaskGenerator();
503 
504 protected:
506 };
507 
508 
510 
511 // struct for detection region of interest (ROI)
513 {
514  // scale(size) of the bounding box
515  double scale;
516  // set of requrested locations to be evaluated
517  vector<cv::Point> locations;
518  // vector that will contain confidence values for each location
519  vector<double> confidences;
520 };
521 
522 struct CV_EXPORTS_W HOGDescriptor
523 {
524 public:
525  enum { L2Hys=0 };
526  enum { DEFAULT_NLEVELS=64 };
527 
528  CV_WRAP HOGDescriptor() : winSize(64,128), blockSize(16,16), blockStride(8,8),
529  cellSize(8,8), nbins(9), derivAperture(1), winSigma(-1),
530  histogramNormType(HOGDescriptor::L2Hys), L2HysThreshold(0.2), gammaCorrection(true),
531  nlevels(HOGDescriptor::DEFAULT_NLEVELS)
532  {}
533 
534  CV_WRAP HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride,
535  Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1,
536  int _histogramNormType=HOGDescriptor::L2Hys,
537  double _L2HysThreshold=0.2, bool _gammaCorrection=false,
538  int _nlevels=HOGDescriptor::DEFAULT_NLEVELS)
539  : winSize(_winSize), blockSize(_blockSize), blockStride(_blockStride), cellSize(_cellSize),
540  nbins(_nbins), derivAperture(_derivAperture), winSigma(_winSigma),
541  histogramNormType(_histogramNormType), L2HysThreshold(_L2HysThreshold),
542  gammaCorrection(_gammaCorrection), nlevels(_nlevels)
543  {}
544 
545  CV_WRAP HOGDescriptor(const String& filename)
546  {
547  load(filename);
548  }
549 
551  {
552  d.copyTo(*this);
553  }
554 
555  virtual ~HOGDescriptor() {}
556 
557  CV_WRAP size_t getDescriptorSize() const;
558  CV_WRAP bool checkDetectorSize() const;
559  CV_WRAP double getWinSigma() const;
560 
561  CV_WRAP virtual void setSVMDetector(InputArray _svmdetector);
562 
563  virtual bool read(FileNode& fn);
564  virtual void write(FileStorage& fs, const String& objname) const;
565 
566  CV_WRAP virtual bool load(const String& filename, const String& objname=String());
567  CV_WRAP virtual void save(const String& filename, const String& objname=String()) const;
568  virtual void copyTo(HOGDescriptor& c) const;
569 
570  CV_WRAP virtual void compute(const Mat& img,
571  CV_OUT vector<float>& descriptors,
572  Size winStride=Size(), Size padding=Size(),
573  const vector<Point>& locations=vector<Point>()) const;
574  //with found weights output
575  CV_WRAP virtual void detect(const Mat& img, CV_OUT vector<Point>& foundLocations,
576  CV_OUT vector<double>& weights,
577  double hitThreshold=0, Size winStride=Size(),
578  Size padding=Size(),
579  const vector<Point>& searchLocations=vector<Point>()) const;
580  //without found weights output
581  virtual void detect(const Mat& img, CV_OUT vector<Point>& foundLocations,
582  double hitThreshold=0, Size winStride=Size(),
583  Size padding=Size(),
584  const vector<Point>& searchLocations=vector<Point>()) const;
585  //with result weights output
586  CV_WRAP virtual void detectMultiScale(const Mat& img, CV_OUT vector<Rect>& foundLocations,
587  CV_OUT vector<double>& foundWeights, double hitThreshold=0,
588  Size winStride=Size(), Size padding=Size(), double scale=1.05,
589  double finalThreshold=2.0,bool useMeanshiftGrouping = false) const;
590  //without found weights output
591  virtual void detectMultiScale(const Mat& img, CV_OUT vector<Rect>& foundLocations,
592  double hitThreshold=0, Size winStride=Size(),
593  Size padding=Size(), double scale=1.05,
594  double finalThreshold=2.0, bool useMeanshiftGrouping = false) const;
595 
596  CV_WRAP virtual void computeGradient(const Mat& img, CV_OUT Mat& grad, CV_OUT Mat& angleOfs,
597  Size paddingTL=Size(), Size paddingBR=Size()) const;
598 
599  CV_WRAP static vector<float> getDefaultPeopleDetector();
600  CV_WRAP static vector<float> getDaimlerPeopleDetector();
601 
602  CV_PROP Size winSize;
603  CV_PROP Size blockSize;
604  CV_PROP Size blockStride;
605  CV_PROP Size cellSize;
606  CV_PROP int nbins;
607  CV_PROP int derivAperture;
608  CV_PROP double winSigma;
609  CV_PROP int histogramNormType;
610  CV_PROP double L2HysThreshold;
611  CV_PROP bool gammaCorrection;
612  CV_PROP vector<float> svmDetector;
613  CV_PROP int nlevels;
614 
615 
616  // evaluate specified ROI and return confidence value for each location
617  void detectROI(const cv::Mat& img, const vector<cv::Point> &locations,
618  CV_OUT std::vector<cv::Point>& foundLocations, CV_OUT std::vector<double>& confidences,
619  double hitThreshold = 0, cv::Size winStride = Size(),
620  cv::Size padding = Size()) const;
621 
622  // evaluate specified ROI and return confidence value for each location in multiple scales
623  void detectMultiScaleROI(const cv::Mat& img,
624  CV_OUT std::vector<cv::Rect>& foundLocations,
625  std::vector<DetectionROI>& locations,
626  double hitThreshold = 0,
627  int groupThreshold = 0) const;
628 
629  // read/parse Dalal's alt model file
630  void readALTModel(std::string modelfile);
631  void groupRectangles(vector<cv::Rect>& rectList, vector<double>& weights, int groupThreshold, double eps) const;
632 };
633 
634 
635 CV_EXPORTS_W void findDataMatrix(InputArray image,
636  CV_OUT vector<string>& codes,
639 CV_EXPORTS_W void drawDataMatrixCodes(InputOutputArray image,
640  const vector<string>& codes,
642 }
643 
644 /****************************************************************************************\
645 * Datamatrix *
646 \****************************************************************************************/
647 
648 struct CV_EXPORTS CvDataMatrixCode {
649  char msg[4];
652 };
653 
654 CV_EXPORTS std::deque<CvDataMatrixCode> cvFindDataMatrix(CvMat *im);
655 
656 /****************************************************************************************\
657 * LINE-MOD *
658 \****************************************************************************************/
659 
660 namespace cv {
661 namespace linemod {
662 
663 using cv::FileNode;
664 using cv::FileStorage;
665 using cv::Mat;
666 using cv::noArray;
668 using cv::Point;
669 using cv::Ptr;
670 using cv::Rect;
671 using cv::Size;
672 
674 
678 struct CV_EXPORTS Feature
679 {
680  int x;
681  int y;
682  int label;
683 
684  Feature() : x(0), y(0), label(0) {}
685  Feature(int x, int y, int label);
686 
687  void read(const FileNode& fn);
688  void write(FileStorage& fs) const;
689 };
690 
691 inline Feature::Feature(int _x, int _y, int _label) : x(_x), y(_y), label(_label) {}
692 
693 struct CV_EXPORTS Template
694 {
695  int width;
696  int height;
698  std::vector<Feature> features;
699 
700  void read(const FileNode& fn);
701  void write(FileStorage& fs) const;
702 };
703 
708 {
709 public:
710  // Virtual destructor
711  virtual ~QuantizedPyramid() {}
712 
719  virtual void quantize(Mat& dst) const =0;
720 
726  virtual bool extractTemplate(Template& templ) const =0;
727 
733  virtual void pyrDown() =0;
734 
735 protected:
737  struct Candidate
738  {
739  Candidate(int x, int y, int label, float score);
740 
742  bool operator<(const Candidate& rhs) const
743  {
744  return score > rhs.score;
745  }
746 
748  float score;
749  };
750 
759  static void selectScatteredFeatures(const std::vector<Candidate>& candidates,
760  std::vector<Feature>& features,
761  size_t num_features, float distance);
762 };
763 
764 inline QuantizedPyramid::Candidate::Candidate(int x, int y, int label, float _score) : f(x, y, label), score(_score) {}
765 
771 class CV_EXPORTS Modality
772 {
773 public:
774  // Virtual destructor
775  virtual ~Modality() {}
776 
785  const Mat& mask = Mat()) const
786  {
787  return processImpl(src, mask);
788  }
789 
790  virtual std::string name() const =0;
791 
792  virtual void read(const FileNode& fn) =0;
793  virtual void write(FileStorage& fs) const =0;
794 
802  static Ptr<Modality> create(const std::string& modality_type);
803 
807  static Ptr<Modality> create(const FileNode& fn);
808 
809 protected:
810  // Indirection is because process() has a default parameter.
811  virtual Ptr<QuantizedPyramid> processImpl(const Mat& src,
812  const Mat& mask) const =0;
813 };
814 
818 class CV_EXPORTS ColorGradient : public Modality
819 {
820 public:
824  ColorGradient();
825 
834  ColorGradient(float weak_threshold, size_t num_features, float strong_threshold);
835 
836  virtual std::string name() const;
837 
838  virtual void read(const FileNode& fn);
839  virtual void write(FileStorage& fs) const;
840 
842  size_t num_features;
844 
845 protected:
846  virtual Ptr<QuantizedPyramid> processImpl(const Mat& src,
847  const Mat& mask) const;
848 };
849 
853 class CV_EXPORTS DepthNormal : public Modality
854 {
855 public:
859  DepthNormal();
860 
871  DepthNormal(int distance_threshold, int difference_threshold, size_t num_features,
872  int extract_threshold);
873 
874  virtual std::string name() const;
875 
876  virtual void read(const FileNode& fn);
877  virtual void write(FileStorage& fs) const;
878 
881  size_t num_features;
883 
884 protected:
885  virtual Ptr<QuantizedPyramid> processImpl(const Mat& src,
886  const Mat& mask) const;
887 };
888 
892 void colormap(const Mat& quantized, Mat& dst);
893 
897 struct CV_EXPORTS Match
898 {
900  {
901  }
902 
903  Match(int x, int y, float similarity, const std::string& class_id, int template_id);
904 
906  bool operator<(const Match& rhs) const
907  {
908  // Secondarily sort on template_id for the sake of duplicate removal
909  if (similarity != rhs.similarity)
910  return similarity > rhs.similarity;
911  else
912  return template_id < rhs.template_id;
913  }
914 
915  bool operator==(const Match& rhs) const
916  {
917  return x == rhs.x && y == rhs.y && similarity == rhs.similarity && class_id == rhs.class_id;
918  }
919 
920  int x;
921  int y;
922  float similarity;
923  std::string class_id;
925 };
926 
927 inline Match::Match(int _x, int _y, float _similarity, const std::string& _class_id, int _template_id)
928  : x(_x), y(_y), similarity(_similarity), class_id(_class_id), template_id(_template_id)
929  {
930  }
931 
936 class CV_EXPORTS Detector
937 {
938 public:
942  Detector();
943 
951  Detector(const std::vector< Ptr<Modality> >& modalities, const std::vector<int>& T_pyramid);
952 
968  void match(const std::vector<Mat>& sources, float threshold, std::vector<Match>& matches,
969  const std::vector<std::string>& class_ids = std::vector<std::string>(),
970  OutputArrayOfArrays quantized_images = noArray(),
971  const std::vector<Mat>& masks = std::vector<Mat>()) const;
972 
983  int addTemplate(const std::vector<Mat>& sources, const std::string& class_id,
984  const Mat& object_mask, Rect* bounding_box = NULL);
985 
989  int addSyntheticTemplate(const std::vector<Template>& templates, const std::string& class_id);
990 
997  const std::vector< Ptr<Modality> >& getModalities() const { return modalities; }
998 
1002  int getT(int pyramid_level) const { return T_at_level[pyramid_level]; }
1003 
1007  int pyramidLevels() const { return pyramid_levels; }
1008 
1015  const std::vector<Template>& getTemplates(const std::string& class_id, int template_id) const;
1016 
1017  int numTemplates() const;
1018  int numTemplates(const std::string& class_id) const;
1019  int numClasses() const { return static_cast<int>(class_templates.size()); }
1020 
1021  std::vector<std::string> classIds() const;
1022 
1023  void read(const FileNode& fn);
1024  void write(FileStorage& fs) const;
1025 
1026  std::string readClass(const FileNode& fn, const std::string &class_id_override = "");
1027  void writeClass(const std::string& class_id, FileStorage& fs) const;
1028 
1029  void readClasses(const std::vector<std::string>& class_ids,
1030  const std::string& format = "templates_%s.yml.gz");
1031  void writeClasses(const std::string& format = "templates_%s.yml.gz") const;
1032 
1033 protected:
1034  std::vector< Ptr<Modality> > modalities;
1036  std::vector<int> T_at_level;
1037 
1038  typedef std::vector<Template> TemplatePyramid;
1039  typedef std::map<std::string, std::vector<TemplatePyramid> > TemplatesMap;
1041 
1042  typedef std::vector<Mat> LinearMemories;
1043  // Indexed as [pyramid level][modality][quantized label]
1044  typedef std::vector< std::vector<LinearMemories> > LinearMemoryPyramid;
1045 
1046  void matchClass(const LinearMemoryPyramid& lm_pyramid,
1047  const std::vector<Size>& sizes,
1048  float threshold, std::vector<Match>& matches,
1049  const std::string& class_id,
1050  const std::vector<TemplatePyramid>& template_pyramids) const;
1051 };
1052 
1058 CV_EXPORTS Ptr<Detector> getDefaultLINE();
1059 
1066 CV_EXPORTS Ptr<Detector> getDefaultLINEMOD();
1067 
1068 } // namespace linemod
1069 } // namespace cv
1070 
1071 #endif
1072 
1073 #endif
int width
Definition: objdetect.hpp:695
struct CvHaarFeature::@352 rect[CV_HAAR_FEATURE_MAX]
int child
Definition: objdetect.hpp:95
Point2i Point
Definition: core.hpp:893
CvPoint pt
Definition: objdetect.hpp:152
Definition: objdetect.hpp:385
bool operator<(const Match &rhs) const
Sort matches with high similarity to the front.
Definition: objdetect.hpp:906
float score
Definition: objdetect.hpp:219
size_t num_features
Definition: objdetect.hpp:881
SimilarRects(double _eps)
Definition: objdetect.hpp:335
Represents a modality operating over an image pyramid.
Definition: objdetect.hpp:707
int height
Definition: objdetect.hpp:696
std::vector< int > T_at_level
Definition: objdetect.hpp:1036
GLenum GLint GLint y
Definition: core_c.h:613
CvFileNode * node
Definition: core_c.h:1638
const int * sizes
Definition: core_c.h:212
int neighbors
Definition: objdetect.hpp:115
Definition: types_c.h:1021
Match()
Definition: objdetect.hpp:899
int difference_threshold
Definition: objdetect.hpp:880
Definition: objdetect.hpp:525
Ptr< QuantizedPyramid > process(const Mat &src, const Mat &mask=Mat()) const
Form a quantized image pyramid from a source image.
Definition: objdetect.hpp:784
float threshold
Definition: objdetect.hpp:454
const char const char ** filename
Definition: core_c.h:1750
int pyramid_levels
Definition: objdetect.hpp:1035
virtual ~Modality()
Definition: objdetect.hpp:775
Definition: objdetect.hpp:78
virtual bool extractTemplate(Template &templ) const =0
Extract most discriminant features at current pyramid level to form a new template.
int ntrees
Definition: objdetect.hpp:467
CvHidHaarClassifierCascade * hid_cascade
Definition: objdetect.hpp:109
int classID
Definition: objdetect.hpp:306
vector< DTree > classifiers
Definition: objdetect.hpp:481
bool isStumpBased
Definition: objdetect.hpp:473
void delete_obj()
deletes the object. Override if needed
Definition: operations.hpp:2612
File Storage Node class.
Definition: core.hpp:4119
CvSize real_window_size
Definition: objdetect.hpp:106
Size2i Size
Definition: core.hpp:896
CvHaarClassifierCascade CvMemStorage * storage
Definition: objdetect.hpp:140
int stageType
Definition: objdetect.hpp:475
Definition: objdetect.hpp:512
Definition: types_c.h:951
CV_PROP double winSigma
Definition: objdetect.hpp:608
Definition: types_c.h:1138
int pyramidLevels() const
Get number of pyramid levels used by this detector.
Definition: objdetect.hpp:1007
int y
y offset
Definition: objdetect.hpp:681
Definition: objdetect.hpp:464
Feature()
Definition: objdetect.hpp:684
int right
Definition: objdetect.hpp:456
Modality that computes quantized surface normals from a dense depth map.
Definition: objdetect.hpp:853
CV_PROP double L2HysThreshold
Definition: objdetect.hpp:610
std::vector< Feature > features
Definition: objdetect.hpp:698
int sizeX
Definition: objdetect.hpp:187
Definition: objdetect.hpp:448
Definition: objdetect.hpp:332
int * right
Definition: objdetect.hpp:84
CVAPI(CvHaarClassifierCascade *) cvLoadHaarClassifierCascade(const char *directory
GLuint src
Definition: core_c.h:1650
Modality that computes quantized gradient orientations from a color image.
Definition: objdetect.hpp:818
int int int flags
Definition: highgui_c.h:186
struct CvLSVMFilterPosition CvLSVMFilterPosition
int numFeatures
Definition: objdetect.hpp:189
CvMat int int num_features
Definition: ml.hpp:1999
CV_EXPORTS Ptr< Detector > getDefaultLINEMOD()
Factory function for detector using LINE-MOD algorithm with color gradients and depth normals...
int d
Definition: legacy.hpp:3064
CV_WRAP HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride, Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1, int _histogramNormType=HOGDescriptor::L2Hys, double _L2HysThreshold=0.2, bool _gammaCorrection=false, int _nlevels=HOGDescriptor::DEFAULT_NLEVELS)
Definition: objdetect.hpp:534
virtual void pyrDown()=0
Go to the next pyramid level.
float strong_threshold
Definition: objdetect.hpp:843
int nodeCount
Definition: objdetect.hpp:461
bool operator<(const Candidate &rhs) const
Sort candidates with high score to the front.
Definition: objdetect.hpp:742
double scale
Definition: objdetect.hpp:107
CvSize orig_window_size
Definition: objdetect.hpp:105
float fineFunction[4]
Definition: objdetect.hpp:186
GLXDrawable GLXDrawable read
int CvMemStorage int double eps
Definition: imgproc_c.h:353
CV_PROP int histogramNormType
Definition: objdetect.hpp:609
int template_id
Definition: objdetect.hpp:924
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: highgui_c.h:230
CV_PROP Size winSize
Definition: objdetect.hpp:602
CV_INLINE CvSize cvSize(int width, int height)
Definition: types_c.h:1145
_Tp height
Definition: core.hpp:883
int label
Quantization.
Definition: objdetect.hpp:682
struct CvHaarStageClassifier CvHaarStageClassifier
CV_EXPORTS_W void write(FileStorage &fs, const string &name, int value)
double eps
Definition: objdetect.hpp:344
CvRect rect
Definition: objdetect.hpp:114
CvHaarClassifier * classifier
Definition: objdetect.hpp:92
const std::vector< Ptr< Modality > > & getModalities() const
Get the modalities used by this detector.
Definition: objdetect.hpp:997
Definition: objdetect.hpp:297
Proxy datatype for passing Mat's and vector<>'s as input parameters.
Definition: core.hpp:1312
vector< double > confidences
Definition: objdetect.hpp:519
CvRect rect
Definition: core_c.h:100
void clear(const ColorA &color=ColorA::black(), bool clearDepthBuffer=true)
virtual void initializeMask(const cv::Mat &)
Definition: objdetect.hpp:497
float weight
Definition: objdetect.hpp:74
vector< Stage > stages
Definition: objdetect.hpp:480
The 2D size class.
Definition: core.hpp:81
int featureType
Definition: objdetect.hpp:476
Definition: objdetect.hpp:648
int y
Definition: objdetect.hpp:921
float score
Definition: objdetect.hpp:305
CvMat * original
Definition: objdetect.hpp:650
const char * name
Definition: core_c.h:1538
const GLbyte * weights
Definition: objdetect.hpp:526
vector< float > leaves
Definition: objdetect.hpp:483
GLint GLvoid * img
Definition: legacy.hpp:1150
struct CvHidHaarClassifierCascade CvHidHaarClassifierCascade
Definition: objdetect.hpp:99
Definition: objdetect.hpp:356
HOGDescriptor(const HOGDescriptor &d)
Definition: objdetect.hpp:550
Ptr< MaskGenerator > maskGenerator
Definition: objdetect.hpp:505
SourceFileRef load(const DataSourceRef &dataSource, size_t sampleRate=0)
int x
Definition: objdetect.hpp:920
int * left
Definition: objdetect.hpp:83
Discriminant feature described by its location and label.
Definition: objdetect.hpp:678
CV_PROP int derivAperture
Definition: objdetect.hpp:607
CV_PROP int nbins
Definition: objdetect.hpp:606
std::vector< Ptr< Modality > > modalities
Definition: objdetect.hpp:1034
int num_components
Definition: objdetect.hpp:204
struct CvHaarFeature CvHaarFeature
struct CvHaarClassifierCascade CvHaarClassifierCascade
CV_EXPORTS void gammaCorrection(const GpuMat &src, GpuMat &dst, bool forward=true, Stream &stream=Stream::Null())
Routines for correcting image color gamma.
int * num_part_filters
Definition: objdetect.hpp:205
int tilted
Definition: objdetect.hpp:70
int getT(int pyramid_level) const
Get sampling step T at pyramid_level.
Definition: objdetect.hpp:1002
_Tp y
Definition: core.hpp:883
vector< DTreeNode > nodes
Definition: objdetect.hpp:482
Definition: objdetect.hpp:451
CV_PROP Size blockSize
Definition: objdetect.hpp:603
int count
Definition: objdetect.hpp:80
virtual ~MaskGenerator()
Definition: objdetect.hpp:495
std::string String
Definition: core.hpp:85
float threshold
Definition: objdetect.hpp:91
CvRect rect
Definition: objdetect.hpp:218
Definition: types_c.h:1272
Definition: objdetect.hpp:382
vector< cv::Point > locations
Definition: objdetect.hpp:517
std::vector< Template > TemplatePyramid
Definition: objdetect.hpp:1038
CV_EXPORTS_W void findDataMatrix(InputArray image, CV_OUT vector< string > &codes, OutputArray corners=noArray(), OutputArrayOfArrays dmtx=noArray())
CV_EXPORTS_W void min(InputArray src1, InputArray src2, OutputArray dst)
computes per-element minimum of two arrays (dst = min(src1, src2))
CvSize int int int CvPoint int delta
Definition: core_c.h:1427
CV_EXPORTS std::deque< CvDataMatrixCode > cvFindDataMatrix(CvMat *im)
Definition: objdetect.hpp:380
struct CvHaarClassifier CvHaarClassifier
virtual ~HOGDescriptor()
Definition: objdetect.hpp:555
Definition: objdetect.hpp:459
const CvArr const CvArr const CvArr * tilted_sum
Definition: objdetect.hpp:147
GLenum GLint x
Definition: core_c.h:632
Definition: objdetect.hpp:381
void CvArr
Definition: types_c.h:196
CV_EXPORTS MatExpr abs(const Mat &m)
vector< int > subsets
Definition: objdetect.hpp:484
int distance_threshold
Definition: objdetect.hpp:879
Definition: objdetect.hpp:68
void colormap(const Mat &quantized, Mat &dst)
Debug function to colormap a quantized image for viewing.
_Tp x
Definition: core.hpp:883
CV_PROP Size blockStride
Definition: objdetect.hpp:604
CvLSVMFilterObject ** filters
Definition: objdetect.hpp:206
std::vector< Mat > LinearMemories
Definition: objdetect.hpp:1042
std::string class_id
Definition: objdetect.hpp:923
Interface for modalities that plug into the LINE template matching representation.
Definition: objdetect.hpp:771
virtual ~QuantizedPyramid()
Definition: objdetect.hpp:711
int x
Definition: objdetect.hpp:165
Definition: objdetect.hpp:300
int featureIdx
Definition: objdetect.hpp:453
Object detector using the LINE template matching algorithm with any set of modalities.
Definition: objdetect.hpp:936
OutputArray OutputArrayOfArrays
Definition: core.hpp:1450
Definition: types_c.h:645
CV_EXPORTS_W double threshold(InputArray src, OutputArray dst, double thresh, double maxval, int type)
applies fixed threshold to the image
XML/YAML File Storage Class.
Definition: core.hpp:4040
Definition: objdetect.hpp:693
float * threshold
Definition: objdetect.hpp:82
int parent
Definition: objdetect.hpp:96
The Core Functionality.
double scale
Definition: objdetect.hpp:515
Feature f
Definition: objdetect.hpp:747
Definition: objdetect.hpp:88
The n-dimensional matrix class.
Definition: core.hpp:1688
int first
Definition: objdetect.hpp:466
CV_PROP Size cellSize
Definition: objdetect.hpp:605
int left
Definition: objdetect.hpp:455
Data data
Definition: objdetect.hpp:487
float score
Definition: objdetect.hpp:748
Definition: types_c.h:465
Definition: objdetect.hpp:522
Definition: objdetect.hpp:492
float threshold
Definition: objdetect.hpp:468
float * H
Definition: objdetect.hpp:190
const CvArr CvSeq CvSeq ** descriptors
Definition: compat.hpp:647
CvSize CvPoint2D32f * corners
Definition: calib3d.hpp:215
float score_threshold
Definition: objdetect.hpp:208
Rect rect
Definition: objdetect.hpp:304
int l
Definition: objdetect.hpp:167
int y
Definition: objdetect.hpp:166
GLfloat GLfloat p
GLuint GLuint GLsizei GLenum type
Definition: core_c.h:114
const GLubyte * c
Definition: legacy.hpp:633
Definition: objdetect.hpp:101
Definition: objdetect.hpp:163
CV_PROP vector< float > svmDetector
Definition: objdetect.hpp:612
int count
Definition: objdetect.hpp:104
template 2D point class.
Definition: core.hpp:82
CV_EXPORTS CvSeq * cvHaarDetectObjectsForROC(const CvArr *image, CvHaarClassifierCascade *cascade, CvMemStorage *storage, std::vector< int > &rejectLevels, std::vector< double > &levelWeightds, double scale_factor CV_DEFAULT(1.1), int min_neighbors CV_DEFAULT(3), int flags CV_DEFAULT(0), CvSize min_size CV_DEFAULT(cvSize(0, 0)), CvSize max_size CV_DEFAULT(cvSize(0, 0)), bool outputRejectLevels=false)
int flags
Definition: objdetect.hpp:103
Size origWinSize
Definition: objdetect.hpp:478
int num_filters
Definition: objdetect.hpp:203
float weak_threshold
Definition: objdetect.hpp:841
int count
Definition: objdetect.hpp:90
CV_EXPORTS_W void drawDataMatrixCodes(InputOutputArray image, const vector< string > &codes, InputArray corners)
int pyramid_level
Definition: objdetect.hpp:697
Candidate(int x, int y, int label, float score)
Definition: objdetect.hpp:764
GLsizei const GLint * locations
CvSize orig_window_size
Definition: objdetect.hpp:121
double factor
Definition: imgproc_c.h:459
CvMat * corners
Definition: objdetect.hpp:651
CvLSVMFilterPosition V
Definition: objdetect.hpp:185
Definition: types_c.h:1333
GLenum GLsizei GLenum format
CvHaarClassifierCascade CvMemStorage double scale_factor CV_DEFAULT(1.1)
GLuint dst
Definition: calib3d.hpp:134
float similarity
Definition: objdetect.hpp:922
TemplatesMap class_templates
Definition: objdetect.hpp:1040
CvHaarStageClassifier * stage_classifier
Definition: objdetect.hpp:108
const CvArr * sum
Definition: objdetect.hpp:147
true
Definition: color.hpp:221
const CvArr * templ
Definition: imgproc_c.h:281
Smart pointer to dynamically allocated objects.
Definition: core.hpp:1268
CV_EXPORTS void groupRectangles_meanshift(vector< Rect > &rectList, vector< double > &foundWeights, vector< double > &foundScales, double detectThreshold=0.0, Size winDetSize=Size(64, 128))
Definition: objdetect.hpp:201
std::vector< std::vector< LinearMemories > > LinearMemoryPyramid
Definition: objdetect.hpp:1044
bool operator()(const Rect &r1, const Rect &r2) const
Definition: objdetect.hpp:336
virtual void quantize(Mat &dst) const =0
Compute quantized image at current pyramid level for online detection.
Definition: objdetect.hpp:184
CvHaarFeature * haar_feature
Definition: objdetect.hpp:81
class CV_EXPORTS FileNode
Definition: core.hpp:3941
Definition: objdetect.hpp:379
CV_EXPORTS OutputArray noArray()
GLenum GLenum GLenum GLenum GLenum scale
std::map< std::string, std::vector< TemplatePyramid > > TemplatesMap
Definition: objdetect.hpp:1039
CvRect r
Definition: objdetect.hpp:73
int sizeY
Definition: objdetect.hpp:188
virtual void copyTo(HOGDescriptor &c) const
CvHaarClassifierCascade * cascade
Definition: objdetect.hpp:140
Ptr< FeatureEvaluator > featureEvaluator
Definition: objdetect.hpp:488
Candidate feature with a score.
Definition: objdetect.hpp:737
struct CvLatentSvmDetector CvLatentSvmDetector
Definition: objdetect.hpp:112
struct CvLSVMFilterObject CvLSVMFilterObject
GLenum GLint GLuint mask
Definition: tracking.hpp:132
struct CvObjectDetection CvObjectDetection
GLclampf f
CV_WRAP HOGDescriptor(const String &filename)
Definition: objdetect.hpp:545
CV_WRAP HOGDescriptor()
Definition: objdetect.hpp:528
CvLatentSvmDetector * detector
Definition: objdetect.hpp:270
Definition: objdetect.hpp:216
const CvArr const CvArr * sqsum
Definition: objdetect.hpp:147
Ptr< CvHaarClassifierCascade > oldCascade
Definition: objdetect.hpp:489
size_t num_features
Definition: objdetect.hpp:842
Proxy datatype for passing Mat's and vector<>'s as input parameters.
Definition: core.hpp:1400
CV_EXPORTS Ptr< Detector > getDefaultLINE()
Factory function for detector using LINE algorithm with color gradients.
CV_EXPORTS void groupRectangles(CV_OUT CV_IN_OUT vector< Rect > &rectList, int groupThreshold, double eps=0.2)
struct CvAvgComp CvAvgComp
CV_PROP int nlevels
Definition: objdetect.hpp:613
CV_PROP bool gammaCorrection
Definition: objdetect.hpp:611
static void selectScatteredFeatures(const std::vector< Candidate > &candidates, std::vector< Feature > &features, size_t num_features, float distance)
Choose candidate features so that they are not bunched together.
Represents a successful template match.
Definition: objdetect.hpp:897
Rect_< int > Rect
Definition: core.hpp:897
float * b
Definition: objdetect.hpp:207
int numClasses() const
Definition: objdetect.hpp:1019
_Tp width
Definition: core.hpp:883
int extract_threshold
Definition: objdetect.hpp:882
float * alpha
Definition: objdetect.hpp:85
int next
Definition: objdetect.hpp:94
bool operator==(const Match &rhs) const
Definition: objdetect.hpp:915
int x
x offset
Definition: objdetect.hpp:680
int ncategories
Definition: objdetect.hpp:477