File: random_cropper_abstract.h

package info (click to toggle)
dlib 19.10-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 124,860 kB
  • sloc: cpp: 293,144; xml: 25,083; python: 1,452; sh: 324; java: 229; makefile: 181; perl: 18
file content (346 lines) | stat: -rw-r--r-- 12,387 bytes parent folder | download | duplicates (3)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright (C) 2016  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#undef DLIB_RaNDOM_CROPPER_ABSTRACT_H_
#ifdef DLIB_RaNDOM_CROPPER_ABSTRACT_H_

#include "../threads.h"
#include <mutex>
#include <vector>
#include "interpolation.h"
#include "../image_processing/full_object_detection.h"
#include "../rand.h"

namespace dlib
{
    class random_cropper
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This object is a tool for extracting random crops of objects from a set of
                images.  The crops are randomly jittered in scale, translation, and
                rotation but more or less centered on objects specified by mmod_rect
                objects.
                
            THREAD SAFETY
                It is safe for multiple threads to make concurrent calls to this object's
                operator() methods.
        !*/

    public:

        random_cropper (
        );
        /*!
            ensures
                - #get_chip_dims() == chip_dims(300,300)
                - #get_randomly_flip() == true
                - #get_max_rotation_degrees() == 30
                - #get_min_object_length_long_dim() == 70 
                - #get_min_object_length_short_dim() == 30
                - #get_max_object_size() == 0.7
                - #get_background_crops_fraction() == 0.5
                - #get_translate_amount() == 0.1
        !*/

        void set_seed (
            time_t seed
        );
        /*!
            ensures
                - Seeds the internal random number generator with the given seed.
        !*/

        double get_translate_amount (
        ) const; 
        /*!
            ensures
                - When a box is cropped out, it will be randomly translated prior to
                  cropping by #get_translate_amount()*(the box's height) up or down and
                  #get_translate_amount()*(the box's width) left or right.
        !*/

        void set_translate_amount (
            double value
        );
        /*!
            requires
                - value >= 0
            ensures
                - #get_translate_amount() == value
        !*/

        double get_background_crops_fraction (
        ) const; 
        /*!
            ensures
                - When making random crops, get_background_crops_fraction() fraction of
                  them will be from random background rather than being centered on some
                  object in the dataset.
        !*/

        void set_background_crops_fraction (
            double value
        );
        /*!
            requires
                - 0 <= value <= 1
            ensures
                - #get_background_crops_fraction() == value
        !*/

        const chip_dims& get_chip_dims(
        ) const; 
        /*!
            ensures
                - returns the dimensions of image chips produced by this object.
        !*/

        void set_chip_dims (
            const chip_dims& dims
        );
        /*!
            ensures
                - #get_chip_dims() == dims
        !*/

        void set_chip_dims (
            unsigned long rows,
            unsigned long cols
        );
        /*!
            ensures
                - #get_chip_dims() == chip_dims(rows,cols)
        !*/

        bool get_randomly_flip (
        ) const;
        /*!
            ensures
                - if this object will randomly mirror chips left to right.
        !*/

        void set_randomly_flip (
            bool value
        );
        /*!
            ensures
                - #get_randomly_flip() == value
        !*/

        double get_max_rotation_degrees (
        ) const;
        /*!
            ensures
                - When extracting an image chip, this object will pick a random rotation
                  in the range [-get_max_rotation_degrees(), get_max_rotation_degrees()]
                  and rotate the chip by that amount.
        !*/

        void set_max_rotation_degrees (
            double value
        );
        /*!
            ensures
                - #get_max_rotation_degrees() == std::abs(value)
        !*/

        long get_min_object_length_long_dim (
        ) const; 
        /*!
            ensures
                - When a chip is extracted around an object, the chip will be sized so that
                  the longest edge of the object (i.e. either its height or width,
                  whichever is longer) is at least #get_min_object_length_long_dim() pixels
                  in length.  When we say "object" here we are referring specifically to
                  the rectangle in the mmod_rect output by the cropper.
        !*/

        long get_min_object_length_short_dim (
        ) const;
        /*!
            ensures
                - When a chip is extracted around an object, the chip will be sized so that
                  the shortest edge of the object (i.e. either its height or width,
                  whichever is shorter) is at least #get_min_object_length_short_dim()
                  pixels in length.  When we say "object" here we are referring
                  specifically to the rectangle in the mmod_rect output by the cropper.
        !*/

        void set_min_object_size (
            long long_dim,
            long short_dim
        ); 
        /*!
            requires
                - 0 < short_dim <= long_dim
            ensures
                - #get_min_object_length_short_dim() == short_dim
                - #get_min_object_length_long_dim() == long_dim
        !*/

        double get_max_object_size (
        ) const; 
        /*!
            ensures
                - When a chip is extracted around an object, the chip will be sized so that
                  both the object's height and width are at most get_max_object_size() *
                  the chip's height and width, respectively.  E.g. if the chip is 640x480
                  pixels in size then the object will be at most 480*get_max_object_size()
                  pixels tall and 640*get_max_object_size() pixels wide. 
        !*/

        void set_max_object_size (
            double value
        ); 
        /*!
            requires
                - 0 < value 
            ensures
                - #get_max_object_size() == value
        !*/

        template <
            typename array_type
            >
        void append (
            size_t num_crops,
            const array_type& images,
            const std::vector<std::vector<mmod_rect>>& rects,
            array_type& crops,
            std::vector<std::vector<mmod_rect>>& crop_rects
        );
        /*!
            requires
                - images.size() == rects.size()
                - crops.size() == crop_rects.size()
                - for all valid i:
                    - images[i].size() != 0
                - array_type is a type with an interface compatible with dlib::array or
                  std::vector and it must in turn contain image objects that implement the
                  interface defined in dlib/image_processing/generic_image.h 
            ensures
                - Randomly extracts num_crops chips from images and appends them to the end
                  of crops.  We also copy the object metadata for each extracted crop and
                  store it into #crop_rects.  In particular, calling this function is the
                  same as making multiple calls to the version of operator() below that
                  outputs a single crop, except that append() will use multiple CPU cores
                  to do the processing and is therefore faster.
                - #crops.size() == crops.size()+num_crops
                - #crop_rects.size() == crop_rects.size()+num_crops
        !*/

        template <
            typename array_type
            >
        void operator() (
            size_t num_crops,
            const array_type& images,
            const std::vector<std::vector<mmod_rect>>& rects,
            array_type& crops,
            std::vector<std::vector<mmod_rect>>& crop_rects
        );
        /*!
            requires
                - images.size() == rects.size()
                - for all valid i:
                    - images[i].size() != 0
                - array_type is a type with an interface compatible with dlib::array or
                  std::vector and it must in turn contain image objects that implement the
                  interface defined in dlib/image_processing/generic_image.h 
            ensures
                - Randomly extracts num_crops chips from images.  We also copy the object
                  metadata for each extracted crop and store it into #crop_rects.  In
                  particular, calling this function is the same as invoking the version of
                  operator() below multiple times, except that this version of operator()
                  will use multiple CPU cores to do the processing and is therefore faster.
                - #crops.size() == num_crops
                - #crop_rects.size() == num_crops
        !*/

        template <
            typename array_type,
            typename image_type
            >
        void operator() (
            const array_type& images,
            const std::vector<std::vector<mmod_rect>>& rects,
            image_type& crop,
            std::vector<mmod_rect>& crop_rects
        );
        /*!
            requires
                - images.size() == rects.size()
                - for all valid i:
                    - images[i].size() != 0
                - image_type == an image object that implements the interface defined in
                  dlib/image_processing/generic_image.h 
                - array_type is a type with an interface compatible with dlib::array or
                  std::vector and it must in turn contain image objects that implement the
                  interface defined in dlib/image_processing/generic_image.h 
            ensures
                - Selects a random image and creates a random crop from it.  Specifically,
                  we pick a random index IDX < images.size() and then execute 
                    (*this)(images[IDX],rects[IDX],crop,crop_rects) 
        !*/

        template <
            typename image_type1,
            typename image_type2
            >
        void operator() (
            const image_type1& img,
            const std::vector<mmod_rect>& rects,
            image_type2& crop,
            std::vector<mmod_rect>& crop_rects
        );
        /*!
            requires
                - img.size() != 0
                - image_type1 == an image object that implements the interface defined in
                  dlib/image_processing/generic_image.h 
                - image_type2 == an image object that implements the interface defined in
                  dlib/image_processing/generic_image.h 
            ensures
                - Extracts a random crop from img and copies over the mmod_rect objects in
                  rects to #crop_rects if they are contained inside the crop.  Moreover,
                  rectangles are marked as ignore if they aren't completely contained
                  inside the crop.
                - #crop_rects.size() <= rects.size()
        !*/

        template <
            typename image_type1
            >
        image_type1 operator() (
            const image_type1& img
        );
        /*!
            requires
                - img.size() != 0
                - image_type1 == an image object that implements the interface defined in
                  dlib/image_processing/generic_image.h 
            ensures
                - This function simply calls (*this)(img, junk1, crop, junk2) and returns
                  crop.  Therefore it is simply a convenience function for extracting a
                  random background patch.
        !*/
    };

// ----------------------------------------------------------------------------------------

    std::ostream& operator<< (
        std::ostream& out,
        const random_cropper& item
    );
    /*!
        ensures
            - Prints the state of all the parameters of item to out.
    !*/

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_RaNDOM_CROPPER_ABSTRACT_H_