/
uncanny.py
253 lines (174 loc) · 7.44 KB
/
uncanny.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
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
import numpy as np
import argparse
import matplotlib.pyplot as plt
from PIL import Image, ImageOps
from scipy import ndimage
import math
import cv2
from skimage.exposure import rescale_intensity
# perform convolution on an image with a given filter
def convolve(im, kernel):
imHeight = im.shape[1]
imWidth = im.shape[0]
kHeight = kernel.shape[1]
kWidth = kernel.shape[0]
# if the kernel has a dimension of 1 then we shouldn't subtract from the padding
if (kWidth == 1):
padx = 1
else:
padx = kWidth-1
if (kHeight == 1):
pady = 1
else:
pady = kHeight-1
# this adds zeros around the image so we can keep the original dimensions
im = cv2.copyMakeBorder(im, padx, padx, pady, pady, cv2.BORDER_CONSTANT, 0)
res = np.zeros( (imWidth, imHeight) )
# move the window along the x direction until it reaches the end
for x in np.arange(0, imWidth):
for y in np.arange(0, imHeight):
# current window of interest
window = im[x:x+padx+1, y:y+pady+1]
res[x, y] = (window * kernel).sum()
res = rescale_intensity( res, in_range=(0, 255) )
res = res * 255
return res
# construct a gaussian gradient kernel of a certain size
# return the spatially separated kernel of the gaussian derivative
def gaussian_kernel_d(sigma, sz):
rng = math.floor(sz/2)
y,x = np.ogrid[-rng:rng+1, -rng:rng+1]
normal = 1 / (2*np.pi*sigma**2)
dGx = -( x / sigma**2 ) * np.exp( -(x**2) / (2.0 * sigma**2) )
dGy = -( y / sigma**2 ) * np.exp( (y**2) / (2.0 * sigma**2) )
return dGx, dGy
# thins out edge lines
def non_max_spr(G, theta):
W, H = G.shape[:2]
res = np.zeros( (W, H) )
# convert angle matrix to degrees
theta = theta * 180.0 / np.pi
tolerance = 25
halfT = tolerance/2
for i in range(0, W-1):
for j in range(0, H-1):
try:
# these are the interpolated pixels on the line that are in front and behind the pixel being checked
q = 0
r = 0
# angle is around 0 degrees
if( (0 <= theta[i][j] < tolerance) or ( (180-tolerance) <= theta[i][j] < 180) ):
q = G[i, j+1]
r = G[i, j-1]
# angle is around 45 degrees
elif( 45-halfT <= theta[i][j] < 45+halfT ):
q = G[i+1][j-1]
r = G[i-1][j+1]
# angle is around 90 degrees
elif( 90-halfT <= theta[i][j] < 90+halfT ):
q = G[i+1][j]
r = G[i-1][j]
# angle is around 135
elif( 135-halfT <= theta[i][j] < 135+halfT ):
q = G[i-1][j-1]
r = G[i+1][j+1]
# if the pixel in question is greater than the iterpolated pixels then it will retain value
if( G[i,j] >= q and G[i,j] >= r ):
res[i][j] = G[i][j]
else:
res[i][j] = 0
except IndexError as err:
pass
return res
# determines which pixels matter the most
def thresholding(img, L, H):
M, N = img.shape[:2]
for i in range(0, M-1):
for j in range(0, N-1):
try:
# if it is in between the threshold values then check surrounding pixels
# if any of the surrounding pixels are greater than the high threshold then the current pixel can become apart of the strong edge
if(L <= img[i, j] and img[i, j] <= H):
if( (img[i+1, j-1] >= H) or (img[i+1, j] >= H) or (img[i+1, j+1] >= H)
or (img[i, j-1] >= H) or (img[i, j+1] >= H)
or (img[i-1, j-1] >= H) or (img[i-1, j] >= H) or (img[i-1, j+1] >= H) ):
img[i, j] = 255
else:
img[i, j] = 0
# if a pixel is below threshold then it can be discarded
elif(img[i, j] <= L):
img[i, j] = 0
elif(img[i, j] >= H):
img[i, j] = 255
except IndexError as err:
pass
return img
# calculate the R score based off the x and y derivatives of the image
def harris_scores(Ix, Iy, sig, k=0.05):
# compute the matrix M for each point (x,y)
Ixx = ndimage.gaussian_filter(Ix**2, sigma=sig)
Ixy = ndimage.gaussian_filter(Ix*Iy, sigma=sig)
Iyy = ndimage.gaussian_filter(Iy**2, sigma=sig)
detM = Ixx * Iyy - Ixy**2
trM = Ixx + Iyy
R = detM - k*(trM**2)
return R
# colour spots where the R score is high enough
def show_corners(R, img, rThr):
M, N = img.shape[:2]
res = np.zeros( (M,N) )
for i in range(0, M-1):
for j in range(0, N-1):
if(R[i][j] > rThr):
res[i][j] = 255
return res
def main():
parser = argparse.ArgumentParser()
# filname of image
parser.add_argument('-f', action='store', dest='fname', help='Image to detect edges on.', required=True)
# user inputs what sigma value they want to use for Gaussian kernel
parser.add_argument('-s', action='store', dest='sigma', type=float, help='Sigma value for the Gaussian kernel.', required=True)
# low value for hysteresis thresholding
parser.add_argument('-L', action='store', dest='low', type=int, help='Lower end of the threshold.', required=True)
# high value for hysteresis thresholding
parser.add_argument('-H', action='store', dest='high', type=int, help='Higher end of the threshold.', required=True)
# thredhold to determine if R score belongs to a corner or not
parser.add_argument('-R', action='store', dest='rval', type=int, help='Threshold for the R scores.', required=True)
# desired dimensions of the kernel
parser.add_argument('-S', action='store', dest='size', type=int, default=5, help='Size of the Gaussian kernel. Default is 5x5 kernel.')
args = parser.parse_args()
if(args.low >= args.high):
raise ValueError("High value threshold must be greater than low value!")
if(args.high > 255):
raise ValueError("High threshold cannot be greater than 255!")
if(args.low < 0):
raise ValueError("Low threshold cannot be lower than 0!")
img = Image.open(args.fname)
# need image to be gray scale for algorithm to work
img = np.asarray( ImageOps.grayscale(img) )
# get spatially separated kernel
dGx, dGy = gaussian_kernel_d(args.sigma, args.size)
# get x and y gradients
Ix = convolve(img, dGx)
Iy = convolve(img, dGy)
# get the R scores for the image
R = harris_scores(Ix, Iy, args.sigma)
# will colour the spots that are greater than the R threshold as white
corn = show_corners(R, img, args.rval)
# calclate the magnitudes of the gradient at each pixel
dG = np.sqrt(Ix**2 + Iy**2)
# this calculates the angle of the gradient at each pixel
angles = np.arctan2(Iy, Ix)
# we only want the largest value along the gradient to be visible
nm = non_max_spr(dG, angles)
corn_sup = non_max_spr(corn, angles)
# exacts the most important pixels based on the threshold values
thresholding(nm, args.low, args.high)
edgeImg = Image.fromarray(nm)
cornerImg = Image.fromarray(corn_sup)
# show edges
edgeImg.show()
# show corners
cornerImg.show()
if __name__ == '__main__':
main()