-
Notifications
You must be signed in to change notification settings - Fork 24
/
cornerfeatures.jl
598 lines (477 loc) · 20.9 KB
/
cornerfeatures.jl
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
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
592
593
594
595
#=--------------------------------------------------------------------
cornerfeatures - Functions for the detection of 'corner' features
Copyright (c) 2015-2016 Peter Kovesi
peterkovesi.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The Software is provided "as is", without warranty of any kind.
PK August 2015
September 2018 updated for v0.7/v1.0
---------------------------------------------------------------------=#
export structuretensor, harris, noble, shi_tomasi, coherence
export hessianfeatures
export fastradial
import LinearAlgebra, Images
#----------------------------------------------------------------------
"""
harris - Harris corner detector
```
Usage: cimg = harris(img, sigma=1; k=0.04)
(cimg, r, c) = harris(img, sigma=1; k=0.04, args...)
Arguments:
img - Image to be processed.
::Array{T,2} or ::ImageMeta{T,2}
sigma - Standard deviation of Gaussian summation window.
Typical values to use might be 1-3. Default is 1.
Keyword arguments:
k - The scaling factor to apply to the trace of the
structure tensor. Defaults to the traditional value
of 0.04
args... - A sequence of keyword arguments for performing non-maximal suppression.
These are: (see nonmaxsuppts() for more details)
radius - Radius of region considered in non-maximal
suppression. Typical values to use might
be 1-3 pixels. Default (and minimum value) is 1.
thresh - Threshold, only features with value greater than
threshold are returned. Default is 0.
N - Maximum number of features to return. In this case the
N strongest features with value above 'thresh' are
returned. Default is Inf.
subpixel - If set to true features are localised to subpixel
precision. Default is false.
img - Optional image data. If an image is supplied the
detected corners are overlayed on this image. This can
be useful for parameter tuning. Default is [].
Returns:
cimg - Corner strength image.
r - Row coordinates of corner points.
c - Column coordinates of corner points.
```
If the input is an ImageMeta cimg will be an ImageMeta with
property spatialorder set to ["y","x"] so that it is consistent with
the coordinates returned in r, c
With none of the optional nonmaximal keyword arguments specified only
`cimg` is returned as a raw corner strength image. You may then want
to look at the values within 'cimg' to determine the appropriate
threshold value to use. Note that the Harris corner strength varies
with the intensity gradient raised to the 4th power. Small changes in
input image contrast result in huge changes in the appropriate
threshold.
The Harris measure is det(M) - k*trace^2(M), where k is a parameter
you have to set (traditionally k = 0.04) and M is the structure
tensor. Use Noble's measure if you wish to avoid the need to set a
parameter k. However the Shi-Tomasi measure is probably what you
really want.
See also: noble(), shi_tomasi(), hessianfeatures(), nonmaxsuppts(), derivative5()
"""
function harris(img::Array{T,2}, sigma::Real=1; k::Real=0.04, args...) where T <: Real
#=
References:
C.G. Harris and M.J. Stephens. "A combined corner and edge detector",
Proceedings Fourth Alvey Vision Conference, Manchester.
pp 147-151, 1988.
=#
(Ix2, Iy2, Ixy) = structuretensor(img, sigma)
cimg = (Ix2.*Iy2 .- Ixy.^2) .- k*(Ix2 + Iy2).^2
if isempty(args)
return cimg
else
(r,c) = nonmaxsuppts(cimg; args...)
return cimg, r, c
end
end
# Case for img::ImageMeta
function harris(img::Images.ImageMeta{T,2}, sigma::Real=1; k::Real=0.04, args...) where T <: Real
# Extract 2D float array in y x spatial order
(dimg, prop) = floatyx(img)
if isempty(args)
cimg = harris(dimg, sigma, k=k)
return Images.Image(cimg, prop)
else
(cimg, r, c) = harris(dimg, sigma, k=k; args...)
return Images.Image(cimg, prop), r, c
end
end
#----------------------------------------------------------------------
"""
noble - Noble's variant of the Harris corner detector
```
Usage: cimg = noble(img, sigma=1)
(cimg, r, c) = noble(img, sigma=1; args...)
Arguments:
img - Image to be processed.
::Array{T,2} or ::ImageMeta{T,2}
sigma - Standard deviation of Gaussian summation window.
Typical values to use might be 1-3. Default is 1.
Keyword arguments:
args... - A sequence of keyword arguments for performing non-maximal suppression.
These are: (see nonmaxsuppts() for more details)
radius - Radius of region considered in non-maximal
suppression. Typical values to use might
be 1-3 pixels. Default (and minimum value) is 1.
thresh - Threshold, only features with value greater than
threshold are returned. Default is 0.
N - Maximum number of features to return. In this case the
N strongest features with value above 'thresh' are
returned. Default is Inf.
subpixel - If set to true features are localised to subpixel
precision. Default is false.
img - Optional image data. If an image is supplied the
detected corners are overlayed on this image. This can
be useful for parameter tuning. Default is [].
Returns:
cimg - Corner strength image.
r - Row coordinates of corner points.
c - Column coordinates of corner points.
```
If the input is an ImageMeta cimg will be an ImageMeta with
property spatialorder set to ["y","x"] so that it is consistent with
the coordinates returned in r, c
With none of the optional keyword arguments specified only `cimg` is
returned as a raw corner strength image. You may then want to look at
the values within 'cimg' to determine the appropriate threshold value
to use.
Noble's variation of the Harris operator avoids the need to set the
parameter `k`. However the Shi-Tomasi measure is probably what you
really want.
See also: harris(), shi_tomasi(), hessianfeatures(), nonmaxsuppts(),
structuretensor(), derivative5()
"""
function noble(img::Array{T,2}, sigma::Real=1; args...) where T <: Real
#=
References:
C.G. Harris and M.J. Stephens. "A combined corner and edge detector",
Proceedings Fourth Alvey Vision Conference, Manchester.
pp 147-151, 1988.
Alison Noble, "Descriptions of Image Surfaces", PhD thesis, Department
of Engineering Science, Oxford University 1989, p45.
=#
(Ix2, Iy2, Ixy) = structuretensor(img, sigma)
cimg = (Ix2.*Iy2 .- Ixy.^2)./(Ix2 .+ Iy2 .+ eps())
if !isempty(args)
(r,c) = nonmaxsuppts(cimg; args...)
return cimg, r, c
else
return cimg
end
end
# Case for img::ImageMeta
function noble(img::Images.ImageMeta{T,2}, sigma::Real=1; args...) where T <: Real
# Extract 2D float array in y x spatial order
(dimg, prop) = floatyx(img)
if isempty(args)
cimg = noble(dimg, sigma)
return Images.Image(cimg, prop)
else
(cimg, r, c) = noble(dimg, sigma; args...)
return Images.Image(cimg, prop), r, c
end
end
#----------------------------------------------------------------------
"""
shi_tomasi - Shi - Tomasi corner detector
```
Usage: cimg = shi_tomasi(img, sigma=1)
(cimg, r, c) = shi_tomasi(img, sigma=1; args...)
Arguments:
img - Image to be processed.
::Array{T,2} or ::ImageMeta{T,2}
sigma - Standard deviation of Gaussian summation window.
Typical values to use might be 1-3. Default is 1.
Keyword arguments:
args... - A sequence of keyword arguments for performing non-maximal suppression.
These are: (see nonmaxsuppts() for more details)
radius - Radius of region considered in non-maximal
suppression. Typical values to use might
be 1-3 pixels. Default (and minimum value) is 1.
thresh - Threshold, only features with value greater than
threshold are returned. Default is 0.
N - Maximum number of features to return. In this case the
N strongest features with value above 'thresh' are
returned. Default is Inf.
subpixel - If set to true features are localised to subpixel
precision. Default is false.
img - Optional image data. If an image is supplied the
detected corners are overlayed on this image. This can
be useful for parameter tuning. Default is [].
Returns:
cimg - Corner strength image.
r - Row coordinates of corner points.
c - Column coordinates of corner points.
```
If the input is an ImageMeta cimg will be an ImageMeta with
property spatialorder set to ["y","x"] so that it is consistent with
the coordinates returned in r, c
With no keyword arguments supplied only 'cimg' is returned as a raw
corner strength image. You may then want to look at the values within
'cimg' to determine the appropriate threshold value to use.
The Shi - Tomasi measure returns the minimum eigenvalue of the
structure tensor. This represents the ideal that the Harris and Noble
detectors attempt to approximate. Back in 1988 Harris wanted to avoid
the computational cost of taking a square root when computing
features. This is no longer relevant today!
See also: harris(), noble(), hessianfeatures(), structuretensor(),
nonmaxsuppts(), derivative5()
"""
function shi_tomasi(img::Array{T1,2}, sigma::Real=1; args...) where T1 <: Real
#=
Reference:
J. Shi and C. Tomasi. "Good Features to Track,". 9th IEEE Conference on
Computer Vision and Pattern Recognition. 1994.
=#
(Ix2, Iy2, Ixy) = structuretensor(img, sigma)
T = Ix2 .+ Iy2 # trace
D = Ix2.*Iy2 .- Ixy.^2 # determinant
# The two eigenvalues of the 2x2 structure tensor are:
# L1 = T/2 + sqrt(T.^2/4 - D)
# L2 = T/2 - sqrt(T.^2/4 - D)
# We just want the minimum eigenvalue
cimg = T/2 .- sqrt.(T.^2/4 .- D .+ eps())
if !isempty(args)
(r,c) = nonmaxsuppts(cimg; args...)
return cimg, r, c
else
return cimg
end
end
# Case for img::ImageMeta
function shi_tomasi(img::Images.ImageMeta{T,2}, sigma::Real=1; args...) where T <: Real
# Extract 2D float array in y x spatial order
(dimg, prop) = floatyx(img)
if isempty(args)
cimg = shi_tomasi(dimg, sigma)
return Images.Image(cimg, prop)
else
(cimg, r, c) = shi_tomasi(dimg, sigma; args...)
return Images.Image(cimg, prop), r, c
end
end
#----------------------------------------------------------------------
"""
coherence - Compute image coherence from structure tensor
```
Usage: cimg = coherence(img, sigma=1)
Arguments:
img - Image to be processed.
::Array{T,2} or ::ImageMeta{T,2}
sigma - Standard deviation of Gaussian summation window.
Typical values to use might be 1-3. Default is 1.
Returns:
cimg - Coherence image.
```
Coherence is defined as the difference between the eigenvalues of the
structure tensor divided by the sum of the eigenvalues, all squared.
```
((L1-L2)./(L1+L2)).^2
```
A small value indicates that the eigenvalues are similar in magnitude
and hence the local structure has a strong 2D component.
If the input is an ImageMeta cimg will be an ImageMeta with
property spatialorder set to ["y","x"].
See also: structuretensor()
"""
function coherence(img::Array{T1,2}, sigma::Real=1) where T1 <: Real
(Ix2, Iy2, Ixy) = structuretensor(img, sigma)
T = Ix2 .+ Iy2 # trace
D = Ix2.*Iy2 .- Ixy.^2 # determinant
# The two eigenvalues of the 2x2 structure tensor are:
L1 = T/2 .+ sqrt(T.^2/4 .- D)
L2 = T/2 .- sqrt(T.^2/4 .- D)
# Coherence is defined as ((L1-L2)./(L1+L2)).^2
# this can be reduced to the following
return 4*(T.^2/4 .- D) ./ T.^2
end
# Case for img::ImageMeta
function coherence(img::Images.ImageMeta{T,2}, sigma::Real=1) where T <: Real
# Extract 2D float array in y x spatial order
(dimg, prop) = floatyx(img)
cimg = coherence(dimg, sigma)
return Images.Image(cimg, prop)
end
#----------------------------------------------------------------------
"""
structuretensor - Compute structure tensor values over an image
```
Usage: (Ix2, Iy2, Ixy) = structuretensor(img, sigma=1)
Arguments:
img - Image to be processed ::Array{T,2}
sigma - Standard deviation of Gaussian summation window. Typical
values to use might be 1-3. Default is 1.
Returns:
Ix2 - 2nd moment of gradients in x
Iy2 - 2nd moment of gradients in y
Ixy - 2nd moment of gradients in xy
```
See also: shi_tomasi(), harris(), noble(), coherence(), derivative5()
"""
function structuretensor(img::Array{T,2}, sigma::Real=1) where T <: Real
# Convert to float if needed
if isa(img[1,1], Integer)
fimg = float(img)
else
fimg = img
end
# Compute derivatives and elements of the structure tensor.
(Ix, Iy) = derivative5(fimg, ("x", "y"))
Ix2 = gaussfilt(Ix.^2, sigma)
Iy2 = gaussfilt(Iy.^2, sigma)
Ixy = gaussfilt(Ix.*Iy, sigma)
return Ix2, Iy2, Ixy
end
#----------------------------------------------------------------------
"""
hessianfeatures - Computes determiant of hessian features in an image.
```
Usage: hdet = hessianfeatures(img, sigma)
Arguments:
img - Greyscale image to be processed.
::Array{T,2} or ::ImageMeta{T,2}
sigma - Defines smoothing scale.
Returns: hdet - Matrix of determinants of Hessian
```
The local maxima of hdet tend to mark the centres of dark or light blobs.
However, the point that gets localised can be dependent on scale. If the
blobs you wish to detect are large you will need to use a value of sigma that
is comparable in magnitude.
The local minima of hdet is useful for marking the intersection points of a
camera calibration checkerboard pattern. These saddle features seem to be
more stable under scale variations.
For example to get the 100 strongest saddle features in image `img` use:
```
> hdet = hessianfeatures(img, 1) # sigma = 1
> (r, c) = nonmaxsuppts(-hdet, N=100)
```
If the input is an ImageMeta cimg will be an ImageMeta with
property spatialorder set to ["y","x"].
See also: harris(), noble(), shi_tomasi(), derivative5(), nonmaxsuppts()
"""
function hessianfeatures(img::Array{T,2}, sigma::Real=1) where T <: Real
if sigma > 0 # Convolve with Gaussian at desired sigma
Gimg = gaussfilt(img, sigma)
else # No smoothing
Gimg = img
sigma = 1 # Needed for normalisation later
end
# Take 2nd derivatives in x and y
(Lxx, Lxy, Lyy) = derivative5(Gimg, ("xx", "xy", "yy"))
# Apply normalizing scaling factor of sigma^2 to 2nd derivatives
Lxx = Lxx*sigma^2
Lyy = Lyy*sigma^2
Lxy = Lxy*sigma^2
# Determinant
return hdet = Lxx.*Lyy .- Lxy.^2
end
# Case for img::ImageMeta
function hessianfeatures(img::Images.ImageMeta{T,2}, sigma::Real=1) where T <: Real
# Extract 2D float array in y x spatial order
(dimg, prop) = floatyx(img)
hdet = hessianfeatures(dimg, sigma)
return Images.Image(cimg, prop)
end
#----------------------------------------------------------------------
"""
fastradial - Loy and Zelinski's fast radial feature detector
```
Usage: (S, So) = fastradial(img, radii, alpha=2, beta=0)
Arguments:
img - Image to be analysed
::Array{T,2} or ::ImageMeta{T,2}
radii - Vector of integer radius values to be processed
suggested radii might be [1, 3, 5]
alpha - Radial strictness parameter.
1 - slack, accepts features with bilateral symmetry.
2 - a reasonable compromise.
3 - strict, only accepts radial symmetry.
... and you can go higher
beta - Gradient threshold. Gradients below this threshold do
not contribute to symmetry measure, defaults to 0.
Returns S - Symmetry map. Bright points with high symmetry are
marked with large positive values. Dark points of
high symmetry marked with large -ve values.
So - Symmetry map based on orientation only.
```
To localize points use nonmaxsuppts() on S, -S or abs(S) depending on
what you are seeking to find.
A good algorithm for detecting eyes in face images.
If the input is an ImageMeta cimg will be an ImageMeta with
property spatialorder set to ["y","x"].
Reference:
Loy, G. Zelinsky, A. Fast radial symmetry for detecting points of
interest. IEEE PAMI, Vol. 25, No. 8, August 2003. pp 959-973.
"""
function fastradial(img::Array{T,2}, radii::Vector, alpha::Real=2, beta::Real=0) where T <: Real
if !isa(radii, Vector{Int}) && !isa(radii, Int) || minimum(radii) < 1
error("radii must be a vector of integers and > 1")
end
(rows,cols) = size(img)
# Compute derivatives in x and y via Farid and Simoncelli's 5 tap
# derivative filters
(imgx, imgy) = derivative5(img, ("x", "y"))
mag = sqrt.(imgx.^2 .+ imgy.^2) .+ eps() # (+eps to avoid division by 0)
# Normalise gradient values so that [imgx imgy] form unit
# direction vectors.
imgx = imgx./mag
imgy = imgy./mag
S = zeros(rows,cols) # Symmetry matrix
So = zeros(rows,cols) # Orientation only symmetry matrix
for n in radii
M = zeros(rows,cols) # Magnitude projection image
O = zeros(rows,cols) # Orientation projection image
# Form the orientation and magnitude projection matrices
for r = 1:rows
for c = 1:cols
if mag[r,c] > beta
# Coordinates of 'positively' and 'negatively' affected pixels
posx = c + round(Int, n*imgx[r,c])
posy = r + round(Int, n*imgy[r,c])
negx = c - round(Int, n*imgx[r,c])
negy = r - round(Int, n*imgy[r,c])
# clamp to image limits
posx = max(min(posx,cols),1)
posy = max(min(posy,rows),1)
negx = max(min(negx,cols),1)
negy = max(min(negy,rows),1)
O[posy, posx] += 1
O[negy, negx] -= 1
M[posy, posx] += mag[r,c]
M[negy, negx] -= mag[r,c]
end
end
end
# Clamp Orientation projection matrix values to a maximum of
# +/-kappa, but first set the normalization parameter kappa to the
# values suggested by Loy and Zelinski
if n == 1
kappa = 8
else
kappa = 9.9
end
O[O .> kappa] .= kappa
O[O .< -kappa] .= -kappa
# Unsmoothed symmetry measure at this radius value
F = M./kappa .* (abs.(O)/kappa).^alpha
Fo = sign.(O) .* (abs.(O)/kappa).^alpha # Orientation only based measure
# Smooth and spread the symmetry measure with a Gaussian
# proportional to n. Also scale the smoothed result by n so
# that large scales do not lose their relative weighting.
# Note we use the function gaussfilt because it seems that
# small sigma values (< 1) cause problems for
# Images.imfilt_gaussian()
# ** But if sigma < 1 do we need to smooth anyway? **
S += gaussfilt(F, 0.25*n) * n
So += gaussfilt(Fo, 0.25*n) * n
end # for each radius
S = S /length(radii) # Average
So = So/length(radii)
return S, So
end
# Case for img::ImageMeta
function fastradial(img::Images.ImageMeta{T,2}, radii::Vector, alpha::Real=2, beta::Real=0) where T <: Real
# Extract 2D float array in y x spatial order
(dimg, prop) = floatyx(img)
(S, So) = fastradial(dimg, radii, alpha, beta)
return Images.Image(S, prop), Images.Image(So, prop)
end