objdetect.hpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. #ifndef OPENCV_OBJDETECT_HPP
  44. #define OPENCV_OBJDETECT_HPP
  45. #include "opencv2/core.hpp"
  46. /**
  47. @defgroup objdetect Object Detection
  48. Haar Feature-based Cascade Classifier for Object Detection
  49. ----------------------------------------------------------
  50. The object detector described below has been initially proposed by Paul Viola @cite Viola01 and
  51. improved by Rainer Lienhart @cite Lienhart02 .
  52. First, a classifier (namely a *cascade of boosted classifiers working with haar-like features*) is
  53. trained with a few hundred sample views of a particular object (i.e., a face or a car), called
  54. positive examples, that are scaled to the same size (say, 20x20), and negative examples - arbitrary
  55. images of the same size.
  56. After a classifier is trained, it can be applied to a region of interest (of the same size as used
  57. during the training) in an input image. The classifier outputs a "1" if the region is likely to show
  58. the object (i.e., face/car), and "0" otherwise. To search for the object in the whole image one can
  59. move the search window across the image and check every location using the classifier. The
  60. classifier is designed so that it can be easily "resized" in order to be able to find the objects of
  61. interest at different sizes, which is more efficient than resizing the image itself. So, to find an
  62. object of an unknown size in the image the scan procedure should be done several times at different
  63. scales.
  64. The word "cascade" in the classifier name means that the resultant classifier consists of several
  65. simpler classifiers (*stages*) that are applied subsequently to a region of interest until at some
  66. stage the candidate is rejected or all the stages are passed. The word "boosted" means that the
  67. classifiers at every stage of the cascade are complex themselves and they are built out of basic
  68. classifiers using one of four different boosting techniques (weighted voting). Currently Discrete
  69. Adaboost, Real Adaboost, Gentle Adaboost and Logitboost are supported. The basic classifiers are
  70. decision-tree classifiers with at least 2 leaves. Haar-like features are the input to the basic
  71. classifiers, and are calculated as described below. The current algorithm uses the following
  72. Haar-like features:
  73. ![image](pics/haarfeatures.png)
  74. The feature used in a particular classifier is specified by its shape (1a, 2b etc.), position within
  75. the region of interest and the scale (this scale is not the same as the scale used at the detection
  76. stage, though these two scales are multiplied). For example, in the case of the third line feature
  77. (2c) the response is calculated as the difference between the sum of image pixels under the
  78. rectangle covering the whole feature (including the two white stripes and the black stripe in the
  79. middle) and the sum of the image pixels under the black stripe multiplied by 3 in order to
  80. compensate for the differences in the size of areas. The sums of pixel values over a rectangular
  81. regions are calculated rapidly using integral images (see below and the integral description).
  82. To see the object detector at work, have a look at the facedetect demo:
  83. <https://github.com/opencv/opencv/tree/master/samples/cpp/dbt_face_detection.cpp>
  84. The following reference is for the detection part only. There is a separate application called
  85. opencv_traincascade that can train a cascade of boosted classifiers from a set of samples.
  86. @note In the new C++ interface it is also possible to use LBP (local binary pattern) features in
  87. addition to Haar-like features. .. [Viola01] Paul Viola and Michael J. Jones. Rapid Object Detection
  88. using a Boosted Cascade of Simple Features. IEEE CVPR, 2001. The paper is available online at
  89. <http://research.microsoft.com/en-us/um/people/viola/Pubs/Detect/violaJones_CVPR2001.pdf>
  90. @{
  91. @defgroup objdetect_c C API
  92. @}
  93. */
  94. typedef struct CvHaarClassifierCascade CvHaarClassifierCascade;
  95. namespace cv
  96. {
  97. //! @addtogroup objdetect
  98. //! @{
  99. ///////////////////////////// Object Detection ////////////////////////////
  100. //! class for grouping object candidates, detected by Cascade Classifier, HOG etc.
  101. //! instance of the class is to be passed to cv::partition (see cxoperations.hpp)
  102. class CV_EXPORTS SimilarRects
  103. {
  104. public:
  105. SimilarRects(double _eps) : eps(_eps) {}
  106. inline bool operator()(const Rect& r1, const Rect& r2) const
  107. {
  108. double delta = eps * ((std::min)(r1.width, r2.width) + (std::min)(r1.height, r2.height)) * 0.5;
  109. return std::abs(r1.x - r2.x) <= delta &&
  110. std::abs(r1.y - r2.y) <= delta &&
  111. std::abs(r1.x + r1.width - r2.x - r2.width) <= delta &&
  112. std::abs(r1.y + r1.height - r2.y - r2.height) <= delta;
  113. }
  114. double eps;
  115. };
  116. /** @brief Groups the object candidate rectangles.
  117. @param rectList Input/output vector of rectangles. Output vector includes retained and grouped
  118. rectangles. (The Python list is not modified in place.)
  119. @param groupThreshold Minimum possible number of rectangles minus 1. The threshold is used in a
  120. group of rectangles to retain it.
  121. @param eps Relative difference between sides of the rectangles to merge them into a group.
  122. The function is a wrapper for the generic function partition . It clusters all the input rectangles
  123. using the rectangle equivalence criteria that combines rectangles with similar sizes and similar
  124. locations. The similarity is defined by eps. When eps=0 , no clustering is done at all. If
  125. \f$\texttt{eps}\rightarrow +\inf\f$ , all the rectangles are put in one cluster. Then, the small
  126. clusters containing less than or equal to groupThreshold rectangles are rejected. In each other
  127. cluster, the average rectangle is computed and put into the output rectangle list.
  128. */
  129. CV_EXPORTS void groupRectangles(std::vector<Rect>& rectList, int groupThreshold, double eps = 0.2);
  130. /** @overload */
  131. CV_EXPORTS_W void groupRectangles(CV_IN_OUT std::vector<Rect>& rectList, CV_OUT std::vector<int>& weights,
  132. int groupThreshold, double eps = 0.2);
  133. /** @overload */
  134. CV_EXPORTS void groupRectangles(std::vector<Rect>& rectList, int groupThreshold,
  135. double eps, std::vector<int>* weights, std::vector<double>* levelWeights );
  136. /** @overload */
  137. CV_EXPORTS void groupRectangles(std::vector<Rect>& rectList, std::vector<int>& rejectLevels,
  138. std::vector<double>& levelWeights, int groupThreshold, double eps = 0.2);
  139. /** @overload */
  140. CV_EXPORTS void groupRectangles_meanshift(std::vector<Rect>& rectList, std::vector<double>& foundWeights,
  141. std::vector<double>& foundScales,
  142. double detectThreshold = 0.0, Size winDetSize = Size(64, 128));
  143. template<> CV_EXPORTS void DefaultDeleter<CvHaarClassifierCascade>::operator ()(CvHaarClassifierCascade* obj) const;
  144. enum { CASCADE_DO_CANNY_PRUNING = 1,
  145. CASCADE_SCALE_IMAGE = 2,
  146. CASCADE_FIND_BIGGEST_OBJECT = 4,
  147. CASCADE_DO_ROUGH_SEARCH = 8
  148. };
  149. class CV_EXPORTS_W BaseCascadeClassifier : public Algorithm
  150. {
  151. public:
  152. virtual ~BaseCascadeClassifier();
  153. virtual bool empty() const = 0;
  154. virtual bool load( const String& filename ) = 0;
  155. virtual void detectMultiScale( InputArray image,
  156. CV_OUT std::vector<Rect>& objects,
  157. double scaleFactor,
  158. int minNeighbors, int flags,
  159. Size minSize, Size maxSize ) = 0;
  160. virtual void detectMultiScale( InputArray image,
  161. CV_OUT std::vector<Rect>& objects,
  162. CV_OUT std::vector<int>& numDetections,
  163. double scaleFactor,
  164. int minNeighbors, int flags,
  165. Size minSize, Size maxSize ) = 0;
  166. virtual void detectMultiScale( InputArray image,
  167. CV_OUT std::vector<Rect>& objects,
  168. CV_OUT std::vector<int>& rejectLevels,
  169. CV_OUT std::vector<double>& levelWeights,
  170. double scaleFactor,
  171. int minNeighbors, int flags,
  172. Size minSize, Size maxSize,
  173. bool outputRejectLevels ) = 0;
  174. virtual bool isOldFormatCascade() const = 0;
  175. virtual Size getOriginalWindowSize() const = 0;
  176. virtual int getFeatureType() const = 0;
  177. virtual void* getOldCascade() = 0;
  178. class CV_EXPORTS MaskGenerator
  179. {
  180. public:
  181. virtual ~MaskGenerator() {}
  182. virtual Mat generateMask(const Mat& src)=0;
  183. virtual void initializeMask(const Mat& /*src*/) { }
  184. };
  185. virtual void setMaskGenerator(const Ptr<MaskGenerator>& maskGenerator) = 0;
  186. virtual Ptr<MaskGenerator> getMaskGenerator() = 0;
  187. };
  188. /** @example facedetect.cpp
  189. This program demonstrates usage of the Cascade classifier class
  190. \image html Cascade_Classifier_Tutorial_Result_Haar.jpg "Sample screenshot" width=321 height=254
  191. */
  192. /** @brief Cascade classifier class for object detection.
  193. */
  194. class CV_EXPORTS_W CascadeClassifier
  195. {
  196. public:
  197. CV_WRAP CascadeClassifier();
  198. /** @brief Loads a classifier from a file.
  199. @param filename Name of the file from which the classifier is loaded.
  200. */
  201. CV_WRAP CascadeClassifier(const String& filename);
  202. ~CascadeClassifier();
  203. /** @brief Checks whether the classifier has been loaded.
  204. */
  205. CV_WRAP bool empty() const;
  206. /** @brief Loads a classifier from a file.
  207. @param filename Name of the file from which the classifier is loaded. The file may contain an old
  208. HAAR classifier trained by the haartraining application or a new cascade classifier trained by the
  209. traincascade application.
  210. */
  211. CV_WRAP bool load( const String& filename );
  212. /** @brief Reads a classifier from a FileStorage node.
  213. @note The file may contain a new cascade classifier (trained traincascade application) only.
  214. */
  215. CV_WRAP bool read( const FileNode& node );
  216. /** @brief Detects objects of different sizes in the input image. The detected objects are returned as a list
  217. of rectangles.
  218. @param image Matrix of the type CV_8U containing an image where objects are detected.
  219. @param objects Vector of rectangles where each rectangle contains the detected object, the
  220. rectangles may be partially outside the original image.
  221. @param scaleFactor Parameter specifying how much the image size is reduced at each image scale.
  222. @param minNeighbors Parameter specifying how many neighbors each candidate rectangle should have
  223. to retain it.
  224. @param flags Parameter with the same meaning for an old cascade as in the function
  225. cvHaarDetectObjects. It is not used for a new cascade.
  226. @param minSize Minimum possible object size. Objects smaller than that are ignored.
  227. @param maxSize Maximum possible object size. Objects larger than that are ignored. If `maxSize == minSize` model is evaluated on single scale.
  228. The function is parallelized with the TBB library.
  229. @note
  230. - (Python) A face detection example using cascade classifiers can be found at
  231. opencv_source_code/samples/python/facedetect.py
  232. */
  233. CV_WRAP void detectMultiScale( InputArray image,
  234. CV_OUT std::vector<Rect>& objects,
  235. double scaleFactor = 1.1,
  236. int minNeighbors = 3, int flags = 0,
  237. Size minSize = Size(),
  238. Size maxSize = Size() );
  239. /** @overload
  240. @param image Matrix of the type CV_8U containing an image where objects are detected.
  241. @param objects Vector of rectangles where each rectangle contains the detected object, the
  242. rectangles may be partially outside the original image.
  243. @param numDetections Vector of detection numbers for the corresponding objects. An object's number
  244. of detections is the number of neighboring positively classified rectangles that were joined
  245. together to form the object.
  246. @param scaleFactor Parameter specifying how much the image size is reduced at each image scale.
  247. @param minNeighbors Parameter specifying how many neighbors each candidate rectangle should have
  248. to retain it.
  249. @param flags Parameter with the same meaning for an old cascade as in the function
  250. cvHaarDetectObjects. It is not used for a new cascade.
  251. @param minSize Minimum possible object size. Objects smaller than that are ignored.
  252. @param maxSize Maximum possible object size. Objects larger than that are ignored. If `maxSize == minSize` model is evaluated on single scale.
  253. */
  254. CV_WRAP_AS(detectMultiScale2) void detectMultiScale( InputArray image,
  255. CV_OUT std::vector<Rect>& objects,
  256. CV_OUT std::vector<int>& numDetections,
  257. double scaleFactor=1.1,
  258. int minNeighbors=3, int flags=0,
  259. Size minSize=Size(),
  260. Size maxSize=Size() );
  261. /** @overload
  262. This function allows you to retrieve the final stage decision certainty of classification.
  263. For this, one needs to set `outputRejectLevels` on true and provide the `rejectLevels` and `levelWeights` parameter.
  264. For each resulting detection, `levelWeights` will then contain the certainty of classification at the final stage.
  265. This value can then be used to separate strong from weaker classifications.
  266. A code sample on how to use it efficiently can be found below:
  267. @code
  268. Mat img;
  269. vector<double> weights;
  270. vector<int> levels;
  271. vector<Rect> detections;
  272. CascadeClassifier model("/path/to/your/model.xml");
  273. model.detectMultiScale(img, detections, levels, weights, 1.1, 3, 0, Size(), Size(), true);
  274. cerr << "Detection " << detections[0] << " with weight " << weights[0] << endl;
  275. @endcode
  276. */
  277. CV_WRAP_AS(detectMultiScale3) void detectMultiScale( InputArray image,
  278. CV_OUT std::vector<Rect>& objects,
  279. CV_OUT std::vector<int>& rejectLevels,
  280. CV_OUT std::vector<double>& levelWeights,
  281. double scaleFactor = 1.1,
  282. int minNeighbors = 3, int flags = 0,
  283. Size minSize = Size(),
  284. Size maxSize = Size(),
  285. bool outputRejectLevels = false );
  286. CV_WRAP bool isOldFormatCascade() const;
  287. CV_WRAP Size getOriginalWindowSize() const;
  288. CV_WRAP int getFeatureType() const;
  289. void* getOldCascade();
  290. CV_WRAP static bool convert(const String& oldcascade, const String& newcascade);
  291. void setMaskGenerator(const Ptr<BaseCascadeClassifier::MaskGenerator>& maskGenerator);
  292. Ptr<BaseCascadeClassifier::MaskGenerator> getMaskGenerator();
  293. Ptr<BaseCascadeClassifier> cc;
  294. };
  295. CV_EXPORTS Ptr<BaseCascadeClassifier::MaskGenerator> createFaceDetectionMaskGenerator();
  296. //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////
  297. //! struct for detection region of interest (ROI)
  298. struct DetectionROI
  299. {
  300. //! scale(size) of the bounding box
  301. double scale;
  302. //! set of requested locations to be evaluated
  303. std::vector<cv::Point> locations;
  304. //! vector that will contain confidence values for each location
  305. std::vector<double> confidences;
  306. };
  307. /**@brief Implementation of HOG (Histogram of Oriented Gradients) descriptor and object detector.
  308. the HOG descriptor algorithm introduced by Navneet Dalal and Bill Triggs @cite Dalal2005 .
  309. useful links:
  310. https://hal.inria.fr/inria-00548512/document/
  311. https://en.wikipedia.org/wiki/Histogram_of_oriented_gradients
  312. https://software.intel.com/en-us/ipp-dev-reference-histogram-of-oriented-gradients-hog-descriptor
  313. http://www.learnopencv.com/histogram-of-oriented-gradients
  314. http://www.learnopencv.com/handwritten-digits-classification-an-opencv-c-python-tutorial
  315. */
  316. struct CV_EXPORTS_W HOGDescriptor
  317. {
  318. public:
  319. enum { L2Hys = 0 //!< Default histogramNormType
  320. };
  321. enum { DEFAULT_NLEVELS = 64 //!< Default nlevels value.
  322. };
  323. /**@brief Creates the HOG descriptor and detector with default params.
  324. aqual to HOGDescriptor(Size(64,128), Size(16,16), Size(8,8), Size(8,8), 9, 1 )
  325. */
  326. CV_WRAP HOGDescriptor() : winSize(64,128), blockSize(16,16), blockStride(8,8),
  327. cellSize(8,8), nbins(9), derivAperture(1), winSigma(-1),
  328. histogramNormType(HOGDescriptor::L2Hys), L2HysThreshold(0.2), gammaCorrection(true),
  329. free_coef(-1.f), nlevels(HOGDescriptor::DEFAULT_NLEVELS), signedGradient(false)
  330. {}
  331. /** @overload
  332. @param _winSize sets winSize with given value.
  333. @param _blockSize sets blockSize with given value.
  334. @param _blockStride sets blockStride with given value.
  335. @param _cellSize sets cellSize with given value.
  336. @param _nbins sets nbins with given value.
  337. @param _derivAperture sets derivAperture with given value.
  338. @param _winSigma sets winSigma with given value.
  339. @param _histogramNormType sets histogramNormType with given value.
  340. @param _L2HysThreshold sets L2HysThreshold with given value.
  341. @param _gammaCorrection sets gammaCorrection with given value.
  342. @param _nlevels sets nlevels with given value.
  343. @param _signedGradient sets signedGradient with given value.
  344. */
  345. CV_WRAP HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride,
  346. Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1,
  347. int _histogramNormType=HOGDescriptor::L2Hys,
  348. double _L2HysThreshold=0.2, bool _gammaCorrection=false,
  349. int _nlevels=HOGDescriptor::DEFAULT_NLEVELS, bool _signedGradient=false)
  350. : winSize(_winSize), blockSize(_blockSize), blockStride(_blockStride), cellSize(_cellSize),
  351. nbins(_nbins), derivAperture(_derivAperture), winSigma(_winSigma),
  352. histogramNormType(_histogramNormType), L2HysThreshold(_L2HysThreshold),
  353. gammaCorrection(_gammaCorrection), free_coef(-1.f), nlevels(_nlevels), signedGradient(_signedGradient)
  354. {}
  355. /** @overload
  356. @param filename the file name containing HOGDescriptor properties and coefficients of the trained classifier
  357. */
  358. CV_WRAP HOGDescriptor(const String& filename)
  359. {
  360. load(filename);
  361. }
  362. /** @overload
  363. @param d the HOGDescriptor which cloned to create a new one.
  364. */
  365. HOGDescriptor(const HOGDescriptor& d)
  366. {
  367. d.copyTo(*this);
  368. }
  369. /**@brief Default destructor.
  370. */
  371. virtual ~HOGDescriptor() {}
  372. /**@brief Returns the number of coefficients required for the classification.
  373. */
  374. CV_WRAP size_t getDescriptorSize() const;
  375. /** @brief Checks if detector size equal to descriptor size.
  376. */
  377. CV_WRAP bool checkDetectorSize() const;
  378. /** @brief Returns winSigma value
  379. */
  380. CV_WRAP double getWinSigma() const;
  381. /**@example peopledetect.cpp
  382. */
  383. /**@brief Sets coefficients for the linear SVM classifier.
  384. @param _svmdetector coefficients for the linear SVM classifier.
  385. */
  386. CV_WRAP virtual void setSVMDetector(InputArray _svmdetector);
  387. /** @brief Reads HOGDescriptor parameters from a file node.
  388. @param fn File node
  389. */
  390. virtual bool read(FileNode& fn);
  391. /** @brief Stores HOGDescriptor parameters in a file storage.
  392. @param fs File storage
  393. @param objname Object name
  394. */
  395. virtual void write(FileStorage& fs, const String& objname) const;
  396. /** @brief loads coefficients for the linear SVM classifier from a file
  397. @param filename Name of the file to read.
  398. @param objname The optional name of the node to read (if empty, the first top-level node will be used).
  399. */
  400. CV_WRAP virtual bool load(const String& filename, const String& objname = String());
  401. /** @brief saves coefficients for the linear SVM classifier to a file
  402. @param filename File name
  403. @param objname Object name
  404. */
  405. CV_WRAP virtual void save(const String& filename, const String& objname = String()) const;
  406. /** @brief clones the HOGDescriptor
  407. @param c cloned HOGDescriptor
  408. */
  409. virtual void copyTo(HOGDescriptor& c) const;
  410. /**@example train_HOG.cpp
  411. */
  412. /** @brief Computes HOG descriptors of given image.
  413. @param img Matrix of the type CV_8U containing an image where HOG features will be calculated.
  414. @param descriptors Matrix of the type CV_32F
  415. @param winStride Window stride. It must be a multiple of block stride.
  416. @param padding Padding
  417. @param locations Vector of Point
  418. */
  419. CV_WRAP virtual void compute(InputArray img,
  420. CV_OUT std::vector<float>& descriptors,
  421. Size winStride = Size(), Size padding = Size(),
  422. const std::vector<Point>& locations = std::vector<Point>()) const;
  423. /** @brief Performs object detection without a multi-scale window.
  424. @param img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
  425. @param foundLocations Vector of point where each point contains left-top corner point of detected object boundaries.
  426. @param weights Vector that will contain confidence values for each detected object.
  427. @param hitThreshold Threshold for the distance between features and SVM classifying plane.
  428. Usually it is 0 and should be specified in the detector coefficients (as the last free coefficient).
  429. But if the free coefficient is omitted (which is allowed), you can specify it manually here.
  430. @param winStride Window stride. It must be a multiple of block stride.
  431. @param padding Padding
  432. @param searchLocations Vector of Point includes set of requested locations to be evaluated.
  433. */
  434. CV_WRAP virtual void detect(const Mat& img, CV_OUT std::vector<Point>& foundLocations,
  435. CV_OUT std::vector<double>& weights,
  436. double hitThreshold = 0, Size winStride = Size(),
  437. Size padding = Size(),
  438. const std::vector<Point>& searchLocations = std::vector<Point>()) const;
  439. /** @brief Performs object detection without a multi-scale window.
  440. @param img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
  441. @param foundLocations Vector of point where each point contains left-top corner point of detected object boundaries.
  442. @param hitThreshold Threshold for the distance between features and SVM classifying plane.
  443. Usually it is 0 and should be specified in the detector coefficients (as the last free coefficient).
  444. But if the free coefficient is omitted (which is allowed), you can specify it manually here.
  445. @param winStride Window stride. It must be a multiple of block stride.
  446. @param padding Padding
  447. @param searchLocations Vector of Point includes locations to search.
  448. */
  449. virtual void detect(const Mat& img, CV_OUT std::vector<Point>& foundLocations,
  450. double hitThreshold = 0, Size winStride = Size(),
  451. Size padding = Size(),
  452. const std::vector<Point>& searchLocations=std::vector<Point>()) const;
  453. /** @brief Detects objects of different sizes in the input image. The detected objects are returned as a list
  454. of rectangles.
  455. @param img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
  456. @param foundLocations Vector of rectangles where each rectangle contains the detected object.
  457. @param foundWeights Vector that will contain confidence values for each detected object.
  458. @param hitThreshold Threshold for the distance between features and SVM classifying plane.
  459. Usually it is 0 and should be specified in the detector coefficients (as the last free coefficient).
  460. But if the free coefficient is omitted (which is allowed), you can specify it manually here.
  461. @param winStride Window stride. It must be a multiple of block stride.
  462. @param padding Padding
  463. @param scale Coefficient of the detection window increase.
  464. @param finalThreshold Final threshold
  465. @param useMeanshiftGrouping indicates grouping algorithm
  466. */
  467. CV_WRAP virtual void detectMultiScale(InputArray img, CV_OUT std::vector<Rect>& foundLocations,
  468. CV_OUT std::vector<double>& foundWeights, double hitThreshold = 0,
  469. Size winStride = Size(), Size padding = Size(), double scale = 1.05,
  470. double finalThreshold = 2.0,bool useMeanshiftGrouping = false) const;
  471. /** @brief Detects objects of different sizes in the input image. The detected objects are returned as a list
  472. of rectangles.
  473. @param img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
  474. @param foundLocations Vector of rectangles where each rectangle contains the detected object.
  475. @param hitThreshold Threshold for the distance between features and SVM classifying plane.
  476. Usually it is 0 and should be specified in the detector coefficients (as the last free coefficient).
  477. But if the free coefficient is omitted (which is allowed), you can specify it manually here.
  478. @param winStride Window stride. It must be a multiple of block stride.
  479. @param padding Padding
  480. @param scale Coefficient of the detection window increase.
  481. @param finalThreshold Final threshold
  482. @param useMeanshiftGrouping indicates grouping algorithm
  483. */
  484. virtual void detectMultiScale(InputArray img, CV_OUT std::vector<Rect>& foundLocations,
  485. double hitThreshold = 0, Size winStride = Size(),
  486. Size padding = Size(), double scale = 1.05,
  487. double finalThreshold = 2.0, bool useMeanshiftGrouping = false) const;
  488. /** @brief Computes gradients and quantized gradient orientations.
  489. @param img Matrix contains the image to be computed
  490. @param grad Matrix of type CV_32FC2 contains computed gradients
  491. @param angleOfs Matrix of type CV_8UC2 contains quantized gradient orientations
  492. @param paddingTL Padding from top-left
  493. @param paddingBR Padding from bottom-right
  494. */
  495. CV_WRAP virtual void computeGradient(const Mat& img, CV_OUT Mat& grad, CV_OUT Mat& angleOfs,
  496. Size paddingTL = Size(), Size paddingBR = Size()) const;
  497. /** @brief Returns coefficients of the classifier trained for people detection (for 64x128 windows).
  498. */
  499. CV_WRAP static std::vector<float> getDefaultPeopleDetector();
  500. /**@example hog.cpp
  501. */
  502. /** @brief Returns coefficients of the classifier trained for people detection (for 48x96 windows).
  503. */
  504. CV_WRAP static std::vector<float> getDaimlerPeopleDetector();
  505. //! Detection window size. Align to block size and block stride. Default value is Size(64,128).
  506. CV_PROP Size winSize;
  507. //! Block size in pixels. Align to cell size. Default value is Size(16,16).
  508. CV_PROP Size blockSize;
  509. //! Block stride. It must be a multiple of cell size. Default value is Size(8,8).
  510. CV_PROP Size blockStride;
  511. //! Cell size. Default value is Size(8,8).
  512. CV_PROP Size cellSize;
  513. //! Number of bins used in the calculation of histogram of gradients. Default value is 9.
  514. CV_PROP int nbins;
  515. //! not documented
  516. CV_PROP int derivAperture;
  517. //! Gaussian smoothing window parameter.
  518. CV_PROP double winSigma;
  519. //! histogramNormType
  520. CV_PROP int histogramNormType;
  521. //! L2-Hys normalization method shrinkage.
  522. CV_PROP double L2HysThreshold;
  523. //! Flag to specify whether the gamma correction preprocessing is required or not.
  524. CV_PROP bool gammaCorrection;
  525. //! coefficients for the linear SVM classifier.
  526. CV_PROP std::vector<float> svmDetector;
  527. //! coefficients for the linear SVM classifier used when OpenCL is enabled
  528. UMat oclSvmDetector;
  529. //! not documented
  530. float free_coef;
  531. //! Maximum number of detection window increases. Default value is 64
  532. CV_PROP int nlevels;
  533. //! Indicates signed gradient will be used or not
  534. CV_PROP bool signedGradient;
  535. /** @brief evaluate specified ROI and return confidence value for each location
  536. @param img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
  537. @param locations Vector of Point
  538. @param foundLocations Vector of Point where each Point is detected object's top-left point.
  539. @param confidences confidences
  540. @param hitThreshold Threshold for the distance between features and SVM classifying plane. Usually
  541. it is 0 and should be specified in the detector coefficients (as the last free coefficient). But if
  542. the free coefficient is omitted (which is allowed), you can specify it manually here
  543. @param winStride winStride
  544. @param padding padding
  545. */
  546. virtual void detectROI(const cv::Mat& img, const std::vector<cv::Point> &locations,
  547. CV_OUT std::vector<cv::Point>& foundLocations, CV_OUT std::vector<double>& confidences,
  548. double hitThreshold = 0, cv::Size winStride = Size(),
  549. cv::Size padding = Size()) const;
  550. /** @brief evaluate specified ROI and return confidence value for each location in multiple scales
  551. @param img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
  552. @param foundLocations Vector of rectangles where each rectangle contains the detected object.
  553. @param locations Vector of DetectionROI
  554. @param hitThreshold Threshold for the distance between features and SVM classifying plane. Usually it is 0 and should be specified
  555. 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.
  556. @param groupThreshold Minimum possible number of rectangles minus 1. The threshold is used in a group of rectangles to retain it.
  557. */
  558. virtual void detectMultiScaleROI(const cv::Mat& img,
  559. CV_OUT std::vector<cv::Rect>& foundLocations,
  560. std::vector<DetectionROI>& locations,
  561. double hitThreshold = 0,
  562. int groupThreshold = 0) const;
  563. /** @brief read/parse Dalal's alt model file
  564. @param modelfile Path of Dalal's alt model file.
  565. */
  566. void readALTModel(String modelfile);
  567. /** @brief Groups the object candidate rectangles.
  568. @param rectList Input/output vector of rectangles. Output vector includes retained and grouped rectangles. (The Python list is not modified in place.)
  569. @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.)
  570. @param groupThreshold Minimum possible number of rectangles minus 1. The threshold is used in a group of rectangles to retain it.
  571. @param eps Relative difference between sides of the rectangles to merge them into a group.
  572. */
  573. void groupRectangles(std::vector<cv::Rect>& rectList, std::vector<double>& weights, int groupThreshold, double eps) const;
  574. };
  575. //! @} objdetect
  576. }
  577. #include "opencv2/objdetect/detection_based_tracker.hpp"
  578. #ifndef DISABLE_OPENCV_24_COMPATIBILITY
  579. #include "opencv2/objdetect/objdetect_c.h"
  580. #endif
  581. #endif