This file is indexed.

/usr/lib/python3/dist-packages/arrayfire/vision.py is in python3-arrayfire 3.3.20160624-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################

"""
Computer vision functions (FAST, ORB, etc)
"""

from .library import *
from .array import *
from .features import *

def fast(image, threshold=20.0, arc_length=9, non_max=True, feature_ratio=0.05, edge=3):
    """
    FAST feature detector.

    Parameters
    ----------

    image         : af.Array
                  A 2D array representing an image.

    threshold     : scalar. optional. default: 20.0.
                  FAST threshold for which a pixel of the circle around a central pixel is consdered.

    arc_length    : scalar. optional. default: 9
                  The minimum length of arc length to be considered. Max length should be 16.

    non_max       : Boolean. optional. default: True
                  A boolean flag specifying if non max suppression has to be performed.

    feature_ratio : scalar. optional. default: 0.05 (5%)
                  Specifies the maximum ratio of features to pixels in the image.

    edge          : scalar. optional. default: 3.
                  Specifies the number of edge rows and columns to be ignored.

    Returns
    ---------
    features     : af.Features()
                 Contains the location and score. Orientation and size are not computed.

    """
    out = Features()
    safe_call(backend.get().af_fast(ct.pointer(out.feat),
                                    image.arr, ct.c_float(threshold), ct.c_uint(arc_length), non_max,
                                    ct.c_float(feature_ratio), ct.c_uint(edge)))
    return out

def harris(image, max_corners=500, min_response=1E5, sigma=1.0, block_size=0, k_thr=0.04):
    """
    Harris corner detector.

    Parameters
    ----------
    image         : af.Array
                  A 2D array specifying an image.

    max_corners   : scalar. optional. default: 500.
                  Specifies the maximum number of corners to be calculated.

    min_response  : scalar. optional. default: 1E5
                  Specifies the cutoff score for a corner to be considered

    sigma         : scalar. optional. default: 1.0
                  - Specifies the standard deviation of a circular window.
                  - Only used when block_size == 0. Must be >= 0.5 and <= 5.0.

    block_size    : scalar. optional. default: 0
                  Specifies the window size.

    k_thr         : scalar. optional. default: 0.04
                  Harris constant. must be >= 0.01

    Returns
    ---------

    features     : af.Features()
                 Contains the location and score. Orientation and size are not computed.

    Note
    ------

    The covariation matrix will be square when `block_size` is used and circular when `sigma` is used.


    """
    out = Features()
    safe_call(backend.get().af_harris(ct.pointer(out.feat),
                                      image.arr, ct.c_uint(max_corners), ct.c_float(min_response),
                                      ct.c_float(sigma), ct.c_uint(block_size), ct.c_float(k_thr)))
    return out

def orb(image, threshold=20.0, max_features=400, scale = 1.5, num_levels = 4, blur_image = False):
    """
    ORB Feature descriptor.

    Parameters
    ----------

    image         : af.Array
                  A 2D array representing an image.

    threshold     : scalar. optional. default: 20.0.
                  FAST threshold for which a pixel of the circle around a central pixel is consdered.

    max_features  : scalar. optional. default: 400.
                  Specifies the maximum number of features to be considered.

    scale         : scalar. optional. default: 1.5.
                  Specifies the factor by which images are down scaled at each level.

    num_levles    : scalar. optional. default: 4.
                  Specifies the number of levels used in the image pyramid.

    blur_image    : Boolean. optional. default: False.
                  Flag specifying if the input has to be blurred before computing descriptors.
                  A gaussian filter with sigma = 2 is applied if True.


    Returns
    ---------
    (features, descriptor)     : tuple of (af.Features(), af.Array)
                               - descriptor is an af.Array of size N x 8

    """
    feat = Features()
    desc = Array()
    safe_call(backend.get().af_orb(ct.pointer(feat.feat), ct.pointer(desc.arr),
                                   ct.c_float(threshold), ct.c_uint(max_features),
                                   ct.c_float(scale), ct.c_uint(num_levels), blur_image))
    return feat, desc

def hamming_matcher(query, database, dim = 0, num_nearest = 1):
    """
    Hamming distance matcher.

    Parameters
    -----------

    query    : af.Array
             A query feature descriptor

    database : af.Array
             A multi dimensional array containing the feature descriptor database.

    dim      : scalar. optional. default: 0.
             Specifies the dimension along which feature descriptor lies.

    num_nearest: scalar. optional. default: 1.
             Specifies the number of nearest neighbors to find.

    Returns
    ---------

    (location, distance): tuple of af.Array
                          location and distances of closest matches.

    """
    index = Array()
    dist = Array()
    safe_call(backend.get().af_hamming_matcher(ct.pointer(idx.arr), ct.pointer(dist.arr),
                                               query.arr, database.arr,
                                               c_dim_t(dim), c_dim_t(num_nearest)))
    return index, dist

def nearest_neighbour(query, database, dim = 0, num_nearest = 1, match_type=MATCH.SSD):
    """
    Nearest Neighbour matcher.

    Parameters
    -----------

    query    : af.Array
             A query feature descriptor

    database : af.Array
             A multi dimensional array containing the feature descriptor database.

    dim      : scalar. optional. default: 0.
             Specifies the dimension along which feature descriptor lies.

    num_nearest: scalar. optional. default: 1.
             Specifies the number of nearest neighbors to find.

    match_type: optional: af.MATCH. default: af.MATCH.SSD
             Specifies the match function metric.

    Returns
    ---------

    (location, distance): tuple of af.Array
                          location and distances of closest matches.

    """
    index = Array()
    dist = Array()
    safe_call(backend.get().af_nearest_neighbour(ct.pointer(idx.arr), ct.pointer(dist.arr),
                                                 query.arr, database.arr,
                                                 c_dim_t(dim), c_dim_t(num_nearest),
                                                 match_type.value))
    return index, dist

def match_template(image, template, match_type = MATCH.SAD):
    """
    Find the closest match of a template in an image.

    Parameters
    ----------

    image    : af.Array
             A multi dimensional array specifying an image or batch of images.

    template : af.Array
             A multi dimensional array specifying a template or batch of templates.

    match_type: optional: af.MATCH. default: af.MATCH.SAD
             Specifies the match function metric.

    Returns
    --------
    out     : af.Array
            An array containing the score of the match at each pixel.

    """
    out = Array()
    safe_call(backend.get().af_match_template(ct.pointer(out.arr),
                                              image.arr, template.arr,
                                              match_type.value))
    return out

def susan(image, radius=3, diff_thr=32, geom_thr=10, feature_ratio=0.05, edge=3):
    """
    SUSAN corner detector.

    Parameters
    ----------
    image         : af.Array
                  A 2D array specifying an image.

    radius        : scalar. optional. default: 500.
                  Specifies the radius of each pixel neighborhood.

    diff_thr      : scalar. optional. default: 1E5
                  Specifies the intensity difference threshold.

    geom_thr      : scalar. optional. default: 1.0
                  Specifies the geometric threshold.

    feature_ratio : scalar. optional. default: 0.05 (5%)
                  Specifies the ratio of corners found to number of pixels.

    edge         : scalar. optional. default: 3
                  Specifies the number of edge rows and columns that are ignored.

    Returns
    ---------

    features     : af.Features()
                 Contains the location and score. Orientation and size are not computed.

    """
    out = Features()
    safe_call(backend.get().af_susan(ct.pointer(out.feat),
                                     image.arr, ct.c_uint(radius), ct.c_float(diff_thr),
                                     ct.c_float(geom_thr), ct.c_float(feature_ratio),
                                     ct.c_uint(edge)))
    return out

def dog(image, radius1, radius2):
    """
    Difference of gaussians.

    Parameters
    ----------
    image    : af.Array
             A 2D array specifying an image.

    radius1  : scalar.
             The radius of first gaussian kernel.

    radius2  : scalar.
             The radius of second gaussian kernel.


    Returns
    --------

    out      : af.Array
             A multi dimensional array containing the difference of gaussians.

    Note
    ------

    The sigma values are calculated to be 0.25 * radius.
    """

    out = Array()
    safe_call(backend.get().af_dog(ct.pointer(out.arr),
                                   image.arr, radius1, radius2))
    return out

def sift(image, num_layers=3, contrast_threshold=0.04, edge_threshold=10.0, initial_sigma = 1.6,
         double_input = True, intensity_scale = 0.00390625, feature_ratio = 0.05):
    """
    SIFT feature detector and descriptor.

    Parameters
    ----------
    image              : af.Array
                       A 2D array representing an image

    num_layers         : optional: integer. Default: 3
                       Number of layers per octave. The number of octaves is calculated internally.

    contrast_threshold : optional: float. Default: 0.04
                       Threshold used to filter out features that have low contrast.

    edge_threshold     : optional: float. Default: 10.0
                       Threshold used to filter out features that are too edge-like.

    initial_sigma      : optional: float. Default: 1.6
                       The sigma value used to filter the input image at the first octave.

    double_input       : optional: bool. Default: True
                       If True, the input image will be scaled to double the size for the first octave.

    intensity_scale    : optional: float. Default: 1.0/255
                       The inverse of the difference between maximum and minimum intensity values.

    feature_ratio      : optional: float. Default: 0.05
                       Specifies the maximum number of features to detect as a ratio of image pixels.

    Returns
    --------
    (features, descriptor)     : tuple of (af.Features(), af.Array)
                               - descriptor is an af.Array of size N x 128

    """

    feat = Features()
    desc = Array()
    safe_call(af_sift(ct.pointer(feat), ct.pointer(desc),
                      image.arr, num_layers, contrast_threshold, edge_threshold,
                      initial_sigma, double_input, intensity_scale, feature_ratio))

    return (feat, desc)

def gloh(image, num_layers=3, contrast_threshold=0.04, edge_threshold=10.0, initial_sigma = 1.6,
         double_input = True, intensity_scale = 0.00390625, feature_ratio = 0.05):
    """
    GLOH feature detector and descriptor.

    Parameters
    ----------
    image              : af.Array
                       A 2D array representing an image

    num_layers         : optional: integer. Default: 3
                       Number of layers per octave. The number of octaves is calculated internally.

    contrast_threshold : optional: float. Default: 0.04
                       Threshold used to filter out features that have low contrast.

    edge_threshold     : optional: float. Default: 10.0
                       Threshold used to filter out features that are too edge-like.

    initial_sigma      : optional: float. Default: 1.6
                       The sigma value used to filter the input image at the first octave.

    double_input       : optional: bool. Default: True
                       If True, the input image will be scaled to double the size for the first octave.

    intensity_scale    : optional: float. Default: 1.0/255
                       The inverse of the difference between maximum and minimum intensity values.

    feature_ratio      : optional: float. Default: 0.05
                       Specifies the maximum number of features to detect as a ratio of image pixels.

    Returns
    --------
    (features, descriptor)     : tuple of (af.Features(), af.Array)
                               - descriptor is an af.Array of size N x 272

    """

    feat = Features()
    desc = Array()
    safe_call(af_gloh(ct.pointer(feat), ct.pointer(desc),
                      image.arr, num_layers, contrast_threshold, edge_threshold,
                      initial_sigma, double_input, intensity_scale, feature_ratio))

    return (feat, desc)

def homography(x_src, y_src, x_dst, y_dst, htype = HOMOGRAPHY.RANSAC,
               ransac_threshold = 3.0, iters = 1000, out_type = Dtype.f32):
    """
    Homography estimation

    Parameters
    ----------
    x_src            :  af.Array
                     A list of x co-ordinates of the source points.

    y_src            :  af.Array
                     A list of y co-ordinates of the source points.

    x_dst            :  af.Array
                     A list of x co-ordinates of the destination points.

    y_dst            :  af.Array
                     A list of y co-ordinates of the destination points.

    htype            : optional: af.HOMOGRAPHY. Default: HOMOGRAPHY.RANSAC
                     htype can be one of
                         - HOMOGRAPHY.RANSAC: RANdom SAmple Consensus will be used to evaluate quality.
                         - HOMOGRAPHY.LMEDS : Least MEDian of Squares is used to evaluate quality.

    ransac_threshold : optional: scalar. Default: 3.0
                     If `htype` is HOMOGRAPHY.RANSAC, it specifies the L2-distance threshold for inliers.

    out_type         : optional. af.Dtype. Default: Dtype.f32.
                     Specifies the output data type.

    Returns
    -------
    (H, inliers)     : A tuple of (af.Array, integer)
    """

    H = Array()
    inliers = ct.c_int(0)
    safe_call(backend.get().af_homography(ct.pointer(H), ct.pointer(inliers),
                                          x_src.arr, y_src.arr, x_dst.arr, y_dst.arr,
                                          htype.value, ransac_threshold, iters, out_type.value))
    return (H, inliers)