/
canny.py
240 lines (212 loc) · 7.34 KB
/
canny.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
'''
Module for Canny edge detection
Requirements: 1.scipy.(numpy is also mandatory, but it is assumed to be
installed with scipy)
2. Python Image Library (only for viewing the final image.)
Author: Vishwanath
contact: vishwa.hyd@gmail.com
'''
try:
import Image
except ImportError:
print 'PIL not found. You cannot view the image'
import os
from scipy import *
from scipy.ndimage import *
from scipy.signal import convolve2d as conv
import os
###########################################################################
## Handout painting code.
###########################################################################
from PIL import Image
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import random
import time
import matplotlib.image as mpimg
import scipy.misc
def canny(im, sigma, thresHigh = 40, thresLow = 10):
'''
Takes an input image in the range [0, 1] and generate a gradient image
with edges marked by 1 pixels.
'''
imin = im.copy() * 255.0
# Create the gauss kernel for blurring the input image
# It will be convolved with the image
# wsize should be an odd number
wsize = 5
gausskernel = gaussFilter(sigma, window = wsize)
# fx is the filter for vertical gradient
# fy is the filter for horizontal gradient
# Please note the vertical direction is positive X
fx = createFilter([0, 1, 0,
0, 0, 0,
0, -1, 0])
fy = createFilter([ 0, 0, 0,
-1, 0, 1,
0, 0, 0])
imout = conv(imin, gausskernel, 'valid')
# print "imout:", imout.shape
gradxx = conv(imout, fx, 'valid')
gradyy = conv(imout, fy, 'valid')
gradx = np.zeros(im.shape)
grady = np.zeros(im.shape)
padx = (imin.shape[0] - gradxx.shape[0]) / 2.0
pady = (imin.shape[1] - gradxx.shape[1]) / 2.0
gradx[padx:-padx, pady:-pady] = gradxx
grady[padx:-padx, pady:-pady] = gradyy
# Net gradient is the square root of sum of square of the horizontal
# and vertical gradients
grad = hypot(gradx, grady)
theta = arctan2(grady, gradx)
theta = 180 + (180 / pi) * theta
# Only significant magnitudes are considered. All others are removed
xx, yy = where(grad < 10)
theta[xx, yy] = 0
grad[xx, yy] = 0
# The angles are quantized. This is the first step in non-maximum
# supression. Since, any pixel will have only 4 approach directions.
x0,y0 = where(((theta<22.5)+(theta>157.5)*(theta<202.5)
+(theta>337.5)) == True)
x45,y45 = where( ((theta>22.5)*(theta<67.5)
+(theta>202.5)*(theta<247.5)) == True)
x90,y90 = where( ((theta>67.5)*(theta<112.5)
+(theta>247.5)*(theta<292.5)) == True)
x135,y135 = where( ((theta>112.5)*(theta<157.5)
+(theta>292.5)*(theta<337.5)) == True)
theta = theta
Image.fromarray(theta).convert('L').save('Angle map.jpg')
theta[x0,y0] = 0
theta[x45,y45] = 45
theta[x90,y90] = 90
theta[x135,y135] = 135
x,y = theta.shape
temp = Image.new('RGB',(y,x),(255,255,255))
for i in range(x):
for j in range(y):
if theta[i,j] == 0:
temp.putpixel((j,i),(0,0,255))
elif theta[i,j] == 45:
temp.putpixel((j,i),(255,0,0))
elif theta[i,j] == 90:
temp.putpixel((j,i),(255,255,0))
elif theta[i,j] == 45:
temp.putpixel((j,i),(0,255,0))
retgrad = grad.copy()
x,y = retgrad.shape
for i in range(x):
for j in range(y):
if theta[i,j] == 0:
test = nms_check(grad,i,j,1,0,-1,0)
if not test:
retgrad[i,j] = 0
elif theta[i,j] == 45:
test = nms_check(grad,i,j,1,-1,-1,1)
if not test:
retgrad[i,j] = 0
elif theta[i,j] == 90:
test = nms_check(grad,i,j,0,1,0,-1)
if not test:
retgrad[i,j] = 0
elif theta[i,j] == 135:
test = nms_check(grad,i,j,1,1,-1,-1)
if not test:
retgrad[i,j] = 0
init_point = stop(retgrad, thresHigh)
# Hysteresis tracking. Since we know that significant edges are
# continuous contours, we will exploit the same.
# thresHigh is used to track the starting point of edges and
# thresLow is used to track the whole edge till end of the edge.
while (init_point != -1):
#Image.fromarray(retgrad).show()
# print 'next segment at',init_point
retgrad[init_point[0],init_point[1]] = -1
p2 = init_point
p1 = init_point
p0 = init_point
p0 = nextNbd(retgrad,p0,p1,p2,thresLow)
while (p0 != -1):
#print p0
p2 = p1
p1 = p0
retgrad[p0[0],p0[1]] = -1
p0 = nextNbd(retgrad,p0,p1,p2,thresLow)
init_point = stop(retgrad,thresHigh)
# Finally, convert the image into a binary image
x,y = where(retgrad == -1)
retgrad[:,:] = 0
retgrad[x,y] = 1.0
return retgrad
def createFilter(rawfilter):
'''
This method is used to create an NxN matrix to be used as a filter,
given a N*N list
'''
order = pow(len(rawfilter), 0.5)
order = int(order)
filt_array = array(rawfilter)
outfilter = filt_array.reshape((order,order))
return outfilter
def gaussFilter(sigma, window = 3):
'''
This method is used to create a gaussian kernel to be used
for the blurring purpose. inputs are sigma and the window size
'''
kernel = zeros((window,window))
c0 = window // 2
for x in range(window):
for y in range(window):
r = hypot((x-c0),(y-c0))
val = (1.0/2*pi*sigma*sigma)*exp(-(r*r)/(2*sigma*sigma))
kernel[x,y] = val
return kernel / kernel.sum()
def nms_check(grad, i, j, x1, y1, x2, y2):
'''
Method for non maximum supression check. A gradient point is an
edge only if the gradient magnitude and the slope agree
for example, consider a horizontal edge. if the angle of gradient
is 0 degress, it is an edge point only if the value of gradient
at that point is greater than its top and bottom neighbours.
'''
try:
if (grad[i,j] > grad[i+x1,j+y1]) and (grad[i,j] > grad[i+x2,j+y2]):
return 1
else:
return 0
except IndexError:
return -1
def stop(im, thres):
'''
This method is used to find the starting point of an edge.
'''
X,Y = where(im > thres)
try:
y = Y.min()
except:
return -1
X = X.tolist()
Y = Y.tolist()
index = Y.index(y)
x = X[index]
return [x,y]
def nextNbd(im, p0, p1, p2, thres):
'''
This method is used to return the next point on the edge.
'''
kit = [-1,0,1]
X,Y = im.shape
for i in kit:
for j in kit:
if (i+j) == 0:
continue
x = p0[0]+i
y = p0[1]+j
if (x<0) or (y<0) or (x>=X) or (y>=Y):
continue
if ([x,y] == p1) or ([x,y] == p2):
continue
if (im[x,y] > thres): #and (im[i,j] < 256):
return [x,y]
return -1