cudaobjdetect.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #ifndef OPENCV_CUDAOBJDETECT_HPP
  43. #define OPENCV_CUDAOBJDETECT_HPP
  44. #ifndef __cplusplus
  45. # error cudaobjdetect.hpp header must be compiled as C++
  46. #endif
  47. #include "opencv2/core/cuda.hpp"
  48. /**
  49. @addtogroup cuda
  50. @{
  51. @defgroup cudaobjdetect Object Detection
  52. @}
  53. */
  54. namespace cv { namespace cuda {
  55. //! @addtogroup cudaobjdetect
  56. //! @{
  57. //
  58. // HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector
  59. //
  60. /** @brief The class implements Histogram of Oriented Gradients (@cite Dalal2005) object detector.
  61. @note
  62. - An example applying the HOG descriptor for people detection can be found at
  63. opencv_source_code/samples/cpp/peopledetect.cpp
  64. - A CUDA example applying the HOG descriptor for people detection can be found at
  65. opencv_source_code/samples/gpu/hog.cpp
  66. - (Python) An example applying the HOG descriptor for people detection can be found at
  67. opencv_source_code/samples/python/peopledetect.py
  68. */
  69. class CV_EXPORTS HOG : public Algorithm
  70. {
  71. public:
  72. enum
  73. {
  74. DESCR_FORMAT_ROW_BY_ROW,
  75. DESCR_FORMAT_COL_BY_COL
  76. };
  77. /** @brief Creates the HOG descriptor and detector.
  78. @param win_size Detection window size. Align to block size and block stride.
  79. @param block_size Block size in pixels. Align to cell size. Only (16,16) is supported for now.
  80. @param block_stride Block stride. It must be a multiple of cell size.
  81. @param cell_size Cell size. Only (8, 8) is supported for now.
  82. @param nbins Number of bins. Only 9 bins per cell are supported for now.
  83. */
  84. static Ptr<HOG> create(Size win_size = Size(64, 128),
  85. Size block_size = Size(16, 16),
  86. Size block_stride = Size(8, 8),
  87. Size cell_size = Size(8, 8),
  88. int nbins = 9);
  89. //! Gaussian smoothing window parameter.
  90. virtual void setWinSigma(double win_sigma) = 0;
  91. virtual double getWinSigma() const = 0;
  92. //! L2-Hys normalization method shrinkage.
  93. virtual void setL2HysThreshold(double threshold_L2hys) = 0;
  94. virtual double getL2HysThreshold() const = 0;
  95. //! Flag to specify whether the gamma correction preprocessing is required or not.
  96. virtual void setGammaCorrection(bool gamma_correction) = 0;
  97. virtual bool getGammaCorrection() const = 0;
  98. //! Maximum number of detection window increases.
  99. virtual void setNumLevels(int nlevels) = 0;
  100. virtual int getNumLevels() const = 0;
  101. //! Threshold for the distance between features and SVM classifying plane.
  102. //! Usually it is 0 and should be specified in the detector coefficients (as the last free
  103. //! coefficient). But if the free coefficient is omitted (which is allowed), you can specify it
  104. //! manually here.
  105. virtual void setHitThreshold(double hit_threshold) = 0;
  106. virtual double getHitThreshold() const = 0;
  107. //! Window stride. It must be a multiple of block stride.
  108. virtual void setWinStride(Size win_stride) = 0;
  109. virtual Size getWinStride() const = 0;
  110. //! Coefficient of the detection window increase.
  111. virtual void setScaleFactor(double scale0) = 0;
  112. virtual double getScaleFactor() const = 0;
  113. //! Coefficient to regulate the similarity threshold. When detected, some
  114. //! objects can be covered by many rectangles. 0 means not to perform grouping.
  115. //! See groupRectangles.
  116. virtual void setGroupThreshold(int group_threshold) = 0;
  117. virtual int getGroupThreshold() const = 0;
  118. //! Descriptor storage format:
  119. //! - **DESCR_FORMAT_ROW_BY_ROW** - Row-major order.
  120. //! - **DESCR_FORMAT_COL_BY_COL** - Column-major order.
  121. virtual void setDescriptorFormat(int descr_format) = 0;
  122. virtual int getDescriptorFormat() const = 0;
  123. /** @brief Returns the number of coefficients required for the classification.
  124. */
  125. virtual size_t getDescriptorSize() const = 0;
  126. /** @brief Returns the block histogram size.
  127. */
  128. virtual size_t getBlockHistogramSize() const = 0;
  129. /** @brief Sets coefficients for the linear SVM classifier.
  130. */
  131. virtual void setSVMDetector(InputArray detector) = 0;
  132. /** @brief Returns coefficients of the classifier trained for people detection.
  133. */
  134. virtual Mat getDefaultPeopleDetector() const = 0;
  135. /** @brief Performs object detection without a multi-scale window.
  136. @param img Source image. CV_8UC1 and CV_8UC4 types are supported for now.
  137. @param found_locations Left-top corner points of detected objects boundaries.
  138. @param confidences Optional output array for confidences.
  139. */
  140. virtual void detect(InputArray img,
  141. std::vector<Point>& found_locations,
  142. std::vector<double>* confidences = NULL) = 0;
  143. /** @brief Performs object detection with a multi-scale window.
  144. @param img Source image. See cuda::HOGDescriptor::detect for type limitations.
  145. @param found_locations Detected objects boundaries.
  146. @param confidences Optional output array for confidences.
  147. */
  148. virtual void detectMultiScale(InputArray img,
  149. std::vector<Rect>& found_locations,
  150. std::vector<double>* confidences = NULL) = 0;
  151. /** @brief Returns block descriptors computed for the whole image.
  152. @param img Source image. See cuda::HOGDescriptor::detect for type limitations.
  153. @param descriptors 2D array of descriptors.
  154. @param stream CUDA stream.
  155. */
  156. virtual void compute(InputArray img,
  157. OutputArray descriptors,
  158. Stream& stream = Stream::Null()) = 0;
  159. };
  160. //
  161. // CascadeClassifier
  162. //
  163. /** @brief Cascade classifier class used for object detection. Supports HAAR and LBP cascades. :
  164. @note
  165. - A cascade classifier example can be found at
  166. opencv_source_code/samples/gpu/cascadeclassifier.cpp
  167. - A Nvidea API specific cascade classifier example can be found at
  168. opencv_source_code/samples/gpu/cascadeclassifier_nvidia_api.cpp
  169. */
  170. class CV_EXPORTS CascadeClassifier : public Algorithm
  171. {
  172. public:
  173. /** @brief Loads the classifier from a file. Cascade type is detected automatically by constructor parameter.
  174. @param filename Name of the file from which the classifier is loaded. Only the old haar classifier
  175. (trained by the haar training application) and NVIDIA's nvbin are supported for HAAR and only new
  176. type of OpenCV XML cascade supported for LBP. The working haar models can be found at opencv_folder/data/haarcascades_cuda/
  177. */
  178. static Ptr<CascadeClassifier> create(const String& filename);
  179. /** @overload
  180. */
  181. static Ptr<CascadeClassifier> create(const FileStorage& file);
  182. //! Maximum possible object size. Objects larger than that are ignored. Used for
  183. //! second signature and supported only for LBP cascades.
  184. virtual void setMaxObjectSize(Size maxObjectSize) = 0;
  185. virtual Size getMaxObjectSize() const = 0;
  186. //! Minimum possible object size. Objects smaller than that are ignored.
  187. virtual void setMinObjectSize(Size minSize) = 0;
  188. virtual Size getMinObjectSize() const = 0;
  189. //! Parameter specifying how much the image size is reduced at each image scale.
  190. virtual void setScaleFactor(double scaleFactor) = 0;
  191. virtual double getScaleFactor() const = 0;
  192. //! Parameter specifying how many neighbors each candidate rectangle should have
  193. //! to retain it.
  194. virtual void setMinNeighbors(int minNeighbors) = 0;
  195. virtual int getMinNeighbors() const = 0;
  196. virtual void setFindLargestObject(bool findLargestObject) = 0;
  197. virtual bool getFindLargestObject() = 0;
  198. virtual void setMaxNumObjects(int maxNumObjects) = 0;
  199. virtual int getMaxNumObjects() const = 0;
  200. virtual Size getClassifierSize() const = 0;
  201. /** @brief Detects objects of different sizes in the input image.
  202. @param image Matrix of type CV_8U containing an image where objects should be detected.
  203. @param objects Buffer to store detected objects (rectangles).
  204. @param stream CUDA stream.
  205. To get final array of detected objects use CascadeClassifier::convert method.
  206. @code
  207. Ptr<cuda::CascadeClassifier> cascade_gpu = cuda::CascadeClassifier::create(...);
  208. Mat image_cpu = imread(...)
  209. GpuMat image_gpu(image_cpu);
  210. GpuMat objbuf;
  211. cascade_gpu->detectMultiScale(image_gpu, objbuf);
  212. std::vector<Rect> faces;
  213. cascade_gpu->convert(objbuf, faces);
  214. for(int i = 0; i < detections_num; ++i)
  215. cv::rectangle(image_cpu, faces[i], Scalar(255));
  216. imshow("Faces", image_cpu);
  217. @endcode
  218. @sa CascadeClassifier::detectMultiScale
  219. */
  220. virtual void detectMultiScale(InputArray image,
  221. OutputArray objects,
  222. Stream& stream = Stream::Null()) = 0;
  223. /** @brief Converts objects array from internal representation to standard vector.
  224. @param gpu_objects Objects array in internal representation.
  225. @param objects Resulting array.
  226. */
  227. virtual void convert(OutputArray gpu_objects,
  228. std::vector<Rect>& objects) = 0;
  229. };
  230. //! @}
  231. }} // namespace cv { namespace cuda {
  232. #endif /* OPENCV_CUDAOBJDETECT_HPP */