image2.cpp 73.9 KB
Newer Older
1
2
// Copyright (C) 2018  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
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
#include "opaque_types.h"
#include <dlib/python.h>
#include "dlib/pixel.h"
#include <dlib/image_transforms.h>
#include <dlib/image_processing.h>

using namespace dlib;
using namespace std;

namespace py = pybind11;

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

template <typename T>
numpy_image<T> py_resize_image (
    const numpy_image<T>& img,
    unsigned long rows,
    unsigned long cols
)
{
    numpy_image<T> out;
    set_image_size(out, rows, cols);
    resize_image(img, out);
    return out;
}

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

template <typename T>
numpy_image<T> py_equalize_histogram (
    const numpy_image<T>& img
)
{
    numpy_image<T> out;
    equalize_histogram(img,out);
    return out;
}

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

43
template <typename T>
44
45
line ht_get_line (
    const hough_transform& ht,
46
    const dlib::vector<T,2>& p
47
48
49
50
51
52
53
)  
{ 
    DLIB_CASSERT(get_rect(ht).contains(p));
    auto temp = ht.get_line(p); 
    return line(temp.first, temp.second);
}

54
template <typename T>
55
56
double ht_get_line_angle_in_degrees (
    const hough_transform& ht,
57
    const dlib::vector<T,2>& p 
58
59
60
61
62
63
)  
{ 
    DLIB_CASSERT(get_rect(ht).contains(p));
    return ht.get_line_angle_in_degrees(p); 
}

64
template <typename T>
65
66
py::tuple ht_get_line_properties (
    const hough_transform& ht,
67
    const dlib::vector<T,2>& p
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
)  
{ 
    DLIB_CASSERT(get_rect(ht).contains(p));
    double angle_in_degrees;
    double radius;
    ht.get_line_properties(p, angle_in_degrees, radius);
    return py::make_tuple(angle_in_degrees, radius);
}

point ht_get_best_hough_point (
    hough_transform& ht,
    const point& p,
    const numpy_image<float>& himg
) 
{ 
Davis King's avatar
Davis King committed
83
    DLIB_CASSERT(num_rows(himg) == ht.size() && num_columns(himg) == ht.size() &&
84
85
86
        get_rect(ht).contains(p) == true,
        "\t point hough_transform::get_best_hough_point()"
        << "\n\t Invalid arguments given to this function."
Davis King's avatar
Davis King committed
87
88
89
        << "\n\t num_rows(himg): " << num_rows(himg)
        << "\n\t num_columns(himg): " << num_columns(himg)
        << "\n\t size():    " << ht.size()
90
91
92
93
94
95
96
97
98
99
100
101
102
        << "\n\t p:         " << p 
    );
    return ht.get_best_hough_point(p,himg); 
}

template <
    typename T 
    >
numpy_image<float> compute_ht (
    const hough_transform& ht,
    const numpy_image<T>& img,
    const rectangle& box
) 
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
    numpy_image<float> out;
    ht(img, box, out);
    return out;
}

template <
    typename T 
    >
numpy_image<float> compute_ht2 (
    const hough_transform& ht,
    const numpy_image<T>& img
) 
{
    numpy_image<float> out;
    ht(img, out);
    return out;
}

template <
    typename T 
    >
py::list ht_find_pixels_voting_for_lines (
    const hough_transform& ht,
    const numpy_image<T>& img,
    const rectangle& box,
    const std::vector<point>& hough_points,
    const unsigned long angle_window_size = 1,
    const unsigned long radius_window_size = 1
) 
{
    return vector_to_python_list(ht.find_pixels_voting_for_lines(img, box, hough_points, angle_window_size, radius_window_size));
}

template <
    typename T 
    >
py::list ht_find_pixels_voting_for_lines2 (
    const hough_transform& ht,
    const numpy_image<T>& img,
    const std::vector<point>& hough_points,
    const unsigned long angle_window_size = 1,
    const unsigned long radius_window_size = 1
) 
{
    return vector_to_python_list(ht.find_pixels_voting_for_lines(img, hough_points, angle_window_size, radius_window_size));
}

std::vector<point> ht_find_strong_hough_points(
    hough_transform& ht,
    const numpy_image<float>& himg,
    const float hough_count_thresh,
    const double angle_nms_thresh,
    const double radius_nms_thresh
)
{
    return ht.find_strong_hough_points(himg, hough_count_thresh, angle_nms_thresh, radius_nms_thresh);
}

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

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

void register_hough_transform(py::module& m)
{
    const char* class_docs =
"This object is a tool for computing the line finding version of the Hough transform \n\
given some kind of edge detection image as input.  It also allows the edge pixels \n\
to be weighted such that higher weighted edge pixels contribute correspondingly \n\
more to the output of the Hough transform, allowing stronger edges to create \n\
correspondingly stronger line detections in the final Hough transform.";


    const char* doc_constr = 
"requires \n\
    - size_ > 0 \n\
ensures \n\
    - This object will compute Hough transforms that are size_ by size_ pixels.   \n\
      This is in terms of both the Hough accumulator array size as well as the \n\
      input image size. \n\
    - size() == size_";
        /*!
            requires
                - size_ > 0
            ensures
                - This object will compute Hough transforms that are size_ by size_ pixels.  
                  This is in terms of both the Hough accumulator array size as well as the
                  input image size.
                - size() == size_
        !*/

193
    py::class_<hough_transform>(m, "hough_transform", class_docs)
194
        .def(py::init<unsigned long>(), doc_constr, py::arg("size_"))
195
        .def_property_readonly("size", &hough_transform::size,
Davis King's avatar
Davis King committed
196
            "returns the size of the Hough transforms generated by this object.  In particular, this object creates Hough transform images that are size by size pixels in size.")
197
198
        .def("get_line", &ht_get_line<long>, py::arg("p"))
        .def("get_line", &ht_get_line<double>, py::arg("p"),
199
"requires \n\
Davis King's avatar
Davis King committed
200
    - rectangle(0,0,size-1,size-1).contains(p) == true \n\
201
202
203
204
      (i.e. p must be a point inside the Hough accumulator array) \n\
ensures \n\
    - returns the line segment in the original image space corresponding \n\
      to Hough transform point p.  \n\
Davis King's avatar
Davis King committed
205
    - The returned points are inside rectangle(0,0,size-1,size-1).") 
206
207
    /*!
        requires
Davis King's avatar
Davis King committed
208
            - rectangle(0,0,size-1,size-1).contains(p) == true
209
210
211
212
              (i.e. p must be a point inside the Hough accumulator array)
        ensures
            - returns the line segment in the original image space corresponding
              to Hough transform point p. 
Davis King's avatar
Davis King committed
213
            - The returned points are inside rectangle(0,0,size-1,size-1).
214
215
    !*/

216
217
        .def("get_line_angle_in_degrees", &ht_get_line_angle_in_degrees<long>, py::arg("p"))
        .def("get_line_angle_in_degrees", &ht_get_line_angle_in_degrees<double>, py::arg("p"),
218
"requires \n\
Davis King's avatar
Davis King committed
219
    - rectangle(0,0,size-1,size-1).contains(p) == true \n\
220
221
222
223
224
225
      (i.e. p must be a point inside the Hough accumulator array) \n\
ensures \n\
    - returns the angle, in degrees, of the line corresponding to the Hough \n\
      transform point p.")
    /*!
        requires
Davis King's avatar
Davis King committed
226
            - rectangle(0,0,size-1,size-1).contains(p) == true
227
228
229
230
231
232
233
              (i.e. p must be a point inside the Hough accumulator array)
        ensures
            - returns the angle, in degrees, of the line corresponding to the Hough
              transform point p.
    !*/


234
235
        .def("get_line_properties", &ht_get_line_properties<long>, py::arg("p"))
        .def("get_line_properties", &ht_get_line_properties<double>, py::arg("p"),
236
"requires \n\
Davis King's avatar
Davis King committed
237
    - rectangle(0,0,size-1,size-1).contains(p) == true \n\
238
239
240
241
242
243
244
245
      (i.e. p must be a point inside the Hough accumulator array) \n\
ensures \n\
    - Converts a point in the Hough transform space into an angle, in degrees, \n\
      and a radius, measured in pixels from the center of the input image. \n\
    - let ANGLE_IN_DEGREES == the angle of the line corresponding to the Hough \n\
      transform point p.  Moreover: -90 <= ANGLE_IN_DEGREES < 90. \n\
    - RADIUS == the distance from the center of the input image, measured in \n\
      pixels, and the line corresponding to the Hough transform point p. \n\
Davis King's avatar
Davis King committed
246
      Moreover: -sqrt(size*size/2) <= RADIUS <= sqrt(size*size/2) \n\
247
248
249
    - returns a tuple of (ANGLE_IN_DEGREES, RADIUS)" )
    /*!
        requires
Davis King's avatar
Davis King committed
250
            - rectangle(0,0,size-1,size-1).contains(p) == true
251
252
253
254
255
256
257
258
              (i.e. p must be a point inside the Hough accumulator array)
        ensures
            - Converts a point in the Hough transform space into an angle, in degrees,
              and a radius, measured in pixels from the center of the input image.
            - let ANGLE_IN_DEGREES == the angle of the line corresponding to the Hough
              transform point p.  Moreover: -90 <= ANGLE_IN_DEGREES < 90.
            - RADIUS == the distance from the center of the input image, measured in
              pixels, and the line corresponding to the Hough transform point p.
Davis King's avatar
Davis King committed
259
              Moreover: -sqrt(size*size/2) <= RADIUS <= sqrt(size*size/2)
260
261
262
            - returns a tuple of (ANGLE_IN_DEGREES, RADIUS)
    !*/

263
        .def("get_best_hough_point", &ht_get_best_hough_point, py::arg("p"), py::arg("himg"),
264
"requires \n\
Davis King's avatar
Davis King committed
265
266
    - himg has size rows and columns. \n\
    - rectangle(0,0,size-1,size-1).contains(p) == true \n\
267
268
269
270
271
272
273
274
275
ensures \n\
    - This function interprets himg as a Hough image and p as a point in the \n\
      original image space.  Given this, it finds the maximum scoring line that \n\
      passes though p.  That is, it checks all the Hough accumulator bins in \n\
      himg corresponding to lines though p and returns the location with the \n\
      largest score.   \n\
    - returns a point X such that get_rect(himg).contains(X) == true")
    /*!
        requires
Davis King's avatar
Davis King committed
276
277
            - himg has size rows and columns.
            - rectangle(0,0,size-1,size-1).contains(p) == true
278
279
280
281
282
283
284
285
286
        ensures
            - This function interprets himg as a Hough image and p as a point in the
              original image space.  Given this, it finds the maximum scoring line that
              passes though p.  That is, it checks all the Hough accumulator bins in
              himg corresponding to lines though p and returns the location with the
              largest score.  
            - returns a point X such that get_rect(himg).contains(X) == true
    !*/

287
288
289
290
291
292
293
294
295
296
        .def("__call__", &compute_ht<uint8_t>, py::arg("img"), py::arg("box"))
        .def("__call__", &compute_ht<uint16_t>, py::arg("img"), py::arg("box"))
        .def("__call__", &compute_ht<uint32_t>, py::arg("img"), py::arg("box"))
        .def("__call__", &compute_ht<uint64_t>, py::arg("img"), py::arg("box"))
        .def("__call__", &compute_ht<int8_t>, py::arg("img"), py::arg("box"))
        .def("__call__", &compute_ht<int16_t>, py::arg("img"), py::arg("box"))
        .def("__call__", &compute_ht<int32_t>, py::arg("img"), py::arg("box"))
        .def("__call__", &compute_ht<int64_t>, py::arg("img"), py::arg("box"))
        .def("__call__", &compute_ht<float>, py::arg("img"), py::arg("box"))
        .def("__call__", &compute_ht<double>, py::arg("img"), py::arg("box"),
297
"requires \n\
Davis King's avatar
Davis King committed
298
299
    - box.width() == size \n\
    - box.height() == size \n\
300
301
302
303
304
305
306
307
308
309
ensures \n\
    - Computes the Hough transform of the part of img contained within box. \n\
      In particular, we do a grayscale version of the Hough transform where any \n\
      non-zero pixel in img is treated as a potential component of a line and \n\
      accumulated into the returned Hough accumulator image.  However, rather than \n\
      adding 1 to each relevant accumulator bin we add the value of the pixel \n\
      in img to each Hough accumulator bin.  This means that, if all the \n\
      pixels in img are 0 or 1 then this routine performs a normal Hough \n\
      transform.  However, if some pixels have larger values then they will be \n\
      weighted correspondingly more in the resulting Hough transform. \n\
Davis King's avatar
Davis King committed
310
    - The returned hough transform image will be size rows by size columns. \n\
311
312
313
314
315
316
317
318
319
    - The returned image is the Hough transform of the part of img contained in \n\
      box.  Each point in the Hough image corresponds to a line in the input box. \n\
      In particular, the line for hough_image[y][x] is given by get_line(point(x,y)).  \n\
      Also, when viewing the Hough image, the x-axis gives the angle of the line \n\
      and the y-axis the distance of the line from the center of the box.  The \n\
      conversion between Hough coordinates and angle and pixel distance can be \n\
      obtained by calling get_line_properties()." )
    /*!
        requires
Davis King's avatar
Davis King committed
320
321
            - box.width() == size
            - box.height() == size
322
323
324
325
326
327
328
329
330
331
        ensures
            - Computes the Hough transform of the part of img contained within box.
              In particular, we do a grayscale version of the Hough transform where any
              non-zero pixel in img is treated as a potential component of a line and
              accumulated into the returned Hough accumulator image.  However, rather than
              adding 1 to each relevant accumulator bin we add the value of the pixel
              in img to each Hough accumulator bin.  This means that, if all the
              pixels in img are 0 or 1 then this routine performs a normal Hough
              transform.  However, if some pixels have larger values then they will be
              weighted correspondingly more in the resulting Hough transform.
Davis King's avatar
Davis King committed
332
            - The returned hough transform image will be size rows by size columns.
333
334
335
336
337
338
339
340
341
            - The returned image is the Hough transform of the part of img contained in
              box.  Each point in the Hough image corresponds to a line in the input box.
              In particular, the line for hough_image[y][x] is given by get_line(point(x,y)). 
              Also, when viewing the Hough image, the x-axis gives the angle of the line
              and the y-axis the distance of the line from the center of the box.  The
              conversion between Hough coordinates and angle and pixel distance can be
              obtained by calling get_line_properties().
    !*/

342
343
344
345
346
347
348
349
350
351
        .def("__call__", &compute_ht2<uint8_t>, py::arg("img"))
        .def("__call__", &compute_ht2<uint16_t>, py::arg("img"))
        .def("__call__", &compute_ht2<uint32_t>, py::arg("img"))
        .def("__call__", &compute_ht2<uint64_t>, py::arg("img"))
        .def("__call__", &compute_ht2<int8_t>, py::arg("img"))
        .def("__call__", &compute_ht2<int16_t>, py::arg("img"))
        .def("__call__", &compute_ht2<int32_t>, py::arg("img"))
        .def("__call__", &compute_ht2<int64_t>, py::arg("img"))
        .def("__call__", &compute_ht2<float>, py::arg("img"))
        .def("__call__", &compute_ht2<double>, py::arg("img"),
352
353
            "    simply performs: return self(img, get_rect(img)).  That is, just runs the hough transform on the whole input image.")

354
355
356
357
358
359
360
361
362
363
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines<uint8_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines<uint16_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines<uint32_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines<uint64_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines<int8_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines<int16_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines<int32_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines<int64_t>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines<float>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines<double>, py::arg("img"), py::arg("box"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1,
364
"requires \n\
Davis King's avatar
Davis King committed
365
366
    - box.width() == size \n\
    - box.height() == size \n\
367
    - for all valid i: \n\
Davis King's avatar
Davis King committed
368
        - rectangle(0,0,size-1,size-1).contains(hough_points[i]) == true \n\
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
          (i.e. hough_points must contain points in the output Hough transform \n\
          space generated by this object.) \n\
    - angle_window_size >= 1 \n\
    - radius_window_size >= 1 \n\
ensures \n\
    - This function computes the Hough transform of the part of img contained \n\
      within box.  It does the same computation as __call__() defined above, \n\
      except instead of accumulating into an image we create an explicit list \n\
      of all the points in img that contributed to each line (i.e each point in \n\
      the Hough image). To do this we take a list of Hough points as input and \n\
      only record hits on these specifically identified Hough points.  A \n\
      typical use of find_pixels_voting_for_lines() is to first run the normal \n\
      Hough transform using __call__(), then find the lines you are interested \n\
      in, and then call find_pixels_voting_for_lines() to determine which \n\
      pixels in the input image belong to those lines. \n\
    - This routine returns a vector, CONSTITUENT_POINTS, with the following \n\
      properties: \n\
Davis King's avatar
Davis King committed
386
        - CONSTITUENT_POINTS.size == hough_points.size \n\
387
388
389
390
391
392
393
394
        - for all valid i: \n\
            - Let HP[i] = centered_rect(hough_points[i], angle_window_size, radius_window_size) \n\
            - Any point in img with a non-zero value that lies on a line \n\
              corresponding to one of the Hough points in HP[i] is added to \n\
              CONSTITUENT_POINTS[i].  Therefore, when this routine finishes, \n\
              #CONSTITUENT_POINTS[i] will contain all the points in img that \n\
              voted for the lines associated with the Hough accumulator bins in \n\
              HP[i]. \n\
Davis King's avatar
Davis King committed
395
            - #CONSTITUENT_POINTS[i].size == the number of points in img that \n\
396
397
398
              voted for any of the lines HP[i] in Hough space.  Note, however, \n\
              that if angle_window_size or radius_window_size are made so large \n\
              that HP[i] overlaps HP[j] for i!=j then the overlapping regions \n\
Davis King's avatar
Davis King committed
399
              of Hough space are assigned to HP[i] or HP[j] arbitrarily. \n\
Davis King's avatar
Davis King committed
400
401
402
              That is, we treat HP[i] and HP[j] as disjoint even if their boxes \n\
              overlap.  In this case, the overlapping region is assigned to \n\
              either HP[i] or HP[j] in an arbitrary manner." )
403
404
    /*!
        requires
Davis King's avatar
Davis King committed
405
406
            - box.width() == size
            - box.height() == size
407
            - for all valid i:
Davis King's avatar
Davis King committed
408
                - rectangle(0,0,size-1,size-1).contains(hough_points[i]) == true
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
                  (i.e. hough_points must contain points in the output Hough transform
                  space generated by this object.)
            - angle_window_size >= 1
            - radius_window_size >= 1
        ensures
            - This function computes the Hough transform of the part of img contained
              within box.  It does the same computation as __call__() defined above,
              except instead of accumulating into an image we create an explicit list
              of all the points in img that contributed to each line (i.e each point in
              the Hough image). To do this we take a list of Hough points as input and
              only record hits on these specifically identified Hough points.  A
              typical use of find_pixels_voting_for_lines() is to first run the normal
              Hough transform using __call__(), then find the lines you are interested
              in, and then call find_pixels_voting_for_lines() to determine which
              pixels in the input image belong to those lines.
            - This routine returns a vector, CONSTITUENT_POINTS, with the following
              properties:
Davis King's avatar
Davis King committed
426
                - CONSTITUENT_POINTS.size == hough_points.size
427
428
429
430
431
432
433
434
                - for all valid i:
                    - Let HP[i] = centered_rect(hough_points[i], angle_window_size, radius_window_size)
                    - Any point in img with a non-zero value that lies on a line
                      corresponding to one of the Hough points in HP[i] is added to
                      CONSTITUENT_POINTS[i].  Therefore, when this routine finishes,
                      #CONSTITUENT_POINTS[i] will contain all the points in img that
                      voted for the lines associated with the Hough accumulator bins in
                      HP[i].
Davis King's avatar
Davis King committed
435
                    - #CONSTITUENT_POINTS[i].size == the number of points in img that
436
437
438
                      voted for any of the lines HP[i] in Hough space.  Note, however,
                      that if angle_window_size or radius_window_size are made so large
                      that HP[i] overlaps HP[j] for i!=j then the overlapping regions
Davis King's avatar
Davis King committed
439
                      of Hough space are assigned to HP[i] or HP[j] arbitrarily.
Davis King's avatar
Davis King committed
440
441
442
                      That is, we treat HP[i] and HP[j] as disjoint even if their boxes
                      overlap.  In this case, the overlapping region is assigned to
                      either HP[i] or HP[j] in an arbitrary manner.
443
    !*/
444
445
446
447
448
449
450
451
452
453
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines2<uint8_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines2<uint16_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines2<uint32_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines2<uint64_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines2<int8_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines2<int16_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines2<int32_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines2<int64_t>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines2<float>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1)
        .def("find_pixels_voting_for_lines", &ht_find_pixels_voting_for_lines2<double>, py::arg("img"), py::arg("hough_points"), py::arg("angle_window_size")=1, py::arg("radius_window_size")=1,
454
455
456
"    performs: return find_pixels_voting_for_lines(img, get_rect(img), hough_points, angle_window_size, radius_window_size); \n\
That is, just runs the routine on the whole input image." )

457
        .def("find_strong_hough_points", &ht_find_strong_hough_points, py::arg("himg"), py::arg("hough_count_thresh"), py::arg("angle_nms_thresh"), py::arg("radius_nms_thresh"),
458
459
460
461
462
463
464
465
466
"requires \n\
    - himg has size() rows and columns. \n\
    - angle_nms_thresh >= 0 \n\
    - radius_nms_thresh >= 0 \n\
ensures \n\
    - This routine finds strong lines in a Hough transform and performs \n\
      non-maximum suppression on the detected lines.  Recall that each point in \n\
      Hough space is associated with a line. Therefore, this routine finds all \n\
      the pixels in himg (a Hough transform image) with values >= \n\
467
      hough_count_thresh and performs non-maximum suppression on the \n\
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
      identified list of pixels.  It does this by discarding lines that are \n\
      within angle_nms_thresh degrees of a stronger line or within \n\
      radius_nms_thresh distance (in terms of radius as defined by \n\
      get_line_properties()) to a stronger Hough point. \n\
    - The identified lines are returned as a list of coordinates in himg." );
    /*!
        requires
            - himg has size() rows and columns.
            - angle_nms_thresh >= 0
            - radius_nms_thresh >= 0
        ensures
            - This routine finds strong lines in a Hough transform and performs
              non-maximum suppression on the detected lines.  Recall that each point in
              Hough space is associated with a line. Therefore, this routine finds all
              the pixels in himg (a Hough transform image) with values >=
483
              hough_count_thresh and performs non-maximum suppression on the
484
485
486
487
488
489
490
              identified list of pixels.  It does this by discarding lines that are
              within angle_nms_thresh degrees of a stronger line or within
              radius_nms_thresh distance (in terms of radius as defined by
              get_line_properties()) to a stronger Hough point.
            - The identified lines are returned as a list of coordinates in himg.
    !*/

491

492
    m.def("get_rect", [](const hough_transform& ht){ return get_rect(ht); },
493
494
        "returns a rectangle(0,0,ht.size()-1,ht.size()-1).  Therefore, it is the rectangle that bounds the Hough transform image.", 
        py::arg("ht")  );
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
}

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

std::vector<point> py_remove_incoherent_edge_pixels (
    const std::vector<point>& line,
    const numpy_image<float>& horz_gradient,
    const numpy_image<float>& vert_gradient,
    double angle_threshold
)
{

    DLIB_CASSERT(num_rows(horz_gradient) == num_rows(vert_gradient));
    DLIB_CASSERT(num_columns(horz_gradient) == num_columns(vert_gradient));
    DLIB_CASSERT(angle_threshold >= 0);
    for (auto& p : line)
        DLIB_CASSERT(get_rect(horz_gradient).contains(p), "All line points must be inside the given images.");

    return remove_incoherent_edge_pixels(line, horz_gradient, vert_gradient, angle_threshold);
}

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

518
519
520
521
522
523
524
525
526
template <typename T>
numpy_image<T> py_transform_image (
    const numpy_image<T>& img,
    const point_transform_projective& map_point,
    long rows,
    long columns
)
{
    DLIB_CASSERT(rows > 0 && columns > 0, "The requested output image dimensions are invalid.");
Davis King's avatar
cleanup  
Davis King committed
527
    numpy_image<T> out(rows, columns);
528

Davis King's avatar
cleanup  
Davis King committed
529
    transform_image(img, out, interpolate_bilinear(), map_point);
530

Davis King's avatar
cleanup  
Davis King committed
531
    return out;
532
}
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// ----------------------------------------------------------------------------------------

template <typename T>
numpy_image<T> py_extract_image_4points (
    const numpy_image<T>& img,
    const py::list& corners,
    long rows,
    long columns
)
{
    DLIB_CASSERT(rows >= 0);
    DLIB_CASSERT(columns >= 0);
    DLIB_CASSERT(len(corners) == 4);

    numpy_image<T> out;
    set_image_size(out, rows, columns);
    try
    {
551
        extract_image_4points(img, out, python_list_to_array<dpoint,4>(corners));
552
553
554
555
556
557
        return out;
    } 
    catch (py::cast_error&){}

    try
    {
558
        extract_image_4points(img, out, python_list_to_array<line,4>(corners));
559
560
561
562
563
564
565
        return out;
    }
    catch(py::cast_error&)
    {
        throw dlib::error("extract_image_4points() requires the corners argument to be a list of 4 dpoints or 4 lines.");
    }
}
566

567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// ----------------------------------------------------------------------------------------

template <typename T>
numpy_image<T> py_mbd (
    const numpy_image<T>& img,
    size_t iterations,
    bool do_left_right_scans 
)
{
    numpy_image<T> out;
    min_barrier_distance(img, out, iterations, do_left_right_scans);
    return out;
}

numpy_image<unsigned char> py_mbd2 (
    const numpy_image<rgb_pixel>& img,
    size_t iterations,
    bool do_left_right_scans 
)
{
    numpy_image<unsigned char> out;
    min_barrier_distance(img, out, iterations, do_left_right_scans);
    return out;
}

592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
// ----------------------------------------------------------------------------------------

template <typename T>
numpy_image<T> py_extract_image_chip (
    const numpy_image<T>& img,
    const chip_details& chip_location 
)
{
    numpy_image<T> out;
    extract_image_chip(img, chip_location, out);
    return out;
}

template <typename T>
py::list py_extract_image_chips (
    const numpy_image<T>& img,
    const py::list& chip_locations
)
{
    dlib::array<numpy_image<T>> out;
    extract_image_chips(img, python_list_to_vector<chip_details>(chip_locations), out);
    py::list ret;
    for (auto& i : out)
        ret.append(i);
    return ret;
}

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

void register_extract_image_chip (py::module& m)
{
    const char* class_docs = 
"WHAT THIS OBJECT REPRESENTS \n\
    This is a simple tool for passing in a pair of row and column values to the \n\
    chip_details constructor.";


    auto print_chip_dims_str = [](const chip_dims& d)
    {
        std::ostringstream sout;
        sout << "rows="<< d.rows << ", cols=" << d.cols; 
        return sout.str();
    };
    auto print_chip_dims_repr = [](const chip_dims& d)
    {
        std::ostringstream sout;
        sout << "chip_dims(rows="<< d.rows << ", cols=" << d.cols << ")"; 
        return sout.str();
    };

    py::class_<chip_dims>(m, "chip_dims", class_docs)
        .def(py::init<unsigned long, unsigned long>(), py::arg("rows"), py::arg("cols"))
        .def("__str__", print_chip_dims_str)
        .def("__repr__", print_chip_dims_repr)
        .def_readwrite("rows", &chip_dims::rows)
        .def_readwrite("cols", &chip_dims::cols);



    auto print_chip_details_str = [](const chip_details& d)
    {
        std::ostringstream sout;
        sout << "rect=" << d.rect << ", angle="<< d.angle << ", rows="<< d.rows << ", cols=" << d.cols; 
        return sout.str();
    };
    auto print_chip_details_repr = [](const chip_details& d)
    {
        std::ostringstream sout;
        sout << "chip_details(rect=drectangle(" 
            << d.rect.left()<<","<<d.rect.top()<<","<<d.rect.right()<<","<<d.rect.bottom()
            <<"), angle="<< d.angle << ", dims=chip_dims(rows="<< d.rows << ", cols=" << d.cols << "))"; 
        return sout.str();
    };


    class_docs =
"WHAT THIS OBJECT REPRESENTS \n\
    This object describes where an image chip is to be extracted from within \n\
    another image.  In particular, it specifies that the image chip is \n\
    contained within the rectangle self.rect and that prior to extraction the \n\
    image should be rotated counter-clockwise by self.angle radians.  Finally, \n\
    the extracted chip should have self.rows rows and self.cols columns in it \n\
    regardless of the shape of self.rect.  This means that the extracted chip \n\
    will be stretched to fit via bilinear interpolation when necessary." ;
        /*!
            WHAT THIS OBJECT REPRESENTS
                This object describes where an image chip is to be extracted from within
                another image.  In particular, it specifies that the image chip is
                contained within the rectangle self.rect and that prior to extraction the
                image should be rotated counter-clockwise by self.angle radians.  Finally,
                the extracted chip should have self.rows rows and self.cols columns in it
                regardless of the shape of self.rect.  This means that the extracted chip
                will be stretched to fit via bilinear interpolation when necessary.
        !*/
    py::class_<chip_details>(m, "chip_details", class_docs)
        .def(py::init<drectangle>(), py::arg("rect"))
        .def(py::init<rectangle>(), py::arg("rect"),
"ensures \n\
    - self.rect == rect_ \n\
    - self.angle == 0 \n\
    - self.rows == rect.height() \n\
    - self.cols == rect.width()" 
        /*!
            ensures
                - self.rect == rect_
                - self.angle == 0
                - self.rows == rect.height()
                - self.cols == rect.width()
        !*/
            )
        .def(py::init<drectangle,unsigned long>(), py::arg("rect"), py::arg("size"))
        .def(py::init<rectangle,unsigned long>(), py::arg("rect"), py::arg("size"),
"ensures \n\
    - self.rect == rect \n\
    - self.angle == 0 \n\
    - self.rows and self.cols is set such that the total size of the chip is as close \n\
      to size as possible but still matches the aspect ratio of rect. \n\
    - As long as size and the aspect ratio of of rect stays constant then \n\
      self.rows and self.cols will always have the same values.  This means \n\
      that, for example, if you want all your chips to have the same dimensions \n\
      then ensure that size is always the same and also that rect always has \n\
      the same aspect ratio.  Otherwise the calculated values of self.rows and \n\
      self.cols may be different for different chips.  Alternatively, you can \n\
      use the chip_details constructor below that lets you specify the exact \n\
      values for rows and cols." 
        /*!
            ensures
                - self.rect == rect
                - self.angle == 0
                - self.rows and self.cols is set such that the total size of the chip is as close
                  to size as possible but still matches the aspect ratio of rect.
                - As long as size and the aspect ratio of of rect stays constant then
                  self.rows and self.cols will always have the same values.  This means
                  that, for example, if you want all your chips to have the same dimensions
                  then ensure that size is always the same and also that rect always has
                  the same aspect ratio.  Otherwise the calculated values of self.rows and
                  self.cols may be different for different chips.  Alternatively, you can
                  use the chip_details constructor below that lets you specify the exact
                  values for rows and cols.
        !*/
            )
        .def(py::init<drectangle,unsigned long,double>(), py::arg("rect"), py::arg("size"), py::arg("angle"))
        .def(py::init<rectangle,unsigned long,double>(), py::arg("rect"), py::arg("size"), py::arg("angle"),
"ensures \n\
    - self.rect == rect \n\
    - self.angle == angle \n\
    - self.rows and self.cols is set such that the total size of the chip is as \n\
      close to size as possible but still matches the aspect ratio of rect. \n\
    - As long as size and the aspect ratio of of rect stays constant then \n\
      self.rows and self.cols will always have the same values.  This means \n\
      that, for example, if you want all your chips to have the same dimensions \n\
      then ensure that size is always the same and also that rect always has \n\
      the same aspect ratio.  Otherwise the calculated values of self.rows and \n\
      self.cols may be different for different chips.  Alternatively, you can \n\
      use the chip_details constructor below that lets you specify the exact \n\
      values for rows and cols." 
        /*!
            ensures
                - self.rect == rect
                - self.angle == angle
                - self.rows and self.cols is set such that the total size of the chip is as
                  close to size as possible but still matches the aspect ratio of rect.
                - As long as size and the aspect ratio of of rect stays constant then
                  self.rows and self.cols will always have the same values.  This means
                  that, for example, if you want all your chips to have the same dimensions
                  then ensure that size is always the same and also that rect always has
                  the same aspect ratio.  Otherwise the calculated values of self.rows and
                  self.cols may be different for different chips.  Alternatively, you can
                  use the chip_details constructor below that lets you specify the exact
                  values for rows and cols.
        !*/
            )
        .def(py::init<drectangle,chip_dims>(), py::arg("rect"), py::arg("dims"))
        .def(py::init<rectangle,chip_dims>(), py::arg("rect"), py::arg("dims"),
"ensures \n\
    - self.rect == rect \n\
    - self.angle == 0 \n\
    - self.rows == dims.rows \n\
    - self.cols == dims.cols" 
        /*!
            ensures
                - self.rect == rect
                - self.angle == 0
                - self.rows == dims.rows
                - self.cols == dims.cols
        !*/
            )
        .def(py::init<drectangle,chip_dims,double>(), py::arg("rect"), py::arg("dims"), py::arg("angle"))
        .def(py::init<rectangle,chip_dims,double>(), py::arg("rect"), py::arg("dims"), py::arg("angle"),
"ensures \n\
    - self.rect == rect \n\
    - self.angle == angle \n\
    - self.rows == dims.rows \n\
    - self.cols == dims.cols" 
        /*!
            ensures
                - self.rect == rect
                - self.angle == angle
                - self.rows == dims.rows
                - self.cols == dims.cols
        !*/
            )
        .def(py::init<std::vector<dpoint>,std::vector<dpoint>,chip_dims>(), py::arg("chip_points"), py::arg("img_points"), py::arg("dims"))
        .def(py::init<std::vector<point>,std::vector<point>,chip_dims>(), py::arg("chip_points"), py::arg("img_points"), py::arg("dims"),
"requires \n\
    - len(chip_points) == len(img_points) \n\
    - len(chip_points) >= 2  \n\
ensures \n\
    - The chip will be extracted such that the pixel locations chip_points[i] \n\
      in the chip are mapped to img_points[i] in the original image by a \n\
      similarity transform.  That is, if you know the pixelwize mapping you \n\
      want between the chip and the original image then you use this function \n\
      of chip_details constructor to define the mapping. \n\
    - self.rows == dims.rows \n\
    - self.cols == dims.cols \n\
    - self.rect and self.angle are computed based on the given size of the output chip \n\
      (specified by dims) and the similarity transform between the chip and \n\
      image (specified by chip_points and img_points)." 
        /*!
            requires
                - len(chip_points) == len(img_points)
                - len(chip_points) >= 2 
            ensures
                - The chip will be extracted such that the pixel locations chip_points[i]
                  in the chip are mapped to img_points[i] in the original image by a
                  similarity transform.  That is, if you know the pixelwize mapping you
                  want between the chip and the original image then you use this function
                  of chip_details constructor to define the mapping.
                - self.rows == dims.rows
                - self.cols == dims.cols
                - self.rect and self.angle are computed based on the given size of the output chip
                  (specified by dims) and the similarity transform between the chip and
                  image (specified by chip_points and img_points).
        !*/
            )
        .def("__str__", print_chip_details_str)
        .def("__repr__", print_chip_details_repr)
        .def_readwrite("rect", &chip_details::rect)
        .def_readwrite("angle", &chip_details::angle)
        .def_readwrite("rows", &chip_details::rows)
        .def_readwrite("cols", &chip_details::cols);


    m.def("extract_image_chip", &py_extract_image_chip<uint8_t>, py::arg("img"), py::arg("chip_location"));
    m.def("extract_image_chip", &py_extract_image_chip<uint16_t>, py::arg("img"), py::arg("chip_location"));
    m.def("extract_image_chip", &py_extract_image_chip<uint32_t>, py::arg("img"), py::arg("chip_location"));
    m.def("extract_image_chip", &py_extract_image_chip<uint64_t>, py::arg("img"), py::arg("chip_location"));
    m.def("extract_image_chip", &py_extract_image_chip<int8_t>, py::arg("img"), py::arg("chip_location"));
    m.def("extract_image_chip", &py_extract_image_chip<int16_t>, py::arg("img"), py::arg("chip_location"));
    m.def("extract_image_chip", &py_extract_image_chip<int32_t>, py::arg("img"), py::arg("chip_location"));
    m.def("extract_image_chip", &py_extract_image_chip<int64_t>, py::arg("img"), py::arg("chip_location"));
    m.def("extract_image_chip", &py_extract_image_chip<float>, py::arg("img"), py::arg("chip_location"));
    m.def("extract_image_chip", &py_extract_image_chip<double>, py::arg("img"), py::arg("chip_location"));
    m.def("extract_image_chip", &py_extract_image_chip<rgb_pixel>, py::arg("img"), py::arg("chip_location"),
        "    This routine is just like extract_image_chips() except it takes a single \n"
        "    chip_details object and returns a single chip image rather than a list of images."
        );

    m.def("extract_image_chips", &py_extract_image_chips<uint8_t>, py::arg("img"), py::arg("chip_locations"));
    m.def("extract_image_chips", &py_extract_image_chips<uint16_t>, py::arg("img"), py::arg("chip_locations"));
    m.def("extract_image_chips", &py_extract_image_chips<uint32_t>, py::arg("img"), py::arg("chip_locations"));
    m.def("extract_image_chips", &py_extract_image_chips<uint64_t>, py::arg("img"), py::arg("chip_locations"));
    m.def("extract_image_chips", &py_extract_image_chips<int8_t>, py::arg("img"), py::arg("chip_locations"));
    m.def("extract_image_chips", &py_extract_image_chips<int16_t>, py::arg("img"), py::arg("chip_locations"));
    m.def("extract_image_chips", &py_extract_image_chips<int32_t>, py::arg("img"), py::arg("chip_locations"));
    m.def("extract_image_chips", &py_extract_image_chips<int64_t>, py::arg("img"), py::arg("chip_locations"));
    m.def("extract_image_chips", &py_extract_image_chips<float>, py::arg("img"), py::arg("chip_locations"));
    m.def("extract_image_chips", &py_extract_image_chips<double>, py::arg("img"), py::arg("chip_locations"));
    m.def("extract_image_chips", &py_extract_image_chips<rgb_pixel>, py::arg("img"), py::arg("chip_locations"),
"requires \n\
    - for all valid i:  \n\
        - chip_locations[i].rect.is_empty() == false \n\
        - chip_locations[i].rows*chip_locations[i].cols != 0 \n\
ensures \n\
    - This function extracts \"chips\" from an image.  That is, it takes a list of \n\
      rectangular sub-windows (i.e. chips) within an image and extracts those \n\
      sub-windows, storing each into its own image.  It also scales and rotates the \n\
      image chips according to the instructions inside each chip_details object. \n\
      It uses bilinear interpolation. \n\
    - The extracted image chips are returned in a python list of numpy arrays.  The \n\
      length of the returned array is len(chip_locations). \n\
    - Let CHIPS be the returned array, then we have: \n\
        - for all valid i: \n\
            - #CHIPS[i] == The image chip extracted from the position \n\
              chip_locations[i].rect in img. \n\
            - #CHIPS[i].shape(0) == chip_locations[i].rows \n\
            - #CHIPS[i].shape(1) == chip_locations[i].cols \n\
            - The image will have been rotated counter-clockwise by \n\
              chip_locations[i].angle radians, around the center of \n\
              chip_locations[i].rect, before the chip was extracted.  \n\
    - Any pixels in an image chip that go outside img are set to 0 (i.e. black)." 
    /*!
        requires
            - for all valid i: 
                - chip_locations[i].rect.is_empty() == false
                - chip_locations[i].rows*chip_locations[i].cols != 0
        ensures
            - This function extracts "chips" from an image.  That is, it takes a list of
              rectangular sub-windows (i.e. chips) within an image and extracts those
              sub-windows, storing each into its own image.  It also scales and rotates the
              image chips according to the instructions inside each chip_details object.
              It uses bilinear interpolation.
            - The extracted image chips are returned in a python list of numpy arrays.  The
              length of the returned array is len(chip_locations).
            - Let CHIPS be the returned array, then we have:
                - for all valid i:
                    - #CHIPS[i] == The image chip extracted from the position
                      chip_locations[i].rect in img.
                    - #CHIPS[i].shape(0) == chip_locations[i].rows
                    - #CHIPS[i].shape(1) == chip_locations[i].cols
                    - The image will have been rotated counter-clockwise by
                      chip_locations[i].angle radians, around the center of
                      chip_locations[i].rect, before the chip was extracted. 
            - Any pixels in an image chip that go outside img are set to 0 (i.e. black).
    !*/
        );

}

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

py::array py_tile_images (
    const py::list& images
)
{
    DLIB_CASSERT(len(images) > 0);

    if (is_image<rgb_pixel>(images[0].cast<py::array>()))
    {
        std::vector<numpy_image<rgb_pixel>> tmp(len(images));
        for (size_t i = 0; i < tmp.size(); ++i)
            assign_image(tmp[i], images[i].cast<py::array>());
        return numpy_image<rgb_pixel>(tile_images(tmp));
    }
    else
    {
        std::vector<numpy_image<unsigned char>> tmp(len(images));
        for (size_t i = 0; i < tmp.size(); ++i)
            assign_image(tmp[i], images[i].cast<py::array>());
        return numpy_image<unsigned char>(tile_images(tmp));
    }
}
934

935
936
// ----------------------------------------------------------------------------------------

937
938
939
940
941
942
943
944
945
946
947
948
949
950
template <typename T>
py::array_t<unsigned long> py_get_histogram (
    const numpy_image<T>& img,
    size_t hist_size
)
{
    matrix<unsigned long,1> hist;
    get_histogram(img,hist,hist_size);

    return numpy_image<unsigned long>(std::move(hist)).squeeze();
}

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

951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
py::array py_sub_image (
    const py::array& img,
    const rectangle& win
)
{
    DLIB_CASSERT(img.ndim() >= 2);

    auto width_step = img.strides(0);

    const long nr = img.shape(0);
    const long nc = img.shape(1);
    rectangle rect(0,0,nc-1,nr-1);
    rect = rect.intersect(win);

    std::vector<size_t> shape(img.ndim()), strides(img.ndim());
    for (size_t i = 0; i < shape.size(); ++i)
    {
        shape[i] = img.shape(i);
        strides[i] = img.strides(i);
    }

    shape[0] = rect.height();
    shape[1] = rect.width();

    size_t itemsize = img.itemsize();
    for (size_t i = 1; i < strides.size(); ++i)
        itemsize *= strides[i];

    const void* data = (char*)img.data() + itemsize*rect.left() + rect.top()*strides[0];

    return py::array(img.dtype(), shape, strides, data, img);
}

py::array py_sub_image2 (
    const py::tuple& image_and_rect_tuple
)
{
    DLIB_CASSERT(len(image_and_rect_tuple) == 2);
    return py_sub_image(image_and_rect_tuple[0].cast<py::array>(), image_and_rect_tuple[1].cast<rectangle>());
}

992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
// ----------------------------------------------------------------------------------------

template <typename T>
dpoint py_max_point(const numpy_image<T>& img)
{
    DLIB_CASSERT(img.size() != 0);
    return max_point(mat(img));
}

template <typename T>
dpoint py_max_point_interpolated(const numpy_image<T>& img)
{
    DLIB_CASSERT(img.size() != 0);
    return max_point_interpolated(mat(img));
}


1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
// ----------------------------------------------------------------------------------------

template <typename T>
void py_zero_border_pixels (
    numpy_image<T>& img,
    long x_border_size,
    long y_border_size
)
{
    zero_border_pixels(img, x_border_size, y_border_size);
}

template <typename T>
void py_zero_border_pixels2 (
    numpy_image<T>& img,
    const rectangle& inside
)
{
    zero_border_pixels(img, inside);
}

1030
1031
// ----------------------------------------------------------------------------------------

1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
void bind_image_classes2(py::module& m)
{

    const char* docs = "Resizes img, using bilinear interpolation, to have the indicated number of rows and columns.";


    m.def("resize_image", &py_resize_image<uint8_t>, py::arg("img"), py::arg("rows"), py::arg("cols"));
    m.def("resize_image", &py_resize_image<uint16_t>, py::arg("img"), py::arg("rows"), py::arg("cols"));
    m.def("resize_image", &py_resize_image<uint32_t>, py::arg("img"), py::arg("rows"), py::arg("cols"));
    m.def("resize_image", &py_resize_image<uint64_t>, py::arg("img"), py::arg("rows"), py::arg("cols"));
    m.def("resize_image", &py_resize_image<int8_t>, py::arg("img"), py::arg("rows"), py::arg("cols"));
    m.def("resize_image", &py_resize_image<int16_t>, py::arg("img"), py::arg("rows"), py::arg("cols"));
    m.def("resize_image", &py_resize_image<int32_t>, py::arg("img"), py::arg("rows"), py::arg("cols"));
    m.def("resize_image", &py_resize_image<int64_t>, py::arg("img"), py::arg("rows"), py::arg("cols"));
    m.def("resize_image", &py_resize_image<float>, py::arg("img"), py::arg("rows"), py::arg("cols"));
    m.def("resize_image", &py_resize_image<double>, docs, py::arg("img"), py::arg("rows"), py::arg("cols"));
1048
    m.def("resize_image", &py_resize_image<rgb_pixel>, docs, py::arg("img"), py::arg("rows"), py::arg("cols"));
1049

1050
1051
    register_extract_image_chip(m);

1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
    m.def("sub_image", &py_sub_image, py::arg("img"), py::arg("rect"),
"Returns a new numpy array that references the sub window in img defined by rect. \n\
If rect is larger than img then rect is cropped so that it does not go outside img. \n\
Therefore, this routine is equivalent to performing: \n\
    win = get_rect(img).intersect(rect) \n\
    subimg = img[win.top():win.bottom()-1,win.left():win.right()-1]" 
    /*!
        Returns a new numpy array that references the sub window in img defined by rect.
        If rect is larger than img then rect is cropped so that it does not go outside img.
        Therefore, this routine is equivalent to performing:
            win = get_rect(img).intersect(rect)
            subimg = img[win.top():win.bottom()-1,win.left():win.right()-1]
    !*/
        );
    m.def("sub_image", &py_sub_image2, py::arg("image_and_rect_tuple"),
        "Performs: return sub_image(image_and_rect_tuple[0], image_and_rect_tuple[1])");


1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
    m.def("get_histogram", &py_get_histogram<uint8_t>, py::arg("img"), py::arg("hist_size"));
    m.def("get_histogram", &py_get_histogram<uint16_t>, py::arg("img"), py::arg("hist_size"));
    m.def("get_histogram", &py_get_histogram<uint32_t>, py::arg("img"), py::arg("hist_size"));
    m.def("get_histogram", &py_get_histogram<uint64_t>, py::arg("img"), py::arg("hist_size"),
"ensures \n\
    - Returns a numpy array, HIST, that contains a histogram of the pixels in img. \n\
      In particular, we will have: \n\
        - len(HIST) == hist_size \n\
        - for all valid i:  \n\
            - HIST[i] == the number of times a pixel with intensity i appears in img." 
    /*!
        ensures
            - Returns a numpy array, HIST, that contains a histogram of the pixels in img.
              In particular, we will have:
                - len(HIST) == hist_size
                - for all valid i: 
                    - HIST[i] == the number of times a pixel with intensity i appears in img.
    !*/
        );


1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
    m.def("tile_images", py_tile_images, py::arg("images"),
"requires \n\
    - images is a list of numpy arrays that can be interpreted as images.  They \n\
      must all be the same type of image as well. \n\
ensures \n\
    - This function takes the given images and tiles them into a single large \n\
      square image and returns this new big tiled image.  Therefore, it is a \n\
      useful method to visualize many small images at once." 
        /*!
            requires
                - images is a list of numpy arrays that can be interpreted as images.  They
                  must all be the same type of image as well.
            ensures
                - This function takes the given images and tiles them into a single large
                  square image and returns this new big tiled image.  Therefore, it is a
                  useful method to visualize many small images at once.
        !*/
        );
1109
1110
1111
1112

    docs = "Returns a histogram equalized version of img.";
    m.def("equalize_histogram", &py_equalize_histogram<uint8_t>, py::arg("img"));
    m.def("equalize_histogram", &py_equalize_histogram<uint16_t>, docs, py::arg("img"));
1113

1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
    m.def("min_barrier_distance", &py_mbd<uint8_t>, py::arg("img"), py::arg("iterations")=10, py::arg("do_left_right_scans")=true);
    m.def("min_barrier_distance", &py_mbd<uint16_t>, py::arg("img"), py::arg("iterations")=10, py::arg("do_left_right_scans")=true);
    m.def("min_barrier_distance", &py_mbd<uint32_t>, py::arg("img"), py::arg("iterations")=10, py::arg("do_left_right_scans")=true);
    m.def("min_barrier_distance", &py_mbd<uint64_t>, py::arg("img"), py::arg("iterations")=10, py::arg("do_left_right_scans")=true);
    m.def("min_barrier_distance", &py_mbd<int8_t>, py::arg("img"), py::arg("iterations")=10, py::arg("do_left_right_scans")=true);
    m.def("min_barrier_distance", &py_mbd<int16_t>, py::arg("img"), py::arg("iterations")=10, py::arg("do_left_right_scans")=true);
    m.def("min_barrier_distance", &py_mbd<int32_t>, py::arg("img"), py::arg("iterations")=10, py::arg("do_left_right_scans")=true);
    m.def("min_barrier_distance", &py_mbd<int64_t>, py::arg("img"), py::arg("iterations")=10, py::arg("do_left_right_scans")=true);
    m.def("min_barrier_distance", &py_mbd<float>, py::arg("img"), py::arg("iterations")=10, py::arg("do_left_right_scans")=true);
    m.def("min_barrier_distance", &py_mbd<double>, py::arg("img"), py::arg("iterations")=10, py::arg("do_left_right_scans")=true);
    m.def("min_barrier_distance", &py_mbd2, py::arg("img"), py::arg("iterations")=10, py::arg("do_left_right_scans")=true,
"requires \n\
    - iterations > 0 \n\
ensures \n\
    - This function implements the salient object detection method described in the paper: \n\
        \"Minimum barrier salient object detection at 80 fps\" by Zhang, Jianming, et al.  \n\
      In particular, we compute the minimum barrier distance between the borders of \n\
      the image and all the other pixels.  The resulting image is returned.  Note that \n\
      the paper talks about a bunch of other things you could do beyond computing \n\
      the minimum barrier distance, but this function doesn't do any of that. It's \n\
      just the vanilla MBD. \n\
    - We will perform iterations iterations of MBD passes over the image.  Larger \n\
      values might give better results but run slower. \n\
    - During each MBD iteration we make raster scans over the image.  These pass \n\
      from top->bottom, bottom->top, left->right, and right->left.  If \n\
      do_left_right_scans==false then the left/right passes are not executed. \n\
      Skipping them makes the algorithm about 2x faster but might reduce the \n\
      quality of the output." 
    /*!
        requires
            - iterations > 0
        ensures
            - This function implements the salient object detection method described in the paper:
                "Minimum barrier salient object detection at 80 fps" by Zhang, Jianming, et al. 
              In particular, we compute the minimum barrier distance between the borders of
              the image and all the other pixels.  The resulting image is returned.  Note that
              the paper talks about a bunch of other things you could do beyond computing
              the minimum barrier distance, but this function doesn't do any of that. It's
              just the vanilla MBD.
            - We will perform iterations iterations of MBD passes over the image.  Larger
              values might give better results but run slower.
            - During each MBD iteration we make raster scans over the image.  These pass
              from top->bottom, bottom->top, left->right, and right->left.  If
              do_left_right_scans==false then the left/right passes are not executed.
              Skipping them makes the algorithm about 2x faster but might reduce the
              quality of the output.
    !*/
    );
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223

    register_hough_transform(m);

    m.def("normalize_image_gradients", normalize_image_gradients<numpy_image<double>>, py::arg("img1"), py::arg("img2"));
    m.def("normalize_image_gradients", normalize_image_gradients<numpy_image<float>>, py::arg("img1"), py::arg("img2"),
"requires \n\
    - img1 and img2 have the same dimensions. \n\
ensures \n\
    - This function assumes img1 and img2 are the two gradient images produced by a \n\
      function like sobel_edge_detector().  It then unit normalizes the gradient \n\
      vectors. That is, for all valid r and c, this function ensures that: \n\
        - img1[r][c]*img1[r][c] + img2[r][c]*img2[r][c] == 1  \n\
          unless both img1[r][c] and img2[r][c] were 0 initially, then they stay zero.");
    /*!
        requires
            - img1 and img2 have the same dimensions.
        ensures
            - This function assumes img1 and img2 are the two gradient images produced by a
              function like sobel_edge_detector().  It then unit normalizes the gradient
              vectors. That is, for all valid r and c, this function ensures that:
                - img1[r][c]*img1[r][c] + img2[r][c]*img2[r][c] == 1 
                  unless both img1[r][c] and img2[r][c] were 0 initially, then they stay zero.
    !*/


    m.def("remove_incoherent_edge_pixels", &py_remove_incoherent_edge_pixels, py::arg("line"), py::arg("horz_gradient"),
        py::arg("vert_gradient"), py::arg("angle_thresh"),
"requires \n\
    - horz_gradient and vert_gradient have the same dimensions. \n\
    - horz_gradient and vert_gradient represent unit normalized vectors.  That is, \n\
      you should have called normalize_image_gradients(horz_gradient,vert_gradient) \n\
      or otherwise caused all the gradients to have unit norm. \n\
    - for all valid i: \n\
        get_rect(horz_gradient).contains(line[i]) \n\
ensures \n\
    - This routine looks at all the points in the given line and discards the ones that \n\
      have outlying gradient directions.  To be specific, this routine returns a set \n\
      of points PTS such that:  \n\
        - for all valid i,j: \n\
            - The difference in angle between the gradients for PTS[i] and PTS[j] is  \n\
              less than angle_threshold degrees.   \n\
        - len(PTS) <= len(line) \n\
        - PTS is just line with some elements removed." );
    /*!
        requires
            - horz_gradient and vert_gradient have the same dimensions.
            - horz_gradient and vert_gradient represent unit normalized vectors.  That is,
              you should have called normalize_image_gradients(horz_gradient,vert_gradient)
              or otherwise caused all the gradients to have unit norm.
            - for all valid i:
                get_rect(horz_gradient).contains(line[i])
        ensures
            - This routine looks at all the points in the given line and discards the ones that
              have outlying gradient directions.  To be specific, this routine returns a set
              of points PTS such that: 
                - for all valid i,j:
                    - The difference in angle between the gradients for PTS[i] and PTS[j] is 
                      less than angle_threshold degrees.  
                - len(PTS) <= len(line)
                - PTS is just line with some elements removed.
    !*/

1224
    py::register_exception<no_convex_quadrilateral>(m, "no_convex_quadrilateral");
1225

1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
    m.def("extract_image_4points", &py_extract_image_4points<uint8_t>, py::arg("img"), py::arg("corners"), py::arg("rows"), py::arg("columns"));
    m.def("extract_image_4points", &py_extract_image_4points<uint16_t>, py::arg("img"), py::arg("corners"), py::arg("rows"), py::arg("columns"));
    m.def("extract_image_4points", &py_extract_image_4points<uint32_t>, py::arg("img"), py::arg("corners"), py::arg("rows"), py::arg("columns"));
    m.def("extract_image_4points", &py_extract_image_4points<uint64_t>, py::arg("img"), py::arg("corners"), py::arg("rows"), py::arg("columns"));
    m.def("extract_image_4points", &py_extract_image_4points<int8_t>, py::arg("img"), py::arg("corners"), py::arg("rows"), py::arg("columns"));
    m.def("extract_image_4points", &py_extract_image_4points<int16_t>, py::arg("img"), py::arg("corners"), py::arg("rows"), py::arg("columns"));
    m.def("extract_image_4points", &py_extract_image_4points<int32_t>, py::arg("img"), py::arg("corners"), py::arg("rows"), py::arg("columns"));
    m.def("extract_image_4points", &py_extract_image_4points<int64_t>, py::arg("img"), py::arg("corners"), py::arg("rows"), py::arg("columns"));
    m.def("extract_image_4points", &py_extract_image_4points<float>, py::arg("img"), py::arg("corners"), py::arg("rows"), py::arg("columns"));
    m.def("extract_image_4points", &py_extract_image_4points<double>, py::arg("img"), py::arg("corners"), py::arg("rows"), py::arg("columns"));
    m.def("extract_image_4points", &py_extract_image_4points<rgb_pixel>, py::arg("img"), py::arg("corners"), py::arg("rows"), py::arg("columns"),
"requires \n\
    - corners is a list of dpoint or line objects. \n\
    - len(corners) == 4 \n\
    - rows >= 0 \n\
    - columns >= 0 \n\
ensures \n\
    - The returned image has the given number of rows and columns. \n\
    - if (corners contains dpoints) then \n\
        - The 4 points in corners define a convex quadrilateral and this function \n\
          extracts that part of the input image img and returns it.  Therefore, \n\
          each corner of the quadrilateral is associated to a corner of the \n\
          extracted image and bilinear interpolation and a projective mapping is \n\
          used to transform the pixels in the quadrilateral into the output image. \n\
          To determine which corners of the quadrilateral map to which corners of \n\
          the returned image we fit the tightest possible rectangle to the \n\
          quadrilateral and map its vertices to their nearest rectangle corners. \n\
          These corners are then trivially mapped to the output image (i.e.  upper \n\
          left corner to upper left corner, upper right corner to upper right \n\
          corner, etc.). \n\
    - else \n\
1257
1258
1259
1260
1261
1262
        - This routine finds the 4 intersecting points of the given lines which \n\
          form a convex quadrilateral and uses them as described above to extract \n\
          an image.   i.e. It just then calls: extract_image_4points(img, \n\
          intersections_between_lines, rows, columns). \n\
        - If no convex quadrilateral can be made from the given lines then this \n\
          routine throws no_convex_quadrilateral." 
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
    /*!
        requires
            - corners is a list of dpoint or line objects.
            - len(corners) == 4
            - rows >= 0
            - columns >= 0
        ensures
            - The returned image has the given number of rows and columns.
            - if (corners contains dpoints) then
                - The 4 points in corners define a convex quadrilateral and this function
                  extracts that part of the input image img and returns it.  Therefore,
                  each corner of the quadrilateral is associated to a corner of the
                  extracted image and bilinear interpolation and a projective mapping is
                  used to transform the pixels in the quadrilateral into the output image.
                  To determine which corners of the quadrilateral map to which corners of
                  the returned image we fit the tightest possible rectangle to the
                  quadrilateral and map its vertices to their nearest rectangle corners.
                  These corners are then trivially mapped to the output image (i.e.  upper
                  left corner to upper left corner, upper right corner to upper right
                  corner, etc.).
            - else
1284
1285
1286
1287
1288
1289
                - This routine finds the 4 intersecting points of the given lines which
                  form a convex quadrilateral and uses them as described above to extract
                  an image.   i.e. It just then calls: extract_image_4points(img,
                  intersections_between_lines, rows, columns).
                - If no convex quadrilateral can be made from the given lines then this
                  routine throws no_convex_quadrilateral.
1290
1291
1292
    !*/
          );

1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328

    m.def("transform_image", &py_transform_image<uint8_t>, py::arg("img"), py::arg("map_point"), py::arg("rows"), py::arg("columns"));
    m.def("transform_image", &py_transform_image<uint16_t>, py::arg("img"), py::arg("map_point"), py::arg("rows"), py::arg("columns"));
    m.def("transform_image", &py_transform_image<uint32_t>, py::arg("img"), py::arg("map_point"), py::arg("rows"), py::arg("columns"));
    m.def("transform_image", &py_transform_image<uint64_t>, py::arg("img"), py::arg("map_point"), py::arg("rows"), py::arg("columns"));
    m.def("transform_image", &py_transform_image<int8_t>, py::arg("img"), py::arg("map_point"), py::arg("rows"), py::arg("columns"));
    m.def("transform_image", &py_transform_image<int16_t>, py::arg("img"), py::arg("map_point"), py::arg("rows"), py::arg("columns"));
    m.def("transform_image", &py_transform_image<int32_t>, py::arg("img"), py::arg("map_point"), py::arg("rows"), py::arg("columns"));
    m.def("transform_image", &py_transform_image<int64_t>, py::arg("img"), py::arg("map_point"), py::arg("rows"), py::arg("columns"));
    m.def("transform_image", &py_transform_image<float>, py::arg("img"), py::arg("map_point"), py::arg("rows"), py::arg("columns"));
    m.def("transform_image", &py_transform_image<double>, py::arg("img"), py::arg("map_point"), py::arg("rows"), py::arg("columns"));
    m.def("transform_image", &py_transform_image<rgb_pixel>, py::arg("img"), py::arg("map_point"), py::arg("rows"), py::arg("columns"),
"requires \n\
    - rows > 0 \n\
    - columns > 0 \n\
ensures \n\
    - Returns an image that is the given rows by columns in size and contains a \n\
      transformed part of img.  To do this, we interpret map_point as a mapping \n\
      from pixels in the returned image to pixels in the input img.  transform_image()  \n\
      uses this mapping and bilinear interpolation to fill the output image with an \n\
      interpolated copy of img.   \n\
    - Any locations in the output image that map to pixels outside img are set to 0." 
    /*!
        requires
            - rows > 0
            - columns > 0
        ensures
            - Returns an image that is the given rows by columns in size and contains a
              transformed part of img.  To do this, we interpret map_point as a mapping
              from pixels in the returned image to pixels in the input img.  transform_image() 
              uses this mapping and bilinear interpolation to fill the output image with an
              interpolated copy of img.  
            - Any locations in the output image that map to pixels outside img are set to 0.
    !*/
        );

1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
    m.def("max_point", &py_max_point<uint8_t>, py::arg("img"));
    m.def("max_point", &py_max_point<uint16_t>, py::arg("img"));
    m.def("max_point", &py_max_point<uint32_t>, py::arg("img"));
    m.def("max_point", &py_max_point<uint64_t>, py::arg("img"));
    m.def("max_point", &py_max_point<int8_t>, py::arg("img"));
    m.def("max_point", &py_max_point<int16_t>, py::arg("img"));
    m.def("max_point", &py_max_point<int32_t>, py::arg("img"));
    m.def("max_point", &py_max_point<int64_t>, py::arg("img"));
    m.def("max_point", &py_max_point<float>, py::arg("img"));
    m.def("max_point", &py_max_point<double>, py::arg("img"),
"requires \n\
    - m.size > 0 \n\
ensures \n\
    - returns the location of the maximum element of the array, that is, if the \n\
      returned point is P then it will be the case that: img[P.y,P.x] == img.max()." 
    /*!
        requires
            - m.size > 0
        ensures
            - returns the location of the maximum element of the array, that is, if the
              returned point is P then it will be the case that: img[P.y,P.x] == img.max().
    !*/
        );

    m.def("max_point_interpolated", &py_max_point_interpolated<uint8_t>, py::arg("img"));
    m.def("max_point_interpolated", &py_max_point_interpolated<uint16_t>, py::arg("img"));
    m.def("max_point_interpolated", &py_max_point_interpolated<uint32_t>, py::arg("img"));
    m.def("max_point_interpolated", &py_max_point_interpolated<uint64_t>, py::arg("img"));
    m.def("max_point_interpolated", &py_max_point_interpolated<int8_t>, py::arg("img"));
    m.def("max_point_interpolated", &py_max_point_interpolated<int16_t>, py::arg("img"));
    m.def("max_point_interpolated", &py_max_point_interpolated<int32_t>, py::arg("img"));
    m.def("max_point_interpolated", &py_max_point_interpolated<int64_t>, py::arg("img"));
    m.def("max_point_interpolated", &py_max_point_interpolated<float>, py::arg("img"));
    m.def("max_point_interpolated", &py_max_point_interpolated<double>, py::arg("img"),
"requires \n\
    - m.size > 0 \n\
ensures \n\
    - Like max_point(), this function finds the location in m with the largest \n\
      value.  However, we additionally use some quadratic interpolation to find the \n\
      location of the maximum point with sub-pixel accuracy.  Therefore, the \n\
      returned point is equal to max_point(m) + some small sub-pixel delta." 
    /*!
        requires
            - m.size > 0
        ensures
            - Like max_point(), this function finds the location in m with the largest
              value.  However, we additionally use some quadratic interpolation to find the
              location of the maximum point with sub-pixel accuracy.  Therefore, the
              returned point is equal to max_point(m) + some small sub-pixel delta.
    !*/
        );
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438

    m.def("zero_border_pixels", &py_zero_border_pixels<uint8_t>, py::arg("img"), py::arg("x_border_size"), py::arg("y_border_size"));
    m.def("zero_border_pixels", &py_zero_border_pixels<uint16_t>, py::arg("img"), py::arg("x_border_size"), py::arg("y_border_size"));
    m.def("zero_border_pixels", &py_zero_border_pixels<uint32_t>, py::arg("img"), py::arg("x_border_size"), py::arg("y_border_size"));
    m.def("zero_border_pixels", &py_zero_border_pixels<uint64_t>, py::arg("img"), py::arg("x_border_size"), py::arg("y_border_size"));
    m.def("zero_border_pixels", &py_zero_border_pixels<int8_t>, py::arg("img"), py::arg("x_border_size"), py::arg("y_border_size"));
    m.def("zero_border_pixels", &py_zero_border_pixels<int16_t>, py::arg("img"), py::arg("x_border_size"), py::arg("y_border_size"));
    m.def("zero_border_pixels", &py_zero_border_pixels<int32_t>, py::arg("img"), py::arg("x_border_size"), py::arg("y_border_size"));
    m.def("zero_border_pixels", &py_zero_border_pixels<int64_t>, py::arg("img"), py::arg("x_border_size"), py::arg("y_border_size"));
    m.def("zero_border_pixels", &py_zero_border_pixels<float>, py::arg("img"), py::arg("x_border_size"), py::arg("y_border_size"));
    m.def("zero_border_pixels", &py_zero_border_pixels<double>, py::arg("img"), py::arg("x_border_size"), py::arg("y_border_size"));
    m.def("zero_border_pixels", &py_zero_border_pixels<rgb_pixel>, py::arg("img"), py::arg("x_border_size"), py::arg("y_border_size"),
"requires \n\
    - x_border_size >= 0 \n\
    - y_border_size >= 0 \n\
ensures \n\
    - The size and shape of img isn't changed by this function. \n\
    - for all valid r such that r+y_border_size or r-y_border_size gives an invalid row \n\
        - for all valid c such that c+x_border_size or c-x_border_size gives an invalid column  \n\
            - assigns the pixel img[r][c] to 0.  \n\
              (i.e. assigns 0 to every pixel in the border of img)" 
    /*!
        requires
            - x_border_size >= 0
            - y_border_size >= 0
        ensures
            - The size and shape of img isn't changed by this function.
            - for all valid r such that r+y_border_size or r-y_border_size gives an invalid row
                - for all valid c such that c+x_border_size or c-x_border_size gives an invalid column 
                    - assigns the pixel img[r][c] to 0. 
                      (i.e. assigns 0 to every pixel in the border of img)
    !*/
        );

    m.def("zero_border_pixels", &py_zero_border_pixels2<uint8_t>, py::arg("img"), py::arg("inside"));
    m.def("zero_border_pixels", &py_zero_border_pixels2<uint16_t>, py::arg("img"), py::arg("inside"));
    m.def("zero_border_pixels", &py_zero_border_pixels2<uint32_t>, py::arg("img"), py::arg("inside"));
    m.def("zero_border_pixels", &py_zero_border_pixels2<uint64_t>, py::arg("img"), py::arg("inside"));
    m.def("zero_border_pixels", &py_zero_border_pixels2<int8_t>, py::arg("img"), py::arg("inside"));
    m.def("zero_border_pixels", &py_zero_border_pixels2<int16_t>, py::arg("img"), py::arg("inside"));
    m.def("zero_border_pixels", &py_zero_border_pixels2<int32_t>, py::arg("img"), py::arg("inside"));
    m.def("zero_border_pixels", &py_zero_border_pixels2<int64_t>, py::arg("img"), py::arg("inside"));
    m.def("zero_border_pixels", &py_zero_border_pixels2<float>, py::arg("img"), py::arg("inside"));
    m.def("zero_border_pixels", &py_zero_border_pixels2<double>, py::arg("img"), py::arg("inside"));
    m.def("zero_border_pixels", &py_zero_border_pixels2<rgb_pixel>, py::arg("img"), py::arg("inside"),
"ensures \n\
    - The size and shape of img isn't changed by this function. \n\
    - All the pixels in img that are not contained inside the inside rectangle \n\
      given to this function are set to 0.  That is, anything not \"inside\" is on \n\
      the border and set to 0." 
    /*!
        ensures
            - The size and shape of img isn't changed by this function.
            - All the pixels in img that are not contained inside the inside rectangle
              given to this function are set to 0.  That is, anything not "inside" is on
              the border and set to 0.
    !*/
        );

1439
1440
1441
}