File: object_detection.rst

package info (click to toggle)
opencv 2.3.1-11%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 82,284 kB
  • sloc: xml: 493,314; cpp: 334,117; ansic: 108,641; java: 15,407; python: 14,061; sh: 107; makefile: 61
file content (318 lines) | stat: -rw-r--r-- 12,389 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
Object Detection
================

.. highlight:: cpp

.. index:: gpu::HOGDescriptor

gpu::HOGDescriptor
------------------
.. ocv:class:: gpu::HOGDescriptor

The class implements Histogram of Oriented Gradients ([Dalal2005]_) object detector.
::

    struct CV_EXPORTS HOGDescriptor
    {
        enum { DEFAULT_WIN_SIGMA = -1 };
        enum { DEFAULT_NLEVELS = 64 };
        enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL };

        HOGDescriptor(Size win_size=Size(64, 128), Size block_size=Size(16, 16),
                      Size block_stride=Size(8, 8), Size cell_size=Size(8, 8),
                      int nbins=9, double win_sigma=DEFAULT_WIN_SIGMA,
                      double threshold_L2hys=0.2, bool gamma_correction=true,
                      int nlevels=DEFAULT_NLEVELS);

        size_t getDescriptorSize() const;
        size_t getBlockHistogramSize() const;

        void setSVMDetector(const vector<float>& detector);

        static vector<float> getDefaultPeopleDetector();
        static vector<float> getPeopleDetector48x96();
        static vector<float> getPeopleDetector64x128();

        void detect(const GpuMat& img, vector<Point>& found_locations,
                    double hit_threshold=0, Size win_stride=Size(),
                    Size padding=Size());

        void detectMultiScale(const GpuMat& img, vector<Rect>& found_locations,
                              double hit_threshold=0, Size win_stride=Size(),
                              Size padding=Size(), double scale0=1.05,
                              int group_threshold=2);

        void getDescriptors(const GpuMat& img, Size win_stride,
                            GpuMat& descriptors,
                            int descr_format=DESCR_FORMAT_COL_BY_COL);

        Size win_size;
        Size block_size;
        Size block_stride;
        Size cell_size;
        int nbins;
        double win_sigma;
        double threshold_L2hys;
        bool gamma_correction;
        int nlevels;

    private:
        // Hidden
    }


Interfaces of all methods are kept similar to the ``CPU HOG`` descriptor and detector analogues as much as possible.

.. index:: gpu::HOGDescriptor::HOGDescriptor

gpu::HOGDescriptor::HOGDescriptor
-------------------------------------
.. ocv:function:: gpu::HOGDescriptor::HOGDescriptor(Size win_size=Size(64, 128), Size block_size=Size(16, 16), Size block_stride=Size(8, 8), Size cell_size=Size(8, 8), int nbins=9, double win_sigma=DEFAULT_WIN_SIGMA, double threshold_L2hys=0.2, bool gamma_correction=true, int nlevels=DEFAULT_NLEVELS)

    Creates the ``HOG`` descriptor and detector.

   :param win_size: Detection window size. Align to block size and block stride.

   :param block_size: Block size in pixels. Align to cell size. Only (16,16) is supported for now.

   :param block_stride: Block stride. It must be a multiple of cell size.

   :param cell_size: Cell size. Only (8, 8) is supported for now.

   :param nbins: Number of bins. Only 9 bins per cell are supported for now.

   :param win_sigma: Gaussian smoothing window parameter.

   :param threshold_L2Hys: L2-Hys normalization method shrinkage.

   :param gamma_correction: Flag to specify whether the gamma correction preprocessing is required or not.

   :param nlevels: Maximum number of detection window increases.

.. index:: gpu::HOGDescriptor::getDescriptorSize

gpu::HOGDescriptor::getDescriptorSize
-----------------------------------------
.. ocv:function:: size_t gpu::HOGDescriptor::getDescriptorSize() const

    Returns the number of coefficients required for the classification.

.. index:: gpu::HOGDescriptor::getBlockHistogramSize

gpu::HOGDescriptor::getBlockHistogramSize
---------------------------------------------
.. ocv:function:: size_t gpu::HOGDescriptor::getBlockHistogramSize() const

    Returns the block histogram size.

.. index:: gpu::HOGDescriptor::setSVMDetector

gpu::HOGDescriptor::setSVMDetector
--------------------------------------
.. ocv:function:: void gpu::HOGDescriptor::setSVMDetector(const vector<float>& detector)

    Sets coefficients for the linear SVM classifier.

.. index:: gpu::HOGDescriptor::getDefaultPeopleDetector

gpu::HOGDescriptor::getDefaultPeopleDetector
------------------------------------------------
.. ocv:function:: static vector<float> gpu::HOGDescriptor::getDefaultPeopleDetector()

    Returns coefficients of the classifier trained for people detection (for default window size).

.. index:: gpu::HOGDescriptor::getPeopleDetector48x96

gpu::HOGDescriptor::getPeopleDetector48x96
----------------------------------------------
.. ocv:function:: static vector<float> gpu::HOGDescriptor::getPeopleDetector48x96()

    Returns coefficients of the classifier trained for people detection (for 48x96 windows).

.. index:: gpu::HOGDescriptor::getPeopleDetector64x128

gpu::HOGDescriptor::getPeopleDetector64x128
-----------------------------------------------
.. ocv:function:: static vector<float> gpu::HOGDescriptor::getPeopleDetector64x128()

    Returns coefficients of the classifier trained for people detection (for 64x128 windows).

.. index:: gpu::HOGDescriptor::detect

gpu::HOGDescriptor::detect
------------------------------
.. ocv:function:: void gpu::HOGDescriptor::detect(const GpuMat& img, vector<Point>& found_locations, double hit_threshold=0, Size win_stride=Size(), Size padding=Size())

    Performs object detection without a multi-scale window.

   :param img: Source image.  ``CV_8UC1``  and  ``CV_8UC4`` types are supported for now.

   :param found_locations: Left-top corner points of detected objects boundaries.

   :param hit_threshold: Threshold for the distance between features and SVM classifying plane. Usually it is 0 and should be specfied 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.

   :param win_stride: Window stride. It must be a multiple of block stride.

   :param padding: Mock parameter to keep the CPU interface compatibility. It must be (0,0).

.. index:: gpu::HOGDescriptor::detectMultiScale

gpu::HOGDescriptor::detectMultiScale
----------------------------------------
.. ocv:function:: void gpu::HOGDescriptor::detectMultiScale(const GpuMat& img, vector<Rect>& found_locations, double hit_threshold=0, Size win_stride=Size(), Size padding=Size(), double scale0=1.05, int group_threshold=2)

    Performs object detection with a multi-scale window.

   :param img: Source image. See  :ocv:func:`gpu::HOGDescriptor::detect`  for type limitations.

   :param found_locations: Detected objects boundaries.

   :param hit_threshold: Threshold for the distance between features and SVM classifying plane. See  :ocv:func:`gpu::HOGDescriptor::detect`  for details.

   :param win_stride: Window stride. It must be a multiple of block stride.

   :param padding: Mock parameter to keep the CPU interface compatibility. It must be (0,0).

   :param scale0: Coefficient of the detection window increase.

   :param group_threshold: Coefficient to regulate the similarity threshold. When detected, some objects can be covered by many rectangles. 0 means not to perform grouping. See  :ocv:func:`groupRectangles` .

.. index:: gpu::HOGDescriptor::getDescriptors

gpu::HOGDescriptor::getDescriptors
--------------------------------------
.. ocv:function:: void gpu::HOGDescriptor::getDescriptors(const GpuMat& img, Size win_stride, GpuMat& descriptors, int descr_format=DESCR_FORMAT_COL_BY_COL)

    Returns block descriptors computed for the whole image. The function is mainly used to learn the classifier.

   :param img: Source image. See  :ocv:func:`gpu::HOGDescriptor::detect`  for type limitations.

   :param win_stride: Window stride. It must be a multiple of block stride.

   :param descriptors: 2D array of descriptors.

   :param descr_format: Descriptor storage format: 

        * **DESCR_FORMAT_ROW_BY_ROW** - Row-major order.

        * **DESCR_FORMAT_COL_BY_COL** - Column-major order.
            

.. index:: gpu::CascadeClassifier_GPU

gpu::CascadeClassifier_GPU
--------------------------
.. ocv:class:: gpu::CascadeClassifier_GPU

Cascade classifier class used for object detection. 
::

    class CV_EXPORTS CascadeClassifier_GPU
    {
    public:
            CascadeClassifier_GPU();
            CascadeClassifier_GPU(const string& filename);
            ~CascadeClassifier_GPU();

            bool empty() const;
            bool load(const string& filename);
            void release();

            /* Returns number of detected objects */
            int detectMultiScale( const GpuMat& image, GpuMat& objectsBuf, double scaleFactor=1.2, int minNeighbors=4, Size minSize=Size());

            /* Finds only the largest object. Special mode if training is required.*/
            bool findLargestObject;

            /* Draws rectangles in input image */
            bool visualizeInPlace;

            Size getClassifierSize() const;
    };


.. index:: gpu::CascadeClassifier_GPU::CascadeClassifier_GPU

gpu::CascadeClassifier_GPU::CascadeClassifier_GPU
-----------------------------------------------------
.. ocv:function:: gpu::CascadeClassifier_GPU(const string& filename)

    Loads the classifier from a file.

    :param filename: Name of the file from which the classifier is loaded. Only the old ``haar`` classifier (trained by the ``haar`` training application) and NVIDIA's ``nvbin`` are supported.

.. index:: gpu::CascadeClassifier_GPU::empty

.. _gpu::CascadeClassifier_GPU::empty:

gpu::CascadeClassifier_GPU::empty
-------------------------------------
.. ocv:function:: bool gpu::CascadeClassifier_GPU::empty() const

    Checks whether the classifier is loaded or not.

.. index:: gpu::CascadeClassifier_GPU::load

.. _gpu::CascadeClassifier_GPU::load:

gpu::CascadeClassifier_GPU::load
------------------------------------
.. ocv:function:: bool gpu::CascadeClassifier_GPU::load(const string& filename)

    Loads the classifier from a file. The previous content is destroyed.

    :param filename: Name of the file from which the classifier is loaded. Only the old ``haar`` classifier (trained by the ``haar`` training application) and NVIDIA's ``nvbin`` are supported.

.. index:: gpu::CascadeClassifier_GPU::release

gpu::CascadeClassifier_GPU::release
---------------------------------------
.. ocv:function:: void gpu::CascadeClassifier_GPU::release()

    Destroys the loaded classifier.

.. index:: gpu::CascadeClassifier_GPU::detectMultiScale

gpu::CascadeClassifier_GPU::detectMultiScale
------------------------------------------------
.. ocv:function:: int gpu::CascadeClassifier_GPU::detectMultiScale(const GpuMat& image, GpuMat& objectsBuf, double scaleFactor=1.2, int minNeighbors=4, Size minSize=Size())

    Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.

    :param image: Matrix of type  ``CV_8U``  containing an image where objects should be detected.

    :param objects: Buffer to store detected objects (rectangles). If it is empty, it is allocated with the default size. If not empty, the function searches not more than N objects, where ``N = sizeof(objectsBufer's data)/sizeof(cv::Rect)``.

    :param scaleFactor: Value to specify how much the image size is reduced at each image scale.

    :param minNeighbors: Value to specify how many neighbours each candidate rectangle has to retain.

    :param minSize: Minimum possible object size. Objects smaller than that are ignored.

    The function returns the number of detected objects, so you can retrieve them as in the following example: 

::

    gpu::CascadeClassifier_GPU cascade_gpu(...);

    Mat image_cpu = imread(...)
    GpuMat image_gpu(image_cpu);

    GpuMat objbuf;
    int detections_number = cascade_gpu.detectMultiScale( image_gpu,
              objbuf, 1.2, minNeighbors);

    Mat obj_host;
    // download only detected number of rectangles
    objbuf.colRange(0, detections_number).download(obj_host);

    Rect* faces = obj_host.ptr<Rect>();
    for(int i = 0; i < detections_num; ++i)
       cv::rectangle(image_cpu, faces[i], Scalar(255));

    imshow("Faces", image_cpu);


.. seealso:: :ocv:func:`CascadeClassifier::detectMultiScale` 

.. [Dalal2005] Navneet Dalal and Bill Triggs. *Histogram of oriented gradients for human detection*. 2005.