Coverage Report

Created: 2023-06-07 08:11

/work/install-coverage/include/opencv4/opencv2/objdetect.hpp
Line
Count
Source (jump to first uncovered line)
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
#include "opencv2/objdetect/aruco_detector.hpp"
49
50
/**
51
@defgroup objdetect Object Detection
52
53
@{
54
    @defgroup objdetect_cascade_classifier Cascade Classifier for Object Detection
55
56
The object detector described below has been initially proposed by Paul Viola @cite Viola01 and
57
improved by Rainer Lienhart @cite Lienhart02 .
58
59
First, a classifier (namely a *cascade of boosted classifiers working with haar-like features*) is
60
trained with a few hundred sample views of a particular object (i.e., a face or a car), called
61
positive examples, that are scaled to the same size (say, 20x20), and negative examples - arbitrary
62
images of the same size.
63
64
After a classifier is trained, it can be applied to a region of interest (of the same size as used
65
during the training) in an input image. The classifier outputs a "1" if the region is likely to show
66
the object (i.e., face/car), and "0" otherwise. To search for the object in the whole image one can
67
move the search window across the image and check every location using the classifier. The
68
classifier is designed so that it can be easily "resized" in order to be able to find the objects of
69
interest at different sizes, which is more efficient than resizing the image itself. So, to find an
70
object of an unknown size in the image the scan procedure should be done several times at different
71
scales.
72
73
The word "cascade" in the classifier name means that the resultant classifier consists of several
74
simpler classifiers (*stages*) that are applied subsequently to a region of interest until at some
75
stage the candidate is rejected or all the stages are passed. The word "boosted" means that the
76
classifiers at every stage of the cascade are complex themselves and they are built out of basic
77
classifiers using one of four different boosting techniques (weighted voting). Currently Discrete
78
Adaboost, Real Adaboost, Gentle Adaboost and Logitboost are supported. The basic classifiers are
79
decision-tree classifiers with at least 2 leaves. Haar-like features are the input to the basic
80
classifiers, and are calculated as described below. The current algorithm uses the following
81
Haar-like features:
82
83
![image](pics/haarfeatures.png)
84
85
The feature used in a particular classifier is specified by its shape (1a, 2b etc.), position within
86
the region of interest and the scale (this scale is not the same as the scale used at the detection
87
stage, though these two scales are multiplied). For example, in the case of the third line feature
88
(2c) the response is calculated as the difference between the sum of image pixels under the
89
rectangle covering the whole feature (including the two white stripes and the black stripe in the
90
middle) and the sum of the image pixels under the black stripe multiplied by 3 in order to
91
compensate for the differences in the size of areas. The sums of pixel values over a rectangular
92
regions are calculated rapidly using integral images (see below and the integral description).
93
94
Check @ref tutorial_cascade_classifier "the corresponding tutorial" for more details.
95
96
The following reference is for the detection part only. There is a separate application called
97
opencv_traincascade that can train a cascade of boosted classifiers from a set of samples.
98
99
@note In the new C++ interface it is also possible to use LBP (local binary pattern) features in
100
addition to Haar-like features. .. [Viola01] Paul Viola and Michael J. Jones. Rapid Object Detection
101
using a Boosted Cascade of Simple Features. IEEE CVPR, 2001. The paper is available online at
102
<https://github.com/SvHey/thesis/blob/master/Literature/ObjectDetection/violaJones_CVPR2001.pdf>
103
104
    @defgroup objdetect_hog HOG (Histogram of Oriented Gradients) descriptor and object detector
105
    @defgroup objdetect_qrcode QRCode detection and encoding
106
    @defgroup objdetect_dnn_face DNN-based face detection and recognition
107
Check @ref tutorial_dnn_face "the corresponding tutorial" for more details.
108
    @defgroup objdetect_common Common functions and classes
109
    @defgroup objdetect_aruco ArUco markers and boards detection for robust camera pose estimation
110
    @{
111
        ArUco Marker Detection
112
        Square fiducial markers (also known as Augmented Reality Markers) are useful for easy,
113
        fast and robust camera pose estimation.
114
115
        The main functionality of ArucoDetector class is detection of markers in an image. If the markers are grouped
116
        as a board, then you can try to recover the missing markers with ArucoDetector::refineDetectedMarkers().
117
        ArUco markers can also be used for advanced chessboard corner finding. To do this, group the markers in the
118
        CharucoBoard and find the corners of the chessboard with the CharucoDetector::detectBoard().
119
120
        The implementation is based on the ArUco Library by R. Muñoz-Salinas and S. Garrido-Jurado @cite Aruco2014.
121
122
        Markers can also be detected based on the AprilTag 2 @cite wang2016iros fiducial detection method.
123
124
        @sa @cite Aruco2014
125
        This code has been originally developed by Sergio Garrido-Jurado as a project
126
        for Google Summer of Code 2015 (GSoC 15).
127
    @}
128
129
@}
130
 */
131
132
typedef struct CvHaarClassifierCascade CvHaarClassifierCascade;
133
134
namespace cv
135
{
136
137
//! @addtogroup objdetect_common
138
//! @{
139
140
///////////////////////////// Object Detection ////////////////////////////
141
142
/** @brief This class is used for grouping object candidates detected by Cascade Classifier, HOG etc.
143
144
instance of the class is to be passed to cv::partition
145
 */
146
class CV_EXPORTS SimilarRects
147
{
148
public:
149
0
    SimilarRects(double _eps) : eps(_eps) {}
150
    inline bool operator()(const Rect& r1, const Rect& r2) const
151
0
    {
152
0
        double delta = eps * ((std::min)(r1.width, r2.width) + (std::min)(r1.height, r2.height)) * 0.5;
153
0
        return std::abs(r1.x - r2.x) <= delta &&
154
0
            std::abs(r1.y - r2.y) <= delta &&
155
0
            std::abs(r1.x + r1.width - r2.x - r2.width) <= delta &&
156
0
            std::abs(r1.y + r1.height - r2.y - r2.height) <= delta;
157
0
    }
158
    double eps;
159
};
160
161
/** @brief Groups the object candidate rectangles.
162
163
@param rectList Input/output vector of rectangles. Output vector includes retained and grouped
164
rectangles. (The Python list is not modified in place.)
165
@param groupThreshold Minimum possible number of rectangles minus 1. The threshold is used in a
166
group of rectangles to retain it.
167
@param eps Relative difference between sides of the rectangles to merge them into a group.
168
169
The function is a wrapper for the generic function partition . It clusters all the input rectangles
170
using the rectangle equivalence criteria that combines rectangles with similar sizes and similar
171
locations. The similarity is defined by eps. When eps=0 , no clustering is done at all. If
172
\f$\texttt{eps}\rightarrow +\inf\f$ , all the rectangles are put in one cluster. Then, the small
173
clusters containing less than or equal to groupThreshold rectangles are rejected. In each other
174
cluster, the average rectangle is computed and put into the output rectangle list.
175
 */
176
CV_EXPORTS   void groupRectangles(std::vector<Rect>& rectList, int groupThreshold, double eps = 0.2);
177
/** @overload */
178
CV_EXPORTS_W void groupRectangles(CV_IN_OUT std::vector<Rect>& rectList, CV_OUT std::vector<int>& weights,
179
                                  int groupThreshold, double eps = 0.2);
180
/** @overload */
181
CV_EXPORTS   void groupRectangles(std::vector<Rect>& rectList, int groupThreshold,
182
                                  double eps, std::vector<int>* weights, std::vector<double>* levelWeights );
183
/** @overload */
184
CV_EXPORTS   void groupRectangles(std::vector<Rect>& rectList, std::vector<int>& rejectLevels,
185
                                  std::vector<double>& levelWeights, int groupThreshold, double eps = 0.2);
186
/** @overload */
187
CV_EXPORTS   void groupRectangles_meanshift(std::vector<Rect>& rectList, std::vector<double>& foundWeights,
188
                                            std::vector<double>& foundScales,
189
                                            double detectThreshold = 0.0, Size winDetSize = Size(64, 128));
190
//! @}
191
192
//! @addtogroup objdetect_cascade_classifier
193
//! @{
194
195
template<> struct DefaultDeleter<CvHaarClassifierCascade>{ CV_EXPORTS void operator ()(CvHaarClassifierCascade* obj) const; };
196
197
enum { CASCADE_DO_CANNY_PRUNING    = 1,
198
       CASCADE_SCALE_IMAGE         = 2,
199
       CASCADE_FIND_BIGGEST_OBJECT = 4,
200
       CASCADE_DO_ROUGH_SEARCH     = 8
201
     };
202
203
class CV_EXPORTS_W BaseCascadeClassifier : public Algorithm
204
{
205
public:
206
    virtual ~BaseCascadeClassifier();
207
    virtual bool empty() const CV_OVERRIDE = 0;
208
    virtual bool load( const String& filename ) = 0;
209
    virtual void detectMultiScale( InputArray image,
210
                           CV_OUT std::vector<Rect>& objects,
211
                           double scaleFactor,
212
                           int minNeighbors, int flags,
213
                           Size minSize, Size maxSize ) = 0;
214
215
    virtual void detectMultiScale( InputArray image,
216
                           CV_OUT std::vector<Rect>& objects,
217
                           CV_OUT std::vector<int>& numDetections,
218
                           double scaleFactor,
219
                           int minNeighbors, int flags,
220
                           Size minSize, Size maxSize ) = 0;
221
222
    virtual void detectMultiScale( InputArray image,
223
                                   CV_OUT std::vector<Rect>& objects,
224
                                   CV_OUT std::vector<int>& rejectLevels,
225
                                   CV_OUT std::vector<double>& levelWeights,
226
                                   double scaleFactor,
227
                                   int minNeighbors, int flags,
228
                                   Size minSize, Size maxSize,
229
                                   bool outputRejectLevels ) = 0;
230
231
    virtual bool isOldFormatCascade() const = 0;
232
    virtual Size getOriginalWindowSize() const = 0;
233
    virtual int getFeatureType() const = 0;
234
    virtual void* getOldCascade() = 0;
235
236
    class CV_EXPORTS MaskGenerator
237
    {
238
    public:
239
0
        virtual ~MaskGenerator() {}
240
        virtual Mat generateMask(const Mat& src)=0;
241
0
        virtual void initializeMask(const Mat& /*src*/) { }
242
    };
243
    virtual void setMaskGenerator(const Ptr<MaskGenerator>& maskGenerator) = 0;
244
    virtual Ptr<MaskGenerator> getMaskGenerator() = 0;
245
};
246
247
/** @example samples/cpp/facedetect.cpp
248
This program demonstrates usage of the Cascade classifier class
249
\image html Cascade_Classifier_Tutorial_Result_Haar.jpg "Sample screenshot" width=321 height=254
250
*/
251
/** @brief Cascade classifier class for object detection.
252
 */
253
class CV_EXPORTS_W CascadeClassifier
254
{
255
public:
256
    CV_WRAP CascadeClassifier();
257
    /** @brief Loads a classifier from a file.
258
259
    @param filename Name of the file from which the classifier is loaded.
260
     */
261
    CV_WRAP CascadeClassifier(const String& filename);
262
    ~CascadeClassifier();
263
    /** @brief Checks whether the classifier has been loaded.
264
    */
265
    CV_WRAP bool empty() const;
266
    /** @brief Loads a classifier from a file.
267
268
    @param filename Name of the file from which the classifier is loaded. The file may contain an old
269
    HAAR classifier trained by the haartraining application or a new cascade classifier trained by the
270
    traincascade application.
271
     */
272
    CV_WRAP bool load( const String& filename );
273
    /** @brief Reads a classifier from a FileStorage node.
274
275
    @note The file may contain a new cascade classifier (trained by the traincascade application) only.
276
     */
277
    CV_WRAP bool read( const FileNode& node );
278
279
    /** @brief Detects objects of different sizes in the input image. The detected objects are returned as a list
280
    of rectangles.
281
282
    @param image Matrix of the type CV_8U containing an image where objects are detected.
283
    @param objects Vector of rectangles where each rectangle contains the detected object, the
284
    rectangles may be partially outside the original image.
285
    @param scaleFactor Parameter specifying how much the image size is reduced at each image scale.
286
    @param minNeighbors Parameter specifying how many neighbors each candidate rectangle should have
287
    to retain it.
288
    @param flags Parameter with the same meaning for an old cascade as in the function
289
    cvHaarDetectObjects. It is not used for a new cascade.
290
    @param minSize Minimum possible object size. Objects smaller than that are ignored.
291
    @param maxSize Maximum possible object size. Objects larger than that are ignored. If `maxSize == minSize` model is evaluated on single scale.
292
    */
293
    CV_WRAP void detectMultiScale( InputArray image,
294
                          CV_OUT std::vector<Rect>& objects,
295
                          double scaleFactor = 1.1,
296
                          int minNeighbors = 3, int flags = 0,
297
                          Size minSize = Size(),
298
                          Size maxSize = Size() );
299
300
    /** @overload
301
    @param image Matrix of the type CV_8U containing an image where objects are detected.
302
    @param objects Vector of rectangles where each rectangle contains the detected object, the
303
    rectangles may be partially outside the original image.
304
    @param numDetections Vector of detection numbers for the corresponding objects. An object's number
305
    of detections is the number of neighboring positively classified rectangles that were joined
306
    together to form the object.
307
    @param scaleFactor Parameter specifying how much the image size is reduced at each image scale.
308
    @param minNeighbors Parameter specifying how many neighbors each candidate rectangle should have
309
    to retain it.
310
    @param flags Parameter with the same meaning for an old cascade as in the function
311
    cvHaarDetectObjects. It is not used for a new cascade.
312
    @param minSize Minimum possible object size. Objects smaller than that are ignored.
313
    @param maxSize Maximum possible object size. Objects larger than that are ignored. If `maxSize == minSize` model is evaluated on single scale.
314
    */
315
    CV_WRAP_AS(detectMultiScale2) void detectMultiScale( InputArray image,
316
                          CV_OUT std::vector<Rect>& objects,
317
                          CV_OUT std::vector<int>& numDetections,
318
                          double scaleFactor=1.1,
319
                          int minNeighbors=3, int flags=0,
320
                          Size minSize=Size(),
321
                          Size maxSize=Size() );
322
323
    /** @overload
324
    This function allows you to retrieve the final stage decision certainty of classification.
325
    For this, one needs to set `outputRejectLevels` on true and provide the `rejectLevels` and `levelWeights` parameter.
326
    For each resulting detection, `levelWeights` will then contain the certainty of classification at the final stage.
327
    This value can then be used to separate strong from weaker classifications.
328
329
    A code sample on how to use it efficiently can be found below:
330
    @code
331
    Mat img;
332
    vector<double> weights;
333
    vector<int> levels;
334
    vector<Rect> detections;
335
    CascadeClassifier model("/path/to/your/model.xml");
336
    model.detectMultiScale(img, detections, levels, weights, 1.1, 3, 0, Size(), Size(), true);
337
    cerr << "Detection " << detections[0] << " with weight " << weights[0] << endl;
338
    @endcode
339
    */
340
    CV_WRAP_AS(detectMultiScale3) void detectMultiScale( InputArray image,
341
                                  CV_OUT std::vector<Rect>& objects,
342
                                  CV_OUT std::vector<int>& rejectLevels,
343
                                  CV_OUT std::vector<double>& levelWeights,
344
                                  double scaleFactor = 1.1,
345
                                  int minNeighbors = 3, int flags = 0,
346
                                  Size minSize = Size(),
347
                                  Size maxSize = Size(),
348
                                  bool outputRejectLevels = false );
349
350
    CV_WRAP bool isOldFormatCascade() const;
351
    CV_WRAP Size getOriginalWindowSize() const;
352
    CV_WRAP int getFeatureType() const;
353
    void* getOldCascade();
354
355
    CV_WRAP static bool convert(const String& oldcascade, const String& newcascade);
356
357
    void setMaskGenerator(const Ptr<BaseCascadeClassifier::MaskGenerator>& maskGenerator);
358
    Ptr<BaseCascadeClassifier::MaskGenerator> getMaskGenerator();
359
360
    Ptr<BaseCascadeClassifier> cc;
361
};
362
363
CV_EXPORTS Ptr<BaseCascadeClassifier::MaskGenerator> createFaceDetectionMaskGenerator();
364
//! @}
365
366
//! @addtogroup objdetect_hog
367
//! @{
368
//////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////
369
370
//! struct for detection region of interest (ROI)
371
struct DetectionROI
372
{
373
   //! scale(size) of the bounding box
374
   double scale;
375
   //! set of requested locations to be evaluated
376
   std::vector<cv::Point> locations;
377
   //! vector that will contain confidence values for each location
378
   std::vector<double> confidences;
379
};
380
381
/**@brief Implementation of HOG (Histogram of Oriented Gradients) descriptor and object detector.
382
383
the HOG descriptor algorithm introduced by Navneet Dalal and Bill Triggs @cite Dalal2005 .
384
385
useful links:
386
387
https://hal.inria.fr/inria-00548512/document/
388
389
https://en.wikipedia.org/wiki/Histogram_of_oriented_gradients
390
391
https://software.intel.com/en-us/ipp-dev-reference-histogram-of-oriented-gradients-hog-descriptor
392
393
http://www.learnopencv.com/histogram-of-oriented-gradients
394
395
http://www.learnopencv.com/handwritten-digits-classification-an-opencv-c-python-tutorial
396
397
 */
398
struct CV_EXPORTS_W HOGDescriptor
399
{
400
public:
401
    enum HistogramNormType { L2Hys = 0 //!< Default histogramNormType
402
         };
403
    enum { DEFAULT_NLEVELS = 64 //!< Default nlevels value.
404
         };
405
    enum DescriptorStorageFormat { DESCR_FORMAT_COL_BY_COL, DESCR_FORMAT_ROW_BY_ROW };
406
407
    /**@brief Creates the HOG descriptor and detector with default parameters.
408
409
    aqual to HOGDescriptor(Size(64,128), Size(16,16), Size(8,8), Size(8,8), 9 )
410
    */
411
    CV_WRAP HOGDescriptor() : winSize(64,128), blockSize(16,16), blockStride(8,8),
412
        cellSize(8,8), nbins(9), derivAperture(1), winSigma(-1),
413
        histogramNormType(HOGDescriptor::L2Hys), L2HysThreshold(0.2), gammaCorrection(true),
414
        free_coef(-1.f), nlevels(HOGDescriptor::DEFAULT_NLEVELS), signedGradient(false)
415
0
    {}
416
417
    /** @overload
418
    @param _winSize sets winSize with given value.
419
    @param _blockSize sets blockSize with given value.
420
    @param _blockStride sets blockStride with given value.
421
    @param _cellSize sets cellSize with given value.
422
    @param _nbins sets nbins with given value.
423
    @param _derivAperture sets derivAperture with given value.
424
    @param _winSigma sets winSigma with given value.
425
    @param _histogramNormType sets histogramNormType with given value.
426
    @param _L2HysThreshold sets L2HysThreshold with given value.
427
    @param _gammaCorrection sets gammaCorrection with given value.
428
    @param _nlevels sets nlevels with given value.
429
    @param _signedGradient sets signedGradient with given value.
430
    */
431
    CV_WRAP HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride,
432
                  Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1,
433
                  HOGDescriptor::HistogramNormType _histogramNormType=HOGDescriptor::L2Hys,
434
                  double _L2HysThreshold=0.2, bool _gammaCorrection=false,
435
                  int _nlevels=HOGDescriptor::DEFAULT_NLEVELS, bool _signedGradient=false)
436
    : winSize(_winSize), blockSize(_blockSize), blockStride(_blockStride), cellSize(_cellSize),
437
    nbins(_nbins), derivAperture(_derivAperture), winSigma(_winSigma),
438
    histogramNormType(_histogramNormType), L2HysThreshold(_L2HysThreshold),
439
    gammaCorrection(_gammaCorrection), free_coef(-1.f), nlevels(_nlevels), signedGradient(_signedGradient)
440
0
    {}
441
442
    /** @overload
443
444
    Creates the HOG descriptor and detector and loads HOGDescriptor parameters and coefficients for the linear SVM classifier from a file.
445
    @param filename The file name containing HOGDescriptor properties and coefficients for the linear SVM classifier.
446
    */
447
    CV_WRAP HOGDescriptor(const String& filename)
448
0
    {
449
0
        load(filename);
450
0
    }
451
452
    /** @overload
453
    @param d the HOGDescriptor which cloned to create a new one.
454
    */
455
    HOGDescriptor(const HOGDescriptor& d)
456
0
    {
457
0
        d.copyTo(*this);
458
0
    }
459
460
    /**@brief Default destructor.
461
    */
462
0
    virtual ~HOGDescriptor() {}
463
464
    /**@brief Returns the number of coefficients required for the classification.
465
    */
466
    CV_WRAP size_t getDescriptorSize() const;
467
468
    /** @brief Checks if detector size equal to descriptor size.
469
    */
470
    CV_WRAP bool checkDetectorSize() const;
471
472
    /** @brief Returns winSigma value
473
    */
474
    CV_WRAP double getWinSigma() const;
475
476
    /**@example samples/cpp/peopledetect.cpp
477
    */
478
    /**@brief Sets coefficients for the linear SVM classifier.
479
    @param svmdetector coefficients for the linear SVM classifier.
480
    */
481
    CV_WRAP virtual void setSVMDetector(InputArray svmdetector);
482
483
    /** @brief Reads HOGDescriptor parameters and coefficients for the linear SVM classifier from a file node.
484
    @param fn File node
485
    */
486
    virtual bool read(FileNode& fn);
487
488
    /** @brief Stores HOGDescriptor parameters and coefficients for the linear SVM classifier in a file storage.
489
    @param fs File storage
490
    @param objname Object name
491
    */
492
    virtual void write(FileStorage& fs, const String& objname) const;
493
494
    /** @brief loads HOGDescriptor parameters and coefficients for the linear SVM classifier from a file
495
    @param filename Name of the file to read.
496
    @param objname The optional name of the node to read (if empty, the first top-level node will be used).
497
    */
498
    CV_WRAP virtual bool load(const String& filename, const String& objname = String());
499
500
    /** @brief saves HOGDescriptor parameters and coefficients for the linear SVM classifier to a file
501
    @param filename File name
502
    @param objname Object name
503
    */
504
    CV_WRAP virtual void save(const String& filename, const String& objname = String()) const;
505
506
    /** @brief clones the HOGDescriptor
507
    @param c cloned HOGDescriptor
508
    */
509
    virtual void copyTo(HOGDescriptor& c) const;
510
511
    /**@example samples/cpp/train_HOG.cpp
512
    */
513
    /** @brief Computes HOG descriptors of given image.
514
    @param img Matrix of the type CV_8U containing an image where HOG features will be calculated.
515
    @param descriptors Matrix of the type CV_32F
516
    @param winStride Window stride. It must be a multiple of block stride.
517
    @param padding Padding
518
    @param locations Vector of Point
519
    */
520
    CV_WRAP virtual void compute(InputArray img,
521
                         CV_OUT std::vector<float>& descriptors,
522
                         Size winStride = Size(), Size padding = Size(),
523
                         const std::vector<Point>& locations = std::vector<Point>()) const;
524
525
    /** @brief Performs object detection without a multi-scale window.
526
    @param img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
527
    @param foundLocations Vector of point where each point contains left-top corner point of detected object boundaries.
528
    @param weights Vector that will contain confidence values for each detected object.
529
    @param hitThreshold Threshold for the distance between features and SVM classifying plane.
530
    Usually it is 0 and should be specified in the detector coefficients (as the last free coefficient).
531
    But if the free coefficient is omitted (which is allowed), you can specify it manually here.
532
    @param winStride Window stride. It must be a multiple of block stride.
533
    @param padding Padding
534
    @param searchLocations Vector of Point includes set of requested locations to be evaluated.
535
    */
536
    CV_WRAP virtual void detect(InputArray img, CV_OUT std::vector<Point>& foundLocations,
537
                        CV_OUT std::vector<double>& weights,
538
                        double hitThreshold = 0, Size winStride = Size(),
539
                        Size padding = Size(),
540
                        const std::vector<Point>& searchLocations = std::vector<Point>()) const;
541
542
    /** @brief Performs object detection without a multi-scale window.
543
    @param img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
544
    @param foundLocations Vector of point where each point contains left-top corner point of detected object boundaries.
545
    @param hitThreshold Threshold for the distance between features and SVM classifying plane.
546
    Usually it is 0 and should be specified in the detector coefficients (as the last free coefficient).
547
    But if the free coefficient is omitted (which is allowed), you can specify it manually here.
548
    @param winStride Window stride. It must be a multiple of block stride.
549
    @param padding Padding
550
    @param searchLocations Vector of Point includes locations to search.
551
    */
552
    virtual void detect(InputArray img, CV_OUT std::vector<Point>& foundLocations,
553
                        double hitThreshold = 0, Size winStride = Size(),
554
                        Size padding = Size(),
555
                        const std::vector<Point>& searchLocations=std::vector<Point>()) const;
556
557
    /** @brief Detects objects of different sizes in the input image. The detected objects are returned as a list
558
    of rectangles.
559
    @param img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
560
    @param foundLocations Vector of rectangles where each rectangle contains the detected object.
561
    @param foundWeights Vector that will contain confidence values for each detected object.
562
    @param hitThreshold Threshold for the distance between features and SVM classifying plane.
563
    Usually it is 0 and should be specified in the detector coefficients (as the last free coefficient).
564
    But if the free coefficient is omitted (which is allowed), you can specify it manually here.
565
    @param winStride Window stride. It must be a multiple of block stride.
566
    @param padding Padding
567
    @param scale Coefficient of the detection window increase.
568
    @param groupThreshold Coefficient to regulate the similarity threshold. When detected, some objects can be covered
569
    by many rectangles. 0 means not to perform grouping.
570
    @param useMeanshiftGrouping indicates grouping algorithm
571
    */
572
    CV_WRAP virtual void detectMultiScale(InputArray img, CV_OUT std::vector<Rect>& foundLocations,
573
                                  CV_OUT std::vector<double>& foundWeights, double hitThreshold = 0,
574
                                  Size winStride = Size(), Size padding = Size(), double scale = 1.05,
575
                                  double groupThreshold = 2.0, bool useMeanshiftGrouping = false) const;
576
577
    /** @brief Detects objects of different sizes in the input image. The detected objects are returned as a list
578
    of rectangles.
579
    @param img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
580
    @param foundLocations Vector of rectangles where each rectangle contains the detected object.
581
    @param hitThreshold Threshold for the distance between features and SVM classifying plane.
582
    Usually it is 0 and should be specified in the detector coefficients (as the last free coefficient).
583
    But if the free coefficient is omitted (which is allowed), you can specify it manually here.
584
    @param winStride Window stride. It must be a multiple of block stride.
585
    @param padding Padding
586
    @param scale Coefficient of the detection window increase.
587
    @param groupThreshold Coefficient to regulate the similarity threshold. When detected, some objects can be covered
588
    by many rectangles. 0 means not to perform grouping.
589
    @param useMeanshiftGrouping indicates grouping algorithm
590
    */
591
    virtual void detectMultiScale(InputArray img, CV_OUT std::vector<Rect>& foundLocations,
592
                                  double hitThreshold = 0, Size winStride = Size(),
593
                                  Size padding = Size(), double scale = 1.05,
594
                                  double groupThreshold = 2.0, bool useMeanshiftGrouping = false) const;
595
596
    /** @brief  Computes gradients and quantized gradient orientations.
597
    @param img Matrix contains the image to be computed
598
    @param grad Matrix of type CV_32FC2 contains computed gradients
599
    @param angleOfs Matrix of type CV_8UC2 contains quantized gradient orientations
600
    @param paddingTL Padding from top-left
601
    @param paddingBR Padding from bottom-right
602
    */
603
    CV_WRAP virtual void computeGradient(InputArray img, InputOutputArray grad, InputOutputArray angleOfs,
604
                                 Size paddingTL = Size(), Size paddingBR = Size()) const;
605
606
    /** @brief Returns coefficients of the classifier trained for people detection (for 64x128 windows).
607
    */
608
    CV_WRAP static std::vector<float> getDefaultPeopleDetector();
609
610
    /**@example samples/tapi/hog.cpp
611
    */
612
    /** @brief Returns coefficients of the classifier trained for people detection (for 48x96 windows).
613
    */
614
    CV_WRAP static std::vector<float> getDaimlerPeopleDetector();
615
616
    //! Detection window size. Align to block size and block stride. Default value is Size(64,128).
617
    CV_PROP Size winSize;
618
619
    //! Block size in pixels. Align to cell size. Default value is Size(16,16).
620
    CV_PROP Size blockSize;
621
622
    //! Block stride. It must be a multiple of cell size. Default value is Size(8,8).
623
    CV_PROP Size blockStride;
624
625
    //! Cell size. Default value is Size(8,8).
626
    CV_PROP Size cellSize;
627
628
    //! Number of bins used in the calculation of histogram of gradients. Default value is 9.
629
    CV_PROP int nbins;
630
631
    //! not documented
632
    CV_PROP int derivAperture;
633
634
    //! Gaussian smoothing window parameter.
635
    CV_PROP double winSigma;
636
637
    //! histogramNormType
638
    CV_PROP HOGDescriptor::HistogramNormType histogramNormType;
639
640
    //! L2-Hys normalization method shrinkage.
641
    CV_PROP double L2HysThreshold;
642
643
    //! Flag to specify whether the gamma correction preprocessing is required or not.
644
    CV_PROP bool gammaCorrection;
645
646
    //! coefficients for the linear SVM classifier.
647
    CV_PROP std::vector<float> svmDetector;
648
649
    //! coefficients for the linear SVM classifier used when OpenCL is enabled
650
    UMat oclSvmDetector;
651
652
    //! not documented
653
    float free_coef;
654
655
    //! Maximum number of detection window increases. Default value is 64
656
    CV_PROP int nlevels;
657
658
    //! Indicates signed gradient will be used or not
659
    CV_PROP bool signedGradient;
660
661
    /** @brief evaluate specified ROI and return confidence value for each location
662
    @param img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
663
    @param locations Vector of Point
664
    @param foundLocations Vector of Point where each Point is detected object's top-left point.
665
    @param confidences confidences
666
    @param hitThreshold Threshold for the distance between features and SVM classifying plane. Usually
667
    it is 0 and should be specified in the detector coefficients (as the last free coefficient). But if
668
    the free coefficient is omitted (which is allowed), you can specify it manually here
669
    @param winStride winStride
670
    @param padding padding
671
    */
672
    virtual void detectROI(InputArray img, const std::vector<cv::Point> &locations,
673
                                   CV_OUT std::vector<cv::Point>& foundLocations, CV_OUT std::vector<double>& confidences,
674
                                   double hitThreshold = 0, cv::Size winStride = Size(),
675
                                   cv::Size padding = Size()) const;
676
677
    /** @brief evaluate specified ROI and return confidence value for each location in multiple scales
678
    @param img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
679
    @param foundLocations Vector of rectangles where each rectangle contains the detected object.
680
    @param locations Vector of DetectionROI
681
    @param hitThreshold Threshold for the distance between features and SVM classifying plane. Usually it is 0 and should be specified
682
    in the detector coefficients (as the last free coefficient). But if the free coefficient is omitted (which is allowed), you can specify it manually here.
683
    @param groupThreshold Minimum possible number of rectangles minus 1. The threshold is used in a group of rectangles to retain it.
684
    */
685
    virtual void detectMultiScaleROI(InputArray img,
686
                                     CV_OUT std::vector<cv::Rect>& foundLocations,
687
                                     std::vector<DetectionROI>& locations,
688
                                     double hitThreshold = 0,
689
                                     int groupThreshold = 0) const;
690
691
    /** @brief Groups the object candidate rectangles.
692
    @param rectList  Input/output vector of rectangles. Output vector includes retained and grouped rectangles. (The Python list is not modified in place.)
693
    @param weights Input/output vector of weights of rectangles. Output vector includes weights of retained and grouped rectangles. (The Python list is not modified in place.)
694
    @param groupThreshold Minimum possible number of rectangles minus 1. The threshold is used in a group of rectangles to retain it.
695
    @param eps Relative difference between sides of the rectangles to merge them into a group.
696
    */
697
    void groupRectangles(std::vector<cv::Rect>& rectList, std::vector<double>& weights, int groupThreshold, double eps) const;
698
};
699
//! @}
700
701
//! @addtogroup objdetect_qrcode
702
//! @{
703
704
class CV_EXPORTS_W QRCodeEncoder {
705
protected:
706
    QRCodeEncoder();  // use ::create()
707
public:
708
    virtual ~QRCodeEncoder();
709
710
    enum EncodeMode {
711
        MODE_AUTO              = -1,
712
        MODE_NUMERIC           = 1, // 0b0001
713
        MODE_ALPHANUMERIC      = 2, // 0b0010
714
        MODE_BYTE              = 4, // 0b0100
715
        MODE_ECI               = 7, // 0b0111
716
        MODE_KANJI             = 8, // 0b1000
717
        MODE_STRUCTURED_APPEND = 3  // 0b0011
718
    };
719
720
    enum CorrectionLevel {
721
        CORRECT_LEVEL_L = 0,
722
        CORRECT_LEVEL_M = 1,
723
        CORRECT_LEVEL_Q = 2,
724
        CORRECT_LEVEL_H = 3
725
    };
726
727
    enum ECIEncodings {
728
        ECI_UTF8 = 26
729
    };
730
731
    /** @brief QR code encoder parameters.
732
     @param version The optional version of QR code (by default - maximum possible depending on
733
                    the length of the string).
734
     @param correction_level The optional level of error correction (by default - the lowest).
735
     @param mode The optional encoding mode - Numeric, Alphanumeric, Byte, Kanji, ECI or Structured Append.
736
     @param structure_number The optional number of QR codes to generate in Structured Append mode.
737
    */
738
    struct CV_EXPORTS_W_SIMPLE Params
739
    {
740
        CV_WRAP Params();
741
        CV_PROP_RW int version;
742
        CV_PROP_RW CorrectionLevel correction_level;
743
        CV_PROP_RW EncodeMode mode;
744
        CV_PROP_RW int structure_number;
745
    };
746
747
    /** @brief Constructor
748
    @param parameters QR code encoder parameters QRCodeEncoder::Params
749
    */
750
    static CV_WRAP
751
    Ptr<QRCodeEncoder> create(const QRCodeEncoder::Params& parameters = QRCodeEncoder::Params());
752
753
    /** @brief Generates QR code from input string.
754
     @param encoded_info Input string to encode.
755
     @param qrcode Generated QR code.
756
    */
757
    CV_WRAP virtual void encode(const String& encoded_info, OutputArray qrcode) = 0;
758
759
    /** @brief Generates QR code from input string in Structured Append mode. The encoded message is splitting over a number of QR codes.
760
     @param encoded_info Input string to encode.
761
     @param qrcodes Vector of generated QR codes.
762
    */
763
    CV_WRAP virtual void encodeStructuredAppend(const String& encoded_info, OutputArrayOfArrays qrcodes) = 0;
764
765
};
766
767
class CV_EXPORTS_W_SIMPLE QRCodeDetectorBase {
768
public:
769
    CV_DEPRECATED_EXTERNAL  // avoid using in C++ code, will be moved to "protected" (need to fix bindings first)
770
    QRCodeDetectorBase();
771
772
    QRCodeDetectorBase(const QRCodeDetectorBase&) = default;
773
    QRCodeDetectorBase(QRCodeDetectorBase&&) = default;
774
    QRCodeDetectorBase& operator=(const QRCodeDetectorBase&) = default;
775
    QRCodeDetectorBase& operator=(QRCodeDetectorBase&&) = default;
776
777
    /** @brief Detects QR code in image and returns the quadrangle containing the code.
778
     @param img grayscale or color (BGR) image containing (or not) QR code.
779
     @param points Output vector of vertices of the minimum-area quadrangle containing the code.
780
     */
781
    CV_WRAP bool detect(InputArray img, OutputArray points) const;
782
783
    /** @brief Decodes QR code in image once it's found by the detect() method.
784
785
     Returns UTF8-encoded output string or empty string if the code cannot be decoded.
786
     @param img grayscale or color (BGR) image containing QR code.
787
     @param points Quadrangle vertices found by detect() method (or some other algorithm).
788
     @param straight_qrcode The optional output image containing rectified and binarized QR code
789
     */
790
    CV_WRAP std::string decode(InputArray img, InputArray points, OutputArray straight_qrcode = noArray()) const;
791
792
    /** @brief Both detects and decodes QR code
793
794
     @param img grayscale or color (BGR) image containing QR code.
795
     @param points optional output array of vertices of the found QR code quadrangle. Will be empty if not found.
796
     @param straight_qrcode The optional output image containing rectified and binarized QR code
797
     */
798
    CV_WRAP std::string detectAndDecode(InputArray img, OutputArray points=noArray(),
799
                                        OutputArray straight_qrcode = noArray()) const;
800
801
802
    /** @brief Detects QR codes in image and returns the vector of the quadrangles containing the codes.
803
     @param img grayscale or color (BGR) image containing (or not) QR codes.
804
     @param points Output vector of vector of vertices of the minimum-area quadrangle containing the codes.
805
     */
806
    CV_WRAP
807
    bool detectMulti(InputArray img, OutputArray points) const;
808
809
    /** @brief Decodes QR codes in image once it's found by the detect() method.
810
     @param img grayscale or color (BGR) image containing QR codes.
811
     @param decoded_info UTF8-encoded output vector of string or empty vector of string if the codes cannot be decoded.
812
     @param points vector of Quadrangle vertices found by detect() method (or some other algorithm).
813
     @param straight_qrcode The optional output vector of images containing rectified and binarized QR codes
814
     */
815
    CV_WRAP
816
    bool decodeMulti(
817
            InputArray img, InputArray points,
818
            CV_OUT std::vector<std::string>& decoded_info,
819
            OutputArrayOfArrays straight_qrcode = noArray()
820
    ) const;
821
822
    /** @brief Both detects and decodes QR codes
823
    @param img grayscale or color (BGR) image containing QR codes.
824
    @param decoded_info UTF8-encoded output vector of string or empty vector of string if the codes cannot be decoded.
825
    @param points optional output vector of vertices of the found QR code quadrangles. Will be empty if not found.
826
    @param straight_qrcode The optional output vector of images containing rectified and binarized QR codes
827
    */
828
    CV_WRAP
829
    bool detectAndDecodeMulti(
830
            InputArray img, CV_OUT std::vector<std::string>& decoded_info,
831
            OutputArray points = noArray(),
832
            OutputArrayOfArrays straight_qrcode = noArray()
833
    ) const;
834
    struct Impl;
835
protected:
836
    Ptr<Impl> p;
837
};
838
839
class CV_EXPORTS_W_SIMPLE QRCodeDetector : public QRCodeDetectorBase
840
{
841
public:
842
    CV_WRAP QRCodeDetector();
843
844
    /** @brief sets the epsilon used during the horizontal scan of QR code stop marker detection.
845
     @param epsX Epsilon neighborhood, which allows you to determine the horizontal pattern
846
     of the scheme 1:1:3:1:1 according to QR code standard.
847
    */
848
    CV_WRAP QRCodeDetector& setEpsX(double epsX);
849
    /** @brief sets the epsilon used during the vertical scan of QR code stop marker detection.
850
     @param epsY Epsilon neighborhood, which allows you to determine the vertical pattern
851
     of the scheme 1:1:3:1:1 according to QR code standard.
852
     */
853
    CV_WRAP QRCodeDetector& setEpsY(double epsY);
854
855
    /** @brief use markers to improve the position of the corners of the QR code
856
     *
857
     * alignmentMarkers using by default
858
     */
859
    CV_WRAP QRCodeDetector& setUseAlignmentMarkers(bool useAlignmentMarkers);
860
861
    /** @brief Decodes QR code on a curved surface in image once it's found by the detect() method.
862
863
     Returns UTF8-encoded output string or empty string if the code cannot be decoded.
864
     @param img grayscale or color (BGR) image containing QR code.
865
     @param points Quadrangle vertices found by detect() method (or some other algorithm).
866
     @param straight_qrcode The optional output image containing rectified and binarized QR code
867
     */
868
    CV_WRAP cv::String decodeCurved(InputArray img, InputArray points, OutputArray straight_qrcode = noArray());
869
870
    /** @brief Both detects and decodes QR code on a curved surface
871
872
     @param img grayscale or color (BGR) image containing QR code.
873
     @param points optional output array of vertices of the found QR code quadrangle. Will be empty if not found.
874
     @param straight_qrcode The optional output image containing rectified and binarized QR code
875
     */
876
    CV_WRAP std::string detectAndDecodeCurved(InputArray img, OutputArray points=noArray(),
877
                                              OutputArray straight_qrcode = noArray());
878
};
879
880
class CV_EXPORTS_W_SIMPLE QRCodeDetectorAruco : public QRCodeDetectorBase {
881
public:
882
    CV_WRAP QRCodeDetectorAruco();
883
884
    struct CV_EXPORTS_W_SIMPLE Params {
885
        CV_WRAP Params();
886
887
        /** @brief The minimum allowed pixel size of a QR module in the smallest image in the image pyramid, default 4.f */
888
        CV_PROP_RW float minModuleSizeInPyramid;
889
890
        /** @brief The maximum allowed relative rotation for finder patterns in the same QR code, default pi/12 */
891
        CV_PROP_RW float maxRotation;
892
893
        /** @brief The maximum allowed relative mismatch in module sizes for finder patterns in the same QR code, default 1.75f */
894
        CV_PROP_RW float maxModuleSizeMismatch;
895
896
        /** @brief The maximum allowed module relative mismatch for timing pattern module, default 2.f
897
         *
898
         * If relative mismatch of timing pattern module more this value, penalty points will be added.
899
         * If a lot of penalty points are added, QR code will be rejected. */
900
        CV_PROP_RW float maxTimingPatternMismatch;
901
902
        /** @brief The maximum allowed percentage of penalty points out of total pins in timing pattern, default 0.4f */
903
        CV_PROP_RW float maxPenalties;
904
905
        /** @brief The maximum allowed relative color mismatch in the timing pattern, default 0.2f*/
906
        CV_PROP_RW float maxColorsMismatch;
907
908
        /** @brief The algorithm find QR codes with almost minimum timing pattern score and minimum size, default 0.9f
909
         *
910
         * The QR code with the minimum "timing pattern score" and minimum "size" is selected as the best QR code.
911
         * If for the current QR code "timing pattern score" * scaleTimingPatternScore < "previous timing pattern score" and "size" < "previous size", then
912
         * current QR code set as the best QR code. */
913
        CV_PROP_RW float scaleTimingPatternScore;
914
    };
915
916
    /** @brief QR code detector constructor for Aruco-based algorithm. See cv::QRCodeDetectorAruco::Params */
917
    CV_WRAP explicit QRCodeDetectorAruco(const QRCodeDetectorAruco::Params& params);
918
919
    /** @brief Detector parameters getter. See cv::QRCodeDetectorAruco::Params */
920
    CV_WRAP const QRCodeDetectorAruco::Params& getDetectorParameters() const;
921
922
    /** @brief Detector parameters setter. See cv::QRCodeDetectorAruco::Params */
923
    CV_WRAP QRCodeDetectorAruco& setDetectorParameters(const QRCodeDetectorAruco::Params& params);
924
925
    /** @brief Aruco detector parameters are used to search for the finder patterns. */
926
    CV_WRAP aruco::DetectorParameters getArucoParameters();
927
928
    /** @brief Aruco detector parameters are used to search for the finder patterns. */
929
    CV_WRAP void setArucoParameters(const aruco::DetectorParameters& params);
930
};
931
932
//! @}
933
}
934
935
#include "opencv2/objdetect/detection_based_tracker.hpp"
936
#include "opencv2/objdetect/face.hpp"
937
#include "opencv2/objdetect/charuco_detector.hpp"
938
939
#endif