-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonmaximum_suppression.py
66 lines (61 loc) · 2.59 KB
/
nonmaximum_suppression.py
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
import numpy as np
class NonMaximumSuppression:
def __init__(self, magnitudes, angles):
self.magnitudes = magnitudes
self.angles = (angles + 180) % 180 # Convert range from [-180,180] to [0,180]
def binning(self):
"""
Returns an array of values in the range [0,3] responding to the direction of the edges
These values correspond to:
0: horizontal edges
1: positive diagonal edges (45 degrees)
2: vertical edges
3: negative diagonal edges (other diagonal)
"""
binned_arr = np.zeros_like(self.angles)
bins = [0, 45, 90, 135, 180]
for i in range(4):
binned_arr[
np.logical_and(self.angles >= bins[i], self.angles < bins[i + 1])
] = i
return binned_arr
def apply_suppression(self):
"""
Apply the nonmaximum suppression: suppress gradient values that are not the local maxima (edges)
For the border, it is common to set border values to 0
"""
suppressed = np.copy(self.magnitudes)
bins = self.binning()
for r in range(1, len(self.magnitudes) - 1):
for c in range(1, len(self.magnitudes[0]) - 1):
bin_val = bins[r][c]
if bin_val == 0: # Horizontal edge
if (
self.magnitudes[r][c] < self.magnitudes[r][c - 1]
or self.magnitudes[r][c] < self.magnitudes[r][c + 1]
):
suppressed[r][c] = 0
elif bin_val == 1: # Positive diagonal
if (
self.magnitudes[r][c] < self.magnitudes[r - 1][c - 1]
or self.magnitudes[r][c] < self.magnitudes[r + 1][c + 1]
):
suppressed[r][c] = 0
elif bin_val == 2: # Vertical edge
if (
self.magnitudes[r][c] < self.magnitudes[r - 1][c]
or self.magnitudes[r][c] < self.magnitudes[r + 1][c]
):
suppressed[r][c] = 0
else: # Negative diagonal
if (
self.magnitudes[r][c] < self.magnitudes[r - 1][c + 1]
or self.magnitudes[r][c] < self.magnitudes[r + 1][c - 1]
):
suppressed[r][c] = 0
# Set borders to 0
suppressed[0, :] = 0
suppressed[-1, :] = 0
suppressed[:, 0] = 0
suppressed[:, -1] = 0
return suppressed