forked from sebasvega95/Canny-edge-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradient.py
59 lines (49 loc) · 1.35 KB
/
gradient.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
from __future__ import division
from gaussian_filter import gaussian
from numpy import array, zeros, abs, sqrt, arctan2, arctan, pi, real
from numpy.fft import fft2, ifft2
from PIL import Image
from matplotlib.pyplot import imshow, show, subplot, figure, gray, title, axis
def gradient(im):
# Sobel operator
op1 = array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
op2 = array([[-1, -2, -1],
[ 0, 0, 0],
[ 1, 2, 1]])
kernel1 = zeros(im.shape)
kernel1[:op1.shape[0], :op1.shape[1]] = op1
kernel1 = fft2(kernel1)
kernel2 = zeros(im.shape)
kernel2[:op2.shape[0], :op2.shape[1]] = op2
kernel2 = fft2(kernel2)
fim = fft2(im)
Gx = real(ifft2(kernel1 * fim)).astype(float)
Gy = real(ifft2(kernel2 * fim)).astype(float)
G = sqrt(Gx**2 + Gy**2)
Theta = arctan2(Gy, Gx) * 180 / pi
return G, Theta
if __name__ == '__main__':
from sys import argv
if len(argv) < 2:
print "Usage: python %s <image>" % argv[0]
exit()
im = array(Image.open(argv[1]))
im = im[:, :, 0]
gim = gaussian(im)
grim, gphase = gradient(gim)
gray()
subplot(2, 2, 1)
imshow(im)
axis('off')
title('Original')
subplot(2, 2, 2)
imshow(gim)
axis('off')
title('Gaussian')
subplot(2, 2, 3)
imshow(grim)
axis('off')
title('Gradient')
show()