-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanny.py
106 lines (96 loc) · 4.15 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
import numpy as np
import cv2
import matplotlib.pyplot as plt
import math
#Generating Gaussian Kernel with given sigma
def gaussian_kernel(size, sigma=1):
size = int(size) // 2
x, y = np.mgrid[-size:size+1, -size:size+1]
normal = 1 / (2.0 * np.pi * sigma**2)
g = np.exp(-((x**2 + y**2) / (2.0*sigma**2))) * normal
return g
#Applying sobel filter on image to return a sobel-filtered image and sobel gradient
def sobelfilter(image):
gx = np.array([[1.0, 0.0, -1.0], [2.0, 0.0, -2.0], [1.0, 0.0, -1.0]])
gy = np.array([[1.0, 2.0, 1.0], [0.0, 0.0, 0.0], [-1.0, -2.0, -1.0]])
rows, columns = np.shape(image)
sobel_filtered_image = np.zeros((rows, columns),np.float64)
Gradient = np.zeros((rows, columns),np.float64)
for i in range(rows - 2):
for j in range(columns - 2):
Gx = np.multiply(gx, image[i:i + 3, j:j + 3]).sum() / 8
Gy = np.multiply(gy, image[i:i + 3, j:j + 3]).sum() / 8
Gradient[i+1,j+1]=np.degrees(np.arctan2(Gy,Gx))
sobel_filtered_image[i + 1, j + 1] = np.sqrt(np.square(Gx)+np.square(Gy))
sobel_filtered_image=sobel_filtered_image/np.max(sobel_filtered_image)
return sobel_filtered_image,Gradient
#Performing NonMaximum Suppresion
def NonMaxSup(Gmag, Grad):
NMS = np.zeros((Gmag.shape[0],Gmag.shape[1]),np.float64)
for i in range(1, Gmag.shape[0] - 1):
for j in range(1, Gmag.shape[1] - 1):
if((Grad[i,j] >= -22.5 and Grad[i,j] <= 22.5) or (Grad[i,j] <= -157.5 and Grad[i,j] >= 157.5)):
if((Gmag[i,j] > Gmag[i,j+1]) and (Gmag[i,j] > Gmag[i,j-1])):
NMS[i,j] = Gmag[i,j]
else:
NMS[i,j] = 0
if((Grad[i,j] >= 22.5 and Grad[i,j] <= 67.5) or (Grad[i,j] <= -112.5 and Grad[i,j] >= -157.5)):
if((Gmag[i,j] > Gmag[i+1,j+1]) and (Gmag[i,j] > Gmag[i-1,j-1])):
NMS[i,j] = Gmag[i,j]
else:
NMS[i,j] = 0
if((Grad[i,j] >= 67.5 and Grad[i,j] <= 112.5) or (Grad[i,j] <= -67.5 and Grad[i,j] >= -112.5)):
if((Gmag[i,j] > Gmag[i+1,j]) and (Gmag[i,j] > Gmag[i-1,j])):
NMS[i,j] = Gmag[i,j]
else:
NMS[i,j] = 0
if((Grad[i,j] >= 112.5 and Grad[i,j] <= 157.5) or (Grad[i,j] <= -22.5 and Grad[i,j] >= -67.5)):
if((Gmag[i,j] > Gmag[i+1,j-1]) and (Gmag[i,j] > Gmag[i-1,j+1])):
NMS[i,j] = Gmag[i,j]
else:
NMS[i,j] = 0
return NMS
#Threshold Hysteresis
def DoThreshHyst(img):
highThresholdRatio = 0.2
lowThresholdRatio = 0.15
GSup = np.copy(img)
h = int(GSup.shape[0])
w = int(GSup.shape[1])
highThreshold = np.max(GSup) * highThresholdRatio
lowThreshold = highThreshold * lowThresholdRatio
x = 0.1
oldx=0
while(oldx != x):
oldx = x
for i in range(1,h-1):
for j in range(1,w-1):
if(GSup[i,j] > highThreshold):
GSup[i,j] = 1
elif(GSup[i,j] < lowThreshold):
GSup[i,j] = 0
else:
if((GSup[i-1,j-1] > highThreshold) or
(GSup[i-1,j] > highThreshold) or
(GSup[i-1,j+1] > highThreshold) or
(GSup[i,j-1] > highThreshold) or
(GSup[i,j+1] > highThreshold) or
(GSup[i+1,j-1] > highThreshold) or
(GSup[i+1,j] > highThreshold) or
(GSup[i+1,j+1] > highThreshold)):
GSup[i,j] = 1
x = np.sum(GSup == 1)
GSup = (GSup == 1) * GSup
return GSup
#Integrating all phases of canny edge detection.
def canny_edges(img):
img=cv2.GaussianBlur(img,(5,5),0)
sobelfilterimage,gradient=sobelfilter(img)
NMS=NonMaxSup(sobelfilterimage,gradient)
canny=DoThreshHyst(NMS)
return canny
img=cv2.imread('Building.jpg',0)
canny=canny_edges(img)
cv2.imshow("cannyedge",canny)
cv2.waitKey(0)
cv2.destroyAllWindows()