Skip to content

Instantly share code, notes, and snippets.

@FienSoP
Created January 16, 2019 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FienSoP/03ed9b0eab196dde7b66f452725a42ac to your computer and use it in GitHub Desktop.
Save FienSoP/03ed9b0eab196dde7b66f452725a42ac to your computer and use it in GitHub Desktop.
def non_max_suppression(img, D):
M, N = img.shape
Z = np.zeros((M,N), dtype=np.int32)
angle = D * 180. / np.pi
angle[angle < 0] += 180
for i in range(1,M-1):
for j in range(1,N-1):
try:
q = 255
r = 255
#angle 0
if (0 <= angle[i,j] < 22.5) or (157.5 <= angle[i,j] <= 180):
q = img[i, j+1]
r = img[i, j-1]
#angle 45
elif (22.5 <= angle[i,j] < 67.5):
q = img[i+1, j-1]
r = img[i-1, j+1]
#angle 90
elif (67.5 <= angle[i,j] < 112.5):
q = img[i+1, j]
r = img[i-1, j]
#angle 135
elif (112.5 <= angle[i,j] < 157.5):
q = img[i-1, j-1]
r = img[i+1, j+1]
if (img[i,j] >= q) and (img[i,j] >= r):
Z[i,j] = img[i,j]
else:
Z[i,j] = 0
except IndexError as e:
pass
return Z
@chawza
Copy link

chawza commented Nov 4, 2020

thank you, your code is clean and minimal. Love it dude

@Nabila412
Copy link

Thankyou for your code, I've tried them but then I got this error, can you tell me whats wrong with this?

TypeError Traceback (most recent call last) in
9 gaussian_kernel(size=5, sigma=1)
10 sobel_filters(img=image)
---> 11 non_max_suppression(img=image, D=3/4)
12 threshold(img=image,lowThresholdRatio=0.05,highThresholdRatio=0.09)
13 hysteresis(img=threshold, weak=0, strong=255)

in non_max_suppression(img, D)
4 Z = np.zeros((M,N), dtype=np.int32)
5 angle = D * (180. / np.pi)
----> 6 angle[angle < 0] += 180
7
8

TypeError: 'float' object is not subscriptable

@sushilkjaiswar
Copy link

Thankyou for your code, I've tried them but then I got this error, can you tell me whats wrong with this?

TypeError Traceback (most recent call last) in 9 gaussian_kernel(size=5, sigma=1) 10 sobel_filters(img=image) ---> 11 non_max_suppression(img=image, D=3/4) 12 threshold(img=image,lowThresholdRatio=0.05,highThresholdRatio=0.09) 13 hysteresis(img=threshold, weak=0, strong=255)

in non_max_suppression(img, D) 4 Z = np.zeros((M,N), dtype=np.int32) 5 angle = D * (180. / np.pi) ----> 6 angle[angle < 0] += 180 7 8

TypeError: 'float' object is not subscriptable

I am facing same issue. Have you figured out the issue present in this script?

@arkkevin
Copy link

how to show the result? I make this in google colab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment