Skip to content

Commit 7a6259c

Browse files
author
Juan Sebastián Vega Patiño
committed
Merge pull request sebasvega95#1 from sebasvega95/maximum
added Non-maximum suppression
2 parents 6a15461 + f42c807 commit 7a6259c

File tree

2 files changed

+95
-32
lines changed

2 files changed

+95
-32
lines changed

gradient.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
from __future__ import division
22
from gaussian_filter import gaussian
3-
from numpy import array, zeros, abs, sqrt, arctan2
3+
from numpy import array, zeros, abs, sqrt, arctan2, pi
44
from numpy.fft import fft2, ifft2
55
from PIL import Image
66
from matplotlib.pyplot import imshow, show, subplot, figure, gray, title, axis
77

88
def gradient(im):
99
# Sobel operator
1010
op1 = array([[-1, 0, 1],
11-
[-2, 0, 2],
12-
[-1, 0, 1]])
11+
[-2, 0, 2],
12+
[-1, 0, 1]])
1313
op2 = array([[-1, -2, -1],
14-
[0, 0, 0],
15-
[1, 2, 1]])
14+
[0, 0, 0],
15+
[1, 2, 1]])
1616
kernel1 = zeros(im.shape)
1717
kernel1[:op1.shape[0], :op1.shape[1]] = op1
1818
kernel1 = fft2(kernel1)
@@ -22,34 +22,34 @@ def gradient(im):
2222
kernel2 = fft2(kernel2)
2323

2424
fim = fft2(im)
25-
Gx = abs(ifft2(kernel1 * fim)).astype(float)
26-
Gy = abs(ifft2(kernel2 * fim)).astype(float)
25+
Gx = ifft2(kernel1 * fim).astype(float)
26+
Gy = ifft2(kernel2 * fim).astype(float)
2727

2828
G = sqrt(Gx**2 + Gy**2)
29-
Theta = arctan2(Gy, Gx)
30-
31-
return G.astype(int), Theta.astype(int)
29+
Theta = arctan2(Gy, Gx) * 180 / pi
30+
return G, Theta
3231

3332
if __name__ == '__main__':
34-
im = array(Image.open("emilia.jpg"))
35-
im = im[:, :, 0]
36-
gim = gaussian(im)
37-
grim, gphase = gradient(gim)
38-
gray()
39-
40-
subplot(2, 2, 1)
41-
imshow(im)
42-
axis('off')
43-
title('Original')
44-
45-
subplot(2, 2, 2)
46-
imshow(gim)
47-
axis('off')
48-
title('Gaussian')
49-
50-
subplot(2, 2, 3)
51-
imshow(grim)
52-
axis('off')
53-
title('Gradient')
54-
55-
show()
33+
im = array(Image.open("emilia.jpg"))
34+
im = im[:, :, 0]
35+
gim = gaussian(im)
36+
grim, gphase = gradient(gim)
37+
38+
gray()
39+
40+
subplot(2, 2, 1)
41+
imshow(im)
42+
axis('off')
43+
title('Original')
44+
45+
subplot(2, 2, 2)
46+
imshow(gim)
47+
axis('off')
48+
title('Gaussian')
49+
50+
subplot(2, 2, 3)
51+
imshow(grim)
52+
axis('off')
53+
title('Gradient')
54+
55+
show()

nonmax_suppression.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from __future__ import division
2+
from gaussian_filter import gaussian
3+
from gradient import gradient
4+
from numpy import array, zeros
5+
from PIL import Image
6+
from matplotlib.pyplot import imshow, show, subplot, figure, gray, title, axis
7+
8+
def maximum(det, phase):
9+
gmax = zeros(det.shape)
10+
for i in xrange(gmax.shape[0]):
11+
for j in xrange(gmax.shape[1]):
12+
if(phase[i][j] < 0):
13+
phase[i][j] += 360
14+
15+
if( ((j+1) < gmax.shape[1]) and ((j-1) >= 0) and ((i+1) < gmax.shape[0]) and ((i-1) >= 0) ):
16+
#0 grados
17+
if((phase[i][j] >= 337.5 and phase[i][j] < 22.5) or (phase[i][j] >= 157.5 and phase[i][j] < 202.5)):
18+
if(det[i][j] > det[i][j + 1] and det[i][j] > det[i][j - 1]):
19+
gmax[i][j] = det[i][j]
20+
#45 grados
21+
if((phase[i][j] >= 22.5 and phase[i][j] < 67.5) or (phase[i][j] >= 202.5 and phase[i][j] < 247.5)):
22+
if(det[i][j] > det[i - 1][j + 1] and det[i][j] > det[i + 1][j - 1]):
23+
gmax[i][j] = det[i][j]
24+
#90 grados
25+
if((phase[i][j] >= 67.5 and phase[i][j] < 112.5) or (phase[i][j] >= 247.5 and phase[i][j] < 292.5)):
26+
if(det[i][j] > det[i - 1][j] and det[i][j] > det[i + 1][j]):
27+
gmax[i][j] = det[i][j]
28+
#136 grados
29+
if((phase[i][j] >= 112.5 and phase[i][j] < 157.5) or (phase[i][j] >= 292.5 and phase[i][j] < 337.5)):
30+
if(det[i][j] > det[i - 1][j - 1] and det[i][j] > det[i + 1][j + 1]):
31+
gmax[i][j] = det[i][j]
32+
return gmax
33+
34+
if __name__ == '__main__':
35+
im = array(Image.open("emilia.jpg"))
36+
im = im[:, :, 0]
37+
gim = gaussian(im)
38+
grim, gphase = gradient(gim)
39+
gmax = maximum(grim, gphase)
40+
41+
gray()
42+
43+
subplot(2, 2, 1)
44+
imshow(im)
45+
axis('off')
46+
title('Original')
47+
48+
subplot(2, 2, 2)
49+
imshow(gim)
50+
axis('off')
51+
title('Gaussian')
52+
53+
subplot(2, 2, 3)
54+
imshow(grim)
55+
axis('off')
56+
title('Gradient')
57+
58+
subplot(2, 2, 4)
59+
imshow(gmax)
60+
axis('off')
61+
title('Non-Maximum suppression')
62+
63+
show()

0 commit comments

Comments
 (0)