-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcore.py
176 lines (140 loc) · 5.16 KB
/
core.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
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
""" Canny Edge Detection is based on the following five steps:
1. Gaussian filter
2. Gradient Intensity
3. Non-maximum suppression
4. Double threshold
5. Edge tracking
This module contains these five steps as five separate Python functions.
"""
# Module imports
from CannyEdge.utils import round_angle
# Third party imports
from scipy.ndimage.filters import gaussian_filter
from scipy import ndimage
from scipy.ndimage import sobel, generic_gradient_magnitude, generic_filter
import numpy as np
def gs_filter(img, sigma):
""" Step 1: Gaussian filter
Args:
img: Numpy ndarray of image
sigma: Smoothing parameter
Returns:
Numpy ndarray of smoothed image
"""
if type(img) != np.ndarray:
raise TypeError('Input image must be of type ndarray.')
else:
return gaussian_filter(img, sigma)
def gradient_intensity(img):
""" Step 2: Find gradients
Args:
img: Numpy ndarray of image to be processed (denoised image)
Returns:
G: gradient-intensed image
D: gradient directions
"""
# Kernel for Gradient in x-direction
Kx = np.array(
[[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], np.int32
)
# Kernel for Gradient in y-direction
Ky = np.array(
[[1, 2, 1], [0, 0, 0], [-1, -2, -1]], np.int32
)
# Apply kernels to the image
Ix = ndimage.filters.convolve(img, Kx)
Iy = ndimage.filters.convolve(img, Ky)
# return the hypothenuse of (Ix, Iy)
G = np.hypot(Ix, Iy)
D = np.arctan2(Iy, Ix)
return (G, D)
def suppression(img, D):
""" Step 3: Non-maximum suppression
Args:
img: Numpy ndarray of image to be processed (gradient-intensed image)
D: Numpy ndarray of gradient directions for each pixel in img
Returns:
...
"""
M, N = img.shape
Z = np.zeros((M,N), dtype=np.int32)
for i in range(M):
for j in range(N):
# find neighbour pixels to visit from the gradient directions
where = round_angle(D[i, j])
try:
if where == 0:
if (img[i, j] >= img[i, j - 1]) and (img[i, j] >= img[i, j + 1]):
Z[i,j] = img[i,j]
elif where == 90:
if (img[i, j] >= img[i - 1, j]) and (img[i, j] >= img[i + 1, j]):
Z[i,j] = img[i,j]
elif where == 135:
if (img[i, j] >= img[i - 1, j - 1]) and (img[i, j] >= img[i + 1, j + 1]):
Z[i,j] = img[i,j]
elif where == 45:
if (img[i, j] >= img[i - 1, j + 1]) and (img[i, j] >= img[i + 1, j - 1]):
Z[i,j] = img[i,j]
except IndexError as e:
""" Todo: Deal with pixels at the image boundaries. """
pass
return Z
def threshold(img, t, T):
""" Step 4: Thresholding
Iterates through image pixels and marks them as WEAK and STRONG edge
pixels based on the threshold values.
Args:
img: Numpy ndarray of image to be processed (suppressed image)
t: lower threshold
T: upper threshold
Return:
img: Thresholdes image
"""
# define gray value of a WEAK and a STRONG pixel
cf = {
'WEAK': np.int32(50),
'STRONG': np.int32(255),
}
# get strong pixel indices
strong_i, strong_j = np.where(img > T)
# get weak pixel indices
weak_i, weak_j = np.where((img >= t) & (img <= T))
# get pixel indices set to be zero
zero_i, zero_j = np.where(img < t)
# set values
img[strong_i, strong_j] = cf.get('STRONG')
img[weak_i, weak_j] = cf.get('WEAK')
img[zero_i, zero_j] = np.int32(0)
return (img, cf.get('WEAK'))
def tracking(img, weak, strong=255):
""" Step 5:
Checks if edges marked as weak are connected to strong edges.
Note that there are better methods (blob analysis) to do this,
but they are more difficult to understand. This just checks neighbour
edges.
Also note that for perfomance reasons you wouldn't do this kind of tracking
in a seperate loop, you would do it in the loop of the tresholding process.
Since this is an **educational** implementation ment to generate plots
to help people understand the major steps of the Canny Edge algorithm,
we exceptionally don't care about perfomance here.
Args:
img: Numpy ndarray of image to be processed (thresholded image)
weak: Value that was used to mark a weak edge in Step 4
Returns:
final Canny Edge image.
"""
M, N = img.shape
for i in range(M):
for j in range(N):
if img[i, j] == weak:
# check if one of the neighbours is strong (=255 by default)
try:
if ((img[i + 1, j] == strong) or (img[i - 1, j] == strong)
or (img[i, j + 1] == strong) or (img[i, j - 1] == strong)
or (img[i+1, j + 1] == strong) or (img[i-1, j - 1] == strong)):
img[i, j] = strong
else:
img[i, j] = 0
except IndexError as e:
pass
return img