1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #ifndef __OPENCV_OBJDETECT_HPP__
44 #define __OPENCV_OBJDETECT_HPP__
46 #include "opencv2/core/core.hpp"
55 /****************************************************************************************\
56 * Haar-like Object Detection functions *
57 \****************************************************************************************/
59 #define CV_HAAR_MAGIC_VAL 0x42500000
60 #define CV_TYPE_NAME_HAAR "opencv-haar-classifier"
62 #define CV_IS_HAAR_CLASSIFIER( haar ) \
64 (((const CvHaarClassifierCascade*)(haar))->flags & CV_MAGIC_MASK)==CV_HAAR_MAGIC_VAL)
66 #define CV_HAAR_FEATURE_MAX 3
68 typedef struct CvHaarFeature
75 } rect[CV_HAAR_FEATURE_MAX];
78 typedef struct CvHaarClassifier
81 CvHaarFeature* haar_feature;
88 typedef struct CvHaarStageClassifier
92 CvHaarClassifier* classifier;
97 } CvHaarStageClassifier;
99 typedef struct CvHidHaarClassifierCascade CvHidHaarClassifierCascade;
101 typedef struct CvHaarClassifierCascade
105 CvSize orig_window_size;
106 CvSize real_window_size;
108 CvHaarStageClassifier* stage_classifier;
109 CvHidHaarClassifierCascade* hid_cascade;
110 } CvHaarClassifierCascade;
112 typedef struct CvAvgComp
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);
123 CVAPI(void) cvReleaseHaarClassifierCascade( CvHaarClassifierCascade** cascade );
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
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 );
139 CVAPI(CvSeq*) cvHaarDetectObjects( const CvArr* image,
140 CvHaarClassifierCascade* cascade, CvMemStorage* storage,
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)));
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 );
150 /* runs the cascade on the specified window */
151 CVAPI(int) cvRunHaarClassifierCascade( const CvHaarClassifierCascade* cascade,
152 CvPoint pt, int start_stage CV_DEFAULT(0));
155 /****************************************************************************************\
156 * Latent SVM Object Detection functions *
157 \****************************************************************************************/
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
168 } CvLSVMFilterPosition;
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{
185 CvLSVMFilterPosition V;
186 float fineFunction[4];
191 } CvLSVMFilterObject;
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
205 int* num_part_filters;
206 CvLSVMFilterObject** filters;
208 float score_threshold;
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
222 //////////////// Object Detection using Latent SVM //////////////
226 // load trained detector from a file
229 // CvLatentSvmDetector* cvLoadLatentSvmDetector(const char* filename);
231 // filename - path to the file containing the parameters of
232 - trained Latent SVM detector
234 // trained Latent SVM detector in internal representation
236 CVAPI(CvLatentSvmDetector*) cvLoadLatentSvmDetector(const char* filename);
239 // release memory allocated for CvLatentSvmDetector structure
242 // void cvReleaseLatentSvmDetector(CvLatentSvmDetector** detector);
244 // detector - CvLatentSvmDetector structure to be released
247 CVAPI(void) cvReleaseLatentSvmDetector(CvLatentSvmDetector** detector);
250 // find rectangular regions in the given image that are likely
251 // to contain objects and corresponding confidence levels
254 // CvSeq* cvLatentSvmDetectObjects(const IplImage* image,
255 // CvLatentSvmDetector* detector,
256 // CvMemStorage* storage,
257 // float overlap_threshold = 0.5f,
258 // int numThreads = -1);
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]
267 // sequence of detected objects (bounding boxes and confidence levels stored in CvObjectDetection structures)
269 CVAPI(CvSeq*) cvLatentSvmDetectObjects(IplImage* image,
270 CvLatentSvmDetector* detector,
271 CvMemStorage* storage,
272 float overlap_threshold CV_DEFAULT(0.5f),
273 int numThreads CV_DEFAULT(-1));
278 CV_EXPORTS CvSeq* cvHaarDetectObjectsForROC( const CvArr* image,
279 CvHaarClassifierCascade* cascade, CvMemStorage* storage,
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 );
289 ///////////////////////////// Object Detection ////////////////////////////
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.
297 class CV_EXPORTS LatentSvmDetector
300 struct CV_EXPORTS ObjectDetection
303 ObjectDetection( const Rect& rect, float score, int classID=-1 );
310 LatentSvmDetector( const vector<string>& filenames, const vector<string>& classNames=vector<string>() );
311 virtual ~LatentSvmDetector();
313 virtual void clear();
314 virtual bool empty() const;
315 bool load( const vector<string>& filenames, const vector<string>& classNames=vector<string>() );
317 virtual void detect( const Mat& image,
318 vector<ObjectDetection>& objectDetections,
319 float overlapThreshold=0.5f,
322 const vector<string>& getClassNames() const;
323 size_t getClassCount() const;
326 vector<CvLatentSvmDetector*> detectors;
327 vector<string> classNames;
330 CV_EXPORTS void groupRectangles(CV_OUT CV_IN_OUT vector<Rect>& rectList, int groupThreshold, double eps=0.2);
331 CV_EXPORTS_W void groupRectangles(CV_OUT CV_IN_OUT vector<Rect>& rectList, CV_OUT vector<int>& weights, int groupThreshold, double eps=0.2);
332 CV_EXPORTS void groupRectangles( vector<Rect>& rectList, int groupThreshold, double eps, vector<int>* weights, vector<double>* levelWeights );
333 CV_EXPORTS void groupRectangles(vector<Rect>& rectList, vector<int>& rejectLevels,
334 vector<double>& levelWeights, int groupThreshold, double eps=0.2);
335 CV_EXPORTS void groupRectangles_meanshift(vector<Rect>& rectList, vector<double>& foundWeights, vector<double>& foundScales,
336 double detectThreshold = 0.0, Size winDetSize = Size(64, 128));
339 class CV_EXPORTS FeatureEvaluator
342 enum { HAAR = 0, LBP = 1, HOG = 2 };
343 virtual ~FeatureEvaluator();
345 virtual bool read(const FileNode& node);
346 virtual Ptr<FeatureEvaluator> clone() const;
347 virtual int getFeatureType() const;
349 virtual bool setImage(const Mat& img, Size origWinSize);
350 virtual bool setWindow(Point p);
352 virtual double calcOrd(int featureIdx) const;
353 virtual int calcCat(int featureIdx) const;
355 static Ptr<FeatureEvaluator> create(int type);
358 template<> CV_EXPORTS void Ptr<CvHaarClassifierCascade>::delete_obj();
362 CASCADE_DO_CANNY_PRUNING=1,
363 CASCADE_SCALE_IMAGE=2,
364 CASCADE_FIND_BIGGEST_OBJECT=4,
365 CASCADE_DO_ROUGH_SEARCH=8
368 class CV_EXPORTS_W CascadeClassifier
371 CV_WRAP CascadeClassifier();
372 CV_WRAP CascadeClassifier( const string& filename );
373 virtual ~CascadeClassifier();
375 CV_WRAP virtual bool empty() const;
376 CV_WRAP bool load( const string& filename );
377 virtual bool read( const FileNode& node );
378 CV_WRAP virtual void detectMultiScale( const Mat& image,
379 CV_OUT vector<Rect>& objects,
380 double scaleFactor=1.1,
381 int minNeighbors=3, int flags=0,
383 Size maxSize=Size() );
385 CV_WRAP virtual void detectMultiScale( const Mat& image,
386 CV_OUT vector<Rect>& objects,
387 vector<int>& rejectLevels,
388 vector<double>& levelWeights,
389 double scaleFactor=1.1,
390 int minNeighbors=3, int flags=0,
393 bool outputRejectLevels=false );
396 bool isOldFormatCascade() const;
397 virtual Size getOriginalWindowSize() const;
398 int getFeatureType() const;
399 bool setImage( const Mat& );
402 //virtual bool detectSingleScale( const Mat& image, int stripCount, Size processingRectSize,
403 // int stripSize, int yStep, double factor, vector<Rect>& candidates );
405 virtual bool detectSingleScale( const Mat& image, int stripCount, Size processingRectSize,
406 int stripSize, int yStep, double factor, vector<Rect>& candidates,
407 vector<int>& rejectLevels, vector<double>& levelWeights, bool outputRejectLevels=false);
411 enum { DO_CANNY_PRUNING = 1, SCALE_IMAGE = 2,
412 FIND_BIGGEST_OBJECT = 4, DO_ROUGH_SEARCH = 8 };
414 friend class CascadeClassifierInvoker;
416 template<class FEval>
417 friend int predictOrdered( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);
419 template<class FEval>
420 friend int predictCategorical( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);
422 template<class FEval>
423 friend int predictOrderedStump( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);
425 template<class FEval>
426 friend int predictCategoricalStump( CascadeClassifier& cascade, Ptr<FeatureEvaluator> &featureEvaluator, double& weight);
428 bool setImage( Ptr<FeatureEvaluator>& feval, const Mat& image);
429 virtual int runAt( Ptr<FeatureEvaluator>& feval, Point pt, double& weight );
434 struct CV_EXPORTS DTreeNode
437 float threshold; // for ordered features only
442 struct CV_EXPORTS DTree
447 struct CV_EXPORTS Stage
454 bool read(const FileNode &node);
463 vector<Stage> stages;
464 vector<DTree> classifiers;
465 vector<DTreeNode> nodes;
466 vector<float> leaves;
471 Ptr<FeatureEvaluator> featureEvaluator;
472 Ptr<CvHaarClassifierCascade> oldCascade;
475 class CV_EXPORTS MaskGenerator
478 virtual ~MaskGenerator() {}
479 virtual cv::Mat generateMask(const cv::Mat& src)=0;
480 virtual void initializeMask(const cv::Mat& /*src*/) {};
482 void setMaskGenerator(Ptr<MaskGenerator> maskGenerator);
483 Ptr<MaskGenerator> getMaskGenerator();
485 void setFaceDetectionMaskGenerator();
488 Ptr<MaskGenerator> maskGenerator;
492 //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////
494 // struct for detection region of interest (ROI)
497 // scale(size) of the bounding box
499 // set of requrested locations to be evaluated
500 vector<cv::Point> locations;
501 // vector that will contain confidence values for each location
502 vector<double> confidences;
505 struct CV_EXPORTS_W HOGDescriptor
509 enum { DEFAULT_NLEVELS=64 };
511 CV_WRAP HOGDescriptor() : winSize(64,128), blockSize(16,16), blockStride(8,8),
512 cellSize(8,8), nbins(9), derivAperture(1), winSigma(-1),
513 histogramNormType(HOGDescriptor::L2Hys), L2HysThreshold(0.2), gammaCorrection(true),
514 nlevels(HOGDescriptor::DEFAULT_NLEVELS)
517 CV_WRAP HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride,
518 Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1,
519 int _histogramNormType=HOGDescriptor::L2Hys,
520 double _L2HysThreshold=0.2, bool _gammaCorrection=false,
521 int _nlevels=HOGDescriptor::DEFAULT_NLEVELS)
522 : winSize(_winSize), blockSize(_blockSize), blockStride(_blockStride), cellSize(_cellSize),
523 nbins(_nbins), derivAperture(_derivAperture), winSigma(_winSigma),
524 histogramNormType(_histogramNormType), L2HysThreshold(_L2HysThreshold),
525 gammaCorrection(_gammaCorrection), nlevels(_nlevels)
528 CV_WRAP HOGDescriptor(const String& filename)
533 HOGDescriptor(const HOGDescriptor& d)
538 virtual ~HOGDescriptor() {}
540 CV_WRAP size_t getDescriptorSize() const;
541 CV_WRAP bool checkDetectorSize() const;
542 CV_WRAP double getWinSigma() const;
544 CV_WRAP virtual void setSVMDetector(InputArray _svmdetector);
546 virtual bool read(FileNode& fn);
547 virtual void write(FileStorage& fs, const String& objname) const;
549 CV_WRAP virtual bool load(const String& filename, const String& objname=String());
550 CV_WRAP virtual void save(const String& filename, const String& objname=String()) const;
551 virtual void copyTo(HOGDescriptor& c) const;
553 CV_WRAP virtual void compute(const Mat& img,
554 CV_OUT vector<float>& descriptors,
555 Size winStride=Size(), Size padding=Size(),
556 const vector<Point>& locations=vector<Point>()) const;
557 //with found weights output
558 CV_WRAP virtual void detect(const Mat& img, CV_OUT vector<Point>& foundLocations,
559 CV_OUT vector<double>& weights,
560 double hitThreshold=0, Size winStride=Size(),
562 const vector<Point>& searchLocations=vector<Point>()) const;
563 //without found weights output
564 virtual void detect(const Mat& img, CV_OUT vector<Point>& foundLocations,
565 double hitThreshold=0, Size winStride=Size(),
567 const vector<Point>& searchLocations=vector<Point>()) const;
568 //with result weights output
569 CV_WRAP virtual void detectMultiScale(const Mat& img, CV_OUT vector<Rect>& foundLocations,
570 CV_OUT vector<double>& foundWeights, double hitThreshold=0,
571 Size winStride=Size(), Size padding=Size(), double scale=1.05,
572 double finalThreshold=2.0,bool useMeanshiftGrouping = false) const;
573 //without found weights output
574 virtual void detectMultiScale(const Mat& img, CV_OUT vector<Rect>& foundLocations,
575 double hitThreshold=0, Size winStride=Size(),
576 Size padding=Size(), double scale=1.05,
577 double finalThreshold=2.0, bool useMeanshiftGrouping = false) const;
579 CV_WRAP virtual void computeGradient(const Mat& img, CV_OUT Mat& grad, CV_OUT Mat& angleOfs,
580 Size paddingTL=Size(), Size paddingBR=Size()) const;
582 CV_WRAP static vector<float> getDefaultPeopleDetector();
583 CV_WRAP static vector<float> getDaimlerPeopleDetector();
585 CV_PROP Size winSize;
586 CV_PROP Size blockSize;
587 CV_PROP Size blockStride;
588 CV_PROP Size cellSize;
590 CV_PROP int derivAperture;
591 CV_PROP double winSigma;
592 CV_PROP int histogramNormType;
593 CV_PROP double L2HysThreshold;
594 CV_PROP bool gammaCorrection;
595 CV_PROP vector<float> svmDetector;
599 // evaluate specified ROI and return confidence value for each location
600 void detectROI(const cv::Mat& img, const vector<cv::Point> &locations,
601 CV_OUT std::vector<cv::Point>& foundLocations, CV_OUT std::vector<double>& confidences,
602 double hitThreshold = 0, cv::Size winStride = Size(),
603 cv::Size padding = Size()) const;
605 // evaluate specified ROI and return confidence value for each location in multiple scales
606 void detectMultiScaleROI(const cv::Mat& img,
607 CV_OUT std::vector<cv::Rect>& foundLocations,
608 std::vector<DetectionROI>& locations,
609 double hitThreshold = 0,
610 int groupThreshold = 0) const;
612 // read/parse Dalal's alt model file
613 void readALTModel(std::string modelfile);
617 CV_EXPORTS_W void findDataMatrix(InputArray image,
618 CV_OUT vector<string>& codes,
619 OutputArray corners=noArray(),
620 OutputArrayOfArrays dmtx=noArray());
621 CV_EXPORTS_W void drawDataMatrixCodes(InputOutputArray image,
622 const vector<string>& codes,
626 /****************************************************************************************\
628 \****************************************************************************************/
630 struct CV_EXPORTS CvDataMatrixCode {
636 CV_EXPORTS std::deque<CvDataMatrixCode> cvFindDataMatrix(CvMat *im);
638 /****************************************************************************************\
640 \****************************************************************************************/
646 using cv::FileStorage;
649 using cv::OutputArrayOfArrays;
655 /// @todo Convert doxy comments to rst
658 * \brief Discriminant feature described by its location and label.
660 struct CV_EXPORTS Feature
664 int label; ///< Quantization
666 Feature() : x(0), y(0), label(0) {}
667 Feature(int x, int y, int label);
669 void read(const FileNode& fn);
670 void write(FileStorage& fs) const;
673 inline Feature::Feature(int _x, int _y, int _label) : x(_x), y(_y), label(_label) {}
675 struct CV_EXPORTS Template
680 std::vector<Feature> features;
682 void read(const FileNode& fn);
683 void write(FileStorage& fs) const;
687 * \brief Represents a modality operating over an image pyramid.
689 class QuantizedPyramid
692 // Virtual destructor
693 virtual ~QuantizedPyramid() {}
696 * \brief Compute quantized image at current pyramid level for online detection.
698 * \param[out] dst The destination 8-bit image. For each pixel at most one bit is set,
699 * representing its classification.
701 virtual void quantize(Mat& dst) const =0;
704 * \brief Extract most discriminant features at current pyramid level to form a new template.
706 * \param[out] templ The new template.
708 virtual bool extractTemplate(Template& templ) const =0;
711 * \brief Go to the next pyramid level.
713 * \todo Allow pyramid scale factor other than 2
715 virtual void pyrDown() =0;
718 /// Candidate feature with a score
721 Candidate(int x, int y, int label, float score);
723 /// Sort candidates with high score to the front
724 bool operator<(const Candidate& rhs) const
726 return score > rhs.score;
734 * \brief Choose candidate features so that they are not bunched together.
736 * \param[in] candidates Candidate features sorted by score.
737 * \param[out] features Destination vector of selected features.
738 * \param[in] num_features Number of candidates to select.
739 * \param[in] distance Hint for desired distance between features.
741 static void selectScatteredFeatures(const std::vector<Candidate>& candidates,
742 std::vector<Feature>& features,
743 size_t num_features, float distance);
746 inline QuantizedPyramid::Candidate::Candidate(int x, int y, int label, float _score) : f(x, y, label), score(_score) {}
749 * \brief Interface for modalities that plug into the LINE template matching representation.
751 * \todo Max response, to allow optimization of summing (255/MAX) features as uint8
753 class CV_EXPORTS Modality
756 // Virtual destructor
757 virtual ~Modality() {}
760 * \brief Form a quantized image pyramid from a source image.
762 * \param[in] src The source image. Type depends on the modality.
763 * \param[in] mask Optional mask. If not empty, unmasked pixels are set to zero
764 * in quantized image and cannot be extracted as features.
766 Ptr<QuantizedPyramid> process(const Mat& src,
767 const Mat& mask = Mat()) const
769 return processImpl(src, mask);
772 virtual std::string name() const =0;
774 virtual void read(const FileNode& fn) =0;
775 virtual void write(FileStorage& fs) const =0;
778 * \brief Create modality by name.
780 * The following modality types are supported:
784 static Ptr<Modality> create(const std::string& modality_type);
787 * \brief Load a modality from file.
789 static Ptr<Modality> create(const FileNode& fn);
792 // Indirection is because process() has a default parameter.
793 virtual Ptr<QuantizedPyramid> processImpl(const Mat& src,
794 const Mat& mask) const =0;
798 * \brief Modality that computes quantized gradient orientations from a color image.
800 class CV_EXPORTS ColorGradient : public Modality
804 * \brief Default constructor. Uses reasonable default parameter values.
809 * \brief Constructor.
811 * \param weak_threshold When quantizing, discard gradients with magnitude less than this.
812 * \param num_features How many features a template must contain.
813 * \param strong_threshold Consider as candidate features only gradients whose norms are
816 ColorGradient(float weak_threshold, size_t num_features, float strong_threshold);
818 virtual std::string name() const;
820 virtual void read(const FileNode& fn);
821 virtual void write(FileStorage& fs) const;
823 float weak_threshold;
825 float strong_threshold;
828 virtual Ptr<QuantizedPyramid> processImpl(const Mat& src,
829 const Mat& mask) const;
833 * \brief Modality that computes quantized surface normals from a dense depth map.
835 class CV_EXPORTS DepthNormal : public Modality
839 * \brief Default constructor. Uses reasonable default parameter values.
844 * \brief Constructor.
846 * \param distance_threshold Ignore pixels beyond this distance.
847 * \param difference_threshold When computing normals, ignore contributions of pixels whose
848 * depth difference with the central pixel is above this threshold.
849 * \param num_features How many features a template must contain.
850 * \param extract_threshold Consider as candidate feature only if there are no differing
851 * orientations within a distance of extract_threshold.
853 DepthNormal(int distance_threshold, int difference_threshold, size_t num_features,
854 int extract_threshold);
856 virtual std::string name() const;
858 virtual void read(const FileNode& fn);
859 virtual void write(FileStorage& fs) const;
861 int distance_threshold;
862 int difference_threshold;
864 int extract_threshold;
867 virtual Ptr<QuantizedPyramid> processImpl(const Mat& src,
868 const Mat& mask) const;
872 * \brief Debug function to colormap a quantized image for viewing.
874 void colormap(const Mat& quantized, Mat& dst);
877 * \brief Represents a successful template match.
879 struct CV_EXPORTS Match
885 Match(int x, int y, float similarity, const std::string& class_id, int template_id);
887 /// Sort matches with high similarity to the front
888 bool operator<(const Match& rhs) const
890 // Secondarily sort on template_id for the sake of duplicate removal
891 if (similarity != rhs.similarity)
892 return similarity > rhs.similarity;
894 return template_id < rhs.template_id;
897 bool operator==(const Match& rhs) const
899 return x == rhs.x && y == rhs.y && similarity == rhs.similarity && class_id == rhs.class_id;
905 std::string class_id;
909 inline Match::Match(int _x, int _y, float _similarity, const std::string& _class_id, int _template_id)
910 : x(_x), y(_y), similarity(_similarity), class_id(_class_id), template_id(_template_id)
915 * \brief Object detector using the LINE template matching algorithm with any set of
918 class CV_EXPORTS Detector
922 * \brief Empty constructor, initialize with read().
927 * \brief Constructor.
929 * \param modalities Modalities to use (color gradients, depth normals, ...).
930 * \param T_pyramid Value of the sampling step T at each pyramid level. The
931 * number of pyramid levels is T_pyramid.size().
933 Detector(const std::vector< Ptr<Modality> >& modalities, const std::vector<int>& T_pyramid);
936 * \brief Detect objects by template matching.
938 * Matches globally at the lowest pyramid level, then refines locally stepping up the pyramid.
940 * \param sources Source images, one for each modality.
941 * \param threshold Similarity threshold, a percentage between 0 and 100.
942 * \param[out] matches Template matches, sorted by similarity score.
943 * \param class_ids If non-empty, only search for the desired object classes.
944 * \param[out] quantized_images Optionally return vector<Mat> of quantized images.
945 * \param masks The masks for consideration during matching. The masks should be CV_8UC1
946 * where 255 represents a valid pixel. If non-empty, the vector must be
947 * the same size as sources. Each element must be
948 * empty or the same size as its corresponding source.
950 void match(const std::vector<Mat>& sources, float threshold, std::vector<Match>& matches,
951 const std::vector<std::string>& class_ids = std::vector<std::string>(),
952 OutputArrayOfArrays quantized_images = noArray(),
953 const std::vector<Mat>& masks = std::vector<Mat>()) const;
956 * \brief Add new object template.
958 * \param sources Source images, one for each modality.
959 * \param class_id Object class ID.
960 * \param object_mask Mask separating object from background.
961 * \param[out] bounding_box Optionally return bounding box of the extracted features.
963 * \return Template ID, or -1 if failed to extract a valid template.
965 int addTemplate(const std::vector<Mat>& sources, const std::string& class_id,
966 const Mat& object_mask, Rect* bounding_box = NULL);
969 * \brief Add a new object template computed by external means.
971 int addSyntheticTemplate(const std::vector<Template>& templates, const std::string& class_id);
974 * \brief Get the modalities used by this detector.
976 * You are not permitted to add/remove modalities, but you may dynamic_cast them to
979 const std::vector< Ptr<Modality> >& getModalities() const { return modalities; }
982 * \brief Get sampling step T at pyramid_level.
984 int getT(int pyramid_level) const { return T_at_level[pyramid_level]; }
987 * \brief Get number of pyramid levels used by this detector.
989 int pyramidLevels() const { return pyramid_levels; }
992 * \brief Get the template pyramid identified by template_id.
994 * For example, with 2 modalities (Gradient, Normal) and two pyramid levels
995 * (L0, L1), the order is (GradientL0, NormalL0, GradientL1, NormalL1).
997 const std::vector<Template>& getTemplates(const std::string& class_id, int template_id) const;
999 int numTemplates() const;
1000 int numTemplates(const std::string& class_id) const;
1001 int numClasses() const { return static_cast<int>(class_templates.size()); }
1003 std::vector<std::string> classIds() const;
1005 void read(const FileNode& fn);
1006 void write(FileStorage& fs) const;
1008 std::string readClass(const FileNode& fn, const std::string &class_id_override = "");
1009 void writeClass(const std::string& class_id, FileStorage& fs) const;
1011 void readClasses(const std::vector<std::string>& class_ids,
1012 const std::string& format = "templates_%s.yml.gz");
1013 void writeClasses(const std::string& format = "templates_%s.yml.gz") const;
1016 std::vector< Ptr<Modality> > modalities;
1018 std::vector<int> T_at_level;
1020 typedef std::vector<Template> TemplatePyramid;
1021 typedef std::map<std::string, std::vector<TemplatePyramid> > TemplatesMap;
1022 TemplatesMap class_templates;
1024 typedef std::vector<Mat> LinearMemories;
1025 // Indexed as [pyramid level][modality][quantized label]
1026 typedef std::vector< std::vector<LinearMemories> > LinearMemoryPyramid;
1028 void matchClass(const LinearMemoryPyramid& lm_pyramid,
1029 const std::vector<Size>& sizes,
1030 float threshold, std::vector<Match>& matches,
1031 const std::string& class_id,
1032 const std::vector<TemplatePyramid>& template_pyramids) const;
1036 * \brief Factory function for detector using LINE algorithm with color gradients.
1038 * Default parameter settings suitable for VGA images.
1040 CV_EXPORTS Ptr<Detector> getDefaultLINE();
1043 * \brief Factory function for detector using LINE-MOD algorithm with color gradients
1044 * and depth normals.
1046 * Default parameter settings suitable for VGA images.
1048 CV_EXPORTS Ptr<Detector> getDefaultLINEMOD();
1050 } // namespace linemod