|
53 | 53 | @addtogroup cuda |
54 | 54 | @{ |
55 | 55 | @defgroup cuda_calib3d Camera Calibration and 3D Reconstruction |
56 | | - @defgroup cuda_objdetect Object Detection |
57 | 56 | @} |
58 | 57 | */ |
59 | 58 |
|
60 | 59 | namespace cv { namespace cuda { |
61 | 60 |
|
62 | | -//////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector ////////////// |
63 | | - |
64 | | -//! @addtogroup cuda_objdetect |
65 | | -//! @{ |
66 | | - |
67 | | -struct CV_EXPORTS HOGConfidence |
68 | | -{ |
69 | | - double scale; |
70 | | - std::vector<Point> locations; |
71 | | - std::vector<double> confidences; |
72 | | - std::vector<double> part_scores[4]; |
73 | | -}; |
74 | | - |
75 | | -/** @brief The class implements Histogram of Oriented Gradients (@cite Dalal2005) object detector. |
76 | | -
|
77 | | -Interfaces of all methods are kept similar to the CPU HOG descriptor and detector analogues as much |
78 | | -as possible. |
79 | | -
|
80 | | -@note |
81 | | - - An example applying the HOG descriptor for people detection can be found at |
82 | | - opencv_source_code/samples/cpp/peopledetect.cpp |
83 | | - - A CUDA example applying the HOG descriptor for people detection can be found at |
84 | | - opencv_source_code/samples/gpu/hog.cpp |
85 | | - - (Python) An example applying the HOG descriptor for people detection can be found at |
86 | | - opencv_source_code/samples/python2/peopledetect.py |
87 | | - */ |
88 | | -struct CV_EXPORTS HOGDescriptor |
89 | | -{ |
90 | | - enum { DEFAULT_WIN_SIGMA = -1 }; |
91 | | - enum { DEFAULT_NLEVELS = 64 }; |
92 | | - enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL }; |
93 | | - |
94 | | - /** @brief Creates the HOG descriptor and detector. |
95 | | -
|
96 | | - @param win_size Detection window size. Align to block size and block stride. |
97 | | - @param block_size Block size in pixels. Align to cell size. Only (16,16) is supported for now. |
98 | | - @param block_stride Block stride. It must be a multiple of cell size. |
99 | | - @param cell_size Cell size. Only (8, 8) is supported for now. |
100 | | - @param nbins Number of bins. Only 9 bins per cell are supported for now. |
101 | | - @param win_sigma Gaussian smoothing window parameter. |
102 | | - @param threshold_L2hys L2-Hys normalization method shrinkage. |
103 | | - @param gamma_correction Flag to specify whether the gamma correction preprocessing is required or |
104 | | - not. |
105 | | - @param nlevels Maximum number of detection window increases. |
106 | | - */ |
107 | | - HOGDescriptor(Size win_size=Size(64, 128), Size block_size=Size(16, 16), |
108 | | - Size block_stride=Size(8, 8), Size cell_size=Size(8, 8), |
109 | | - int nbins=9, double win_sigma=DEFAULT_WIN_SIGMA, |
110 | | - double threshold_L2hys=0.2, bool gamma_correction=true, |
111 | | - int nlevels=DEFAULT_NLEVELS); |
112 | | - |
113 | | - /** @brief Returns the number of coefficients required for the classification. |
114 | | - */ |
115 | | - size_t getDescriptorSize() const; |
116 | | - /** @brief Returns the block histogram size. |
117 | | - */ |
118 | | - size_t getBlockHistogramSize() const; |
119 | | - |
120 | | - /** @brief Sets coefficients for the linear SVM classifier. |
121 | | - */ |
122 | | - void setSVMDetector(const std::vector<float>& detector); |
123 | | - |
124 | | - /** @brief Returns coefficients of the classifier trained for people detection (for default window size). |
125 | | - */ |
126 | | - static std::vector<float> getDefaultPeopleDetector(); |
127 | | - /** @brief Returns coefficients of the classifier trained for people detection (for 48x96 windows). |
128 | | - */ |
129 | | - static std::vector<float> getPeopleDetector48x96(); |
130 | | - /** @brief Returns coefficients of the classifier trained for people detection (for 64x128 windows). |
131 | | - */ |
132 | | - static std::vector<float> getPeopleDetector64x128(); |
133 | | - |
134 | | - /** @brief Performs object detection without a multi-scale window. |
135 | | -
|
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 hit_threshold Threshold for the distance between features and SVM classifying plane. |
139 | | - Usually it is 0 and should be specfied in the detector coefficients (as the last free |
140 | | - coefficient). But if the free coefficient is omitted (which is allowed), you can specify it |
141 | | - manually here. |
142 | | - @param win_stride Window stride. It must be a multiple of block stride. |
143 | | - @param padding Mock parameter to keep the CPU interface compatibility. It must be (0,0). |
144 | | - */ |
145 | | - void detect(const GpuMat& img, std::vector<Point>& found_locations, |
146 | | - double hit_threshold=0, Size win_stride=Size(), |
147 | | - Size padding=Size()); |
148 | | - |
149 | | - /** @brief Performs object detection with a multi-scale window. |
150 | | -
|
151 | | - @param img Source image. See cuda::HOGDescriptor::detect for type limitations. |
152 | | - @param found_locations Detected objects boundaries. |
153 | | - @param hit_threshold Threshold for the distance between features and SVM classifying plane. See |
154 | | - cuda::HOGDescriptor::detect for details. |
155 | | - @param win_stride Window stride. It must be a multiple of block stride. |
156 | | - @param padding Mock parameter to keep the CPU interface compatibility. It must be (0,0). |
157 | | - @param scale0 Coefficient of the detection window increase. |
158 | | - @param group_threshold Coefficient to regulate the similarity threshold. When detected, some |
159 | | - objects can be covered by many rectangles. 0 means not to perform grouping. See groupRectangles . |
160 | | - */ |
161 | | - void detectMultiScale(const GpuMat& img, std::vector<Rect>& found_locations, |
162 | | - double hit_threshold=0, Size win_stride=Size(), |
163 | | - Size padding=Size(), double scale0=1.05, |
164 | | - int group_threshold=2); |
165 | | - |
166 | | - void computeConfidence(const GpuMat& img, std::vector<Point>& hits, double hit_threshold, |
167 | | - Size win_stride, Size padding, std::vector<Point>& locations, std::vector<double>& confidences); |
168 | | - |
169 | | - void computeConfidenceMultiScale(const GpuMat& img, std::vector<Rect>& found_locations, |
170 | | - double hit_threshold, Size win_stride, Size padding, |
171 | | - std::vector<HOGConfidence> &conf_out, int group_threshold); |
172 | | - |
173 | | - /** @brief Returns block descriptors computed for the whole image. |
174 | | -
|
175 | | - @param img Source image. See cuda::HOGDescriptor::detect for type limitations. |
176 | | - @param win_stride Window stride. It must be a multiple of block stride. |
177 | | - @param descriptors 2D array of descriptors. |
178 | | - @param descr_format Descriptor storage format: |
179 | | - - **DESCR_FORMAT_ROW_BY_ROW** - Row-major order. |
180 | | - - **DESCR_FORMAT_COL_BY_COL** - Column-major order. |
181 | | -
|
182 | | - The function is mainly used to learn the classifier. |
183 | | - */ |
184 | | - void getDescriptors(const GpuMat& img, Size win_stride, |
185 | | - GpuMat& descriptors, |
186 | | - int descr_format=DESCR_FORMAT_COL_BY_COL); |
187 | | - |
188 | | - Size win_size; |
189 | | - Size block_size; |
190 | | - Size block_stride; |
191 | | - Size cell_size; |
192 | | - int nbins; |
193 | | - double win_sigma; |
194 | | - double threshold_L2hys; |
195 | | - bool gamma_correction; |
196 | | - int nlevels; |
197 | | - |
198 | | -protected: |
199 | | - void computeBlockHistograms(const GpuMat& img); |
200 | | - void computeGradient(const GpuMat& img, GpuMat& grad, GpuMat& qangle); |
201 | | - |
202 | | - double getWinSigma() const; |
203 | | - bool checkDetectorSize() const; |
204 | | - |
205 | | - static int numPartsWithin(int size, int part_size, int stride); |
206 | | - static Size numPartsWithin(Size size, Size part_size, Size stride); |
207 | | - |
208 | | - // Coefficients of the separating plane |
209 | | - float free_coef; |
210 | | - GpuMat detector; |
211 | | - |
212 | | - // Results of the last classification step |
213 | | - GpuMat labels, labels_buf; |
214 | | - Mat labels_host; |
215 | | - |
216 | | - // Results of the last histogram evaluation step |
217 | | - GpuMat block_hists, block_hists_buf; |
218 | | - |
219 | | - // Gradients conputation results |
220 | | - GpuMat grad, qangle, grad_buf, qangle_buf; |
221 | | - |
222 | | - // returns subbuffer with required size, reallocates buffer if nessesary. |
223 | | - static GpuMat getBuffer(const Size& sz, int type, GpuMat& buf); |
224 | | - static GpuMat getBuffer(int rows, int cols, int type, GpuMat& buf); |
225 | | - |
226 | | - std::vector<GpuMat> image_scales; |
227 | | -}; |
228 | | - |
229 | | -//////////////////////////// CascadeClassifier //////////////////////////// |
230 | | - |
231 | | -/** @brief Cascade classifier class used for object detection. Supports HAAR and LBP cascades. : |
232 | | -
|
233 | | -@note |
234 | | - - A cascade classifier example can be found at |
235 | | - opencv_source_code/samples/gpu/cascadeclassifier.cpp |
236 | | - - A Nvidea API specific cascade classifier example can be found at |
237 | | - opencv_source_code/samples/gpu/cascadeclassifier_nvidia_api.cpp |
238 | | - */ |
239 | | -class CV_EXPORTS CascadeClassifier_CUDA |
240 | | -{ |
241 | | -public: |
242 | | - CascadeClassifier_CUDA(); |
243 | | - /** @brief Loads the classifier from a file. Cascade type is detected automatically by constructor parameter. |
244 | | -
|
245 | | - @param filename Name of the file from which the classifier is loaded. Only the old haar classifier |
246 | | - (trained by the haar training application) and NVIDIA's nvbin are supported for HAAR and only new |
247 | | - type of OpenCV XML cascade supported for LBP. |
248 | | - */ |
249 | | - CascadeClassifier_CUDA(const String& filename); |
250 | | - ~CascadeClassifier_CUDA(); |
251 | | - |
252 | | - /** @brief Checks whether the classifier is loaded or not. |
253 | | - */ |
254 | | - bool empty() const; |
255 | | - /** @brief Loads the classifier from a file. The previous content is destroyed. |
256 | | -
|
257 | | - @param filename Name of the file from which the classifier is loaded. Only the old haar classifier |
258 | | - (trained by the haar training application) and NVIDIA's nvbin are supported for HAAR and only new |
259 | | - type of OpenCV XML cascade supported for LBP. |
260 | | - */ |
261 | | - bool load(const String& filename); |
262 | | - /** @brief Destroys the loaded classifier. |
263 | | - */ |
264 | | - void release(); |
265 | | - |
266 | | - /** @overload */ |
267 | | - int detectMultiScale(const GpuMat& image, GpuMat& objectsBuf, double scaleFactor = 1.2, int minNeighbors = 4, Size minSize = Size()); |
268 | | - /** @brief Detects objects of different sizes in the input image. |
269 | | -
|
270 | | - @param image Matrix of type CV_8U containing an image where objects should be detected. |
271 | | - @param objectsBuf Buffer to store detected objects (rectangles). If it is empty, it is allocated |
272 | | - with the default size. If not empty, the function searches not more than N objects, where |
273 | | - N = sizeof(objectsBufer's data)/sizeof(cv::Rect). |
274 | | - @param maxObjectSize Maximum possible object size. Objects larger than that are ignored. Used for |
275 | | - second signature and supported only for LBP cascades. |
276 | | - @param scaleFactor Parameter specifying how much the image size is reduced at each image scale. |
277 | | - @param minNeighbors Parameter specifying how many neighbors each candidate rectangle should have |
278 | | - to retain it. |
279 | | - @param minSize Minimum possible object size. Objects smaller than that are ignored. |
280 | | -
|
281 | | - The detected objects are returned as a list of rectangles. |
282 | | -
|
283 | | - The function returns the number of detected objects, so you can retrieve them as in the following |
284 | | - example: |
285 | | - @code |
286 | | - cuda::CascadeClassifier_CUDA cascade_gpu(...); |
287 | | -
|
288 | | - Mat image_cpu = imread(...) |
289 | | - GpuMat image_gpu(image_cpu); |
290 | | -
|
291 | | - GpuMat objbuf; |
292 | | - int detections_number = cascade_gpu.detectMultiScale( image_gpu, |
293 | | - objbuf, 1.2, minNeighbors); |
294 | | -
|
295 | | - Mat obj_host; |
296 | | - // download only detected number of rectangles |
297 | | - objbuf.colRange(0, detections_number).download(obj_host); |
298 | | -
|
299 | | - Rect* faces = obj_host.ptr<Rect>(); |
300 | | - for(int i = 0; i < detections_num; ++i) |
301 | | - cv::rectangle(image_cpu, faces[i], Scalar(255)); |
302 | | -
|
303 | | - imshow("Faces", image_cpu); |
304 | | - @endcode |
305 | | - @sa CascadeClassifier::detectMultiScale |
306 | | - */ |
307 | | - int detectMultiScale(const GpuMat& image, GpuMat& objectsBuf, Size maxObjectSize, Size minSize = Size(), double scaleFactor = 1.1, int minNeighbors = 4); |
308 | | - |
309 | | - bool findLargestObject; |
310 | | - bool visualizeInPlace; |
311 | | - |
312 | | - Size getClassifierSize() const; |
313 | | - |
314 | | -private: |
315 | | - struct CascadeClassifierImpl; |
316 | | - CascadeClassifierImpl* impl; |
317 | | - struct HaarCascade; |
318 | | - struct LbpCascade; |
319 | | - friend class CascadeClassifier_CUDA_LBP; |
320 | | -}; |
321 | | - |
322 | | -//! @} cuda_objdetect |
323 | | - |
324 | 61 | //////////////////////////// Labeling //////////////////////////// |
325 | 62 |
|
326 | 63 | //! @addtogroup cuda |
|
0 commit comments