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