-
Notifications
You must be signed in to change notification settings - Fork 0
/
canny_edge.py
223 lines (207 loc) · 8.71 KB
/
canny_edge.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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 29 20:11:28 2019
@author: Ankush Jain
"""
import numpy as np
import matplotlib.pyplot as plt
from math import floor, degrees, atan2
from cv2 import imread, imshow, imwrite, waitKey
# Function for performing Convolution
def convolution(m1, m2, maskSum):
n, m = m1.shape
#print(m1,m2,maskSum)
mSum = 0
for i in range(n):
for j in range(m):
mSum += (int(m1[i, j]) * int(m2[i, j]))
return int(round(mSum/maskSum))
# Function for getting gradient magnitude and angle from X & Y gradient
def gradient_mag_and_angle(gradX, gradY, gradientSize, gaussianSize):
# Setting mask parameters
outMag = np.zeros(gradX.shape, dtype='uint8')
outAng = np.zeros(gradX.shape, dtype='float')
# Variable to hold undefined area from gaussian filtering
undefPixels = floor(gaussianSize/2) + floor(gradientSize/2)
# Getting img dimensions
n, m = gradX.shape
# Starting gaussian masking
for i in range(undefPixels, n-undefPixels):
for j in range(undefPixels, m-undefPixels):
outMag[i][j] = int(round((gradX[i][j]**2 + gradY[i][j]**2)**0.5))
ang = degrees(atan2(gradY[i][j], gradX[i][j]))
if ang < 0:
outAng[i][j] = ang + 360
else:
outAng[i][j] = ang
return outMag, outAng
# Function for Gaussian Smoothing
def gaussian_smoothing(img, maskSize):
# Setting mask parameters
out = np.zeros(img.shape, dtype='uint8')
if maskSize == 7:
mask = np.array([[1,1,2,2,2,1,1],
[1,2,2,4,2,2,1],
[2,2,4,8,4,2,2],
[2,4,8,16,8,4,2],
[2,2,4,8,4,2,2],
[1,2,2,4,2,2,1],
[1,1,2,2,2,1,1]], dtype='uint8')
maskSum = np.uint8(140)
maskSizeHalf = floor(maskSize/2)
# Getting img dimensions
n, m = img.shape
# Starting gaussian masking
for i in range(n):
for j in range(m):
#i,j=3,223
iLow = i - maskSizeHalf
iUp = i + maskSizeHalf + 1
jLow = j - maskSizeHalf
jUp = j + maskSizeHalf + 1
windowPixels = img[iLow:iUp, jLow:jUp]
#
if windowPixels.shape == (maskSize, maskSize):
#print(i,j)
out[i,j] = convolution(mask, windowPixels, maskSum)
return out
# Function for Gradient Operation
def gradient_operation(img, axis, maskSize, gaussianSize):
# Setting mask parameters
out = np.zeros(img.shape, dtype='int8')
# Variable to hold undefined area from gaussian filtering
undefPixels = floor(gaussianSize/2)
# X axis sobel mask
if maskSize == 3 and axis.lower() == 'x':
mask = np.array([[-1,0,1],
[-2,0,2],
[-1,0,1]], dtype='int8')
maskSizeHalf = floor(maskSize/2)
# Y axis sobel mask
if maskSize == 3 and axis.lower() == 'y':
mask = np.array([[1,2,1],
[0,0,0],
[-1,-2,-1]], dtype='int8')
maskSizeHalf = floor(maskSize/2)
# Getting img dimensions
n, m = img.shape
# Starting gaussian masking
for i in range(undefPixels+1, n-undefPixels-1):
for j in range(undefPixels+1, m-undefPixels-1):
#i,j,maskSizeHalf=4,4,1
iLow = i - maskSizeHalf
iUp = i + maskSizeHalf + 1
jLow = j - maskSizeHalf
jUp = j + maskSizeHalf + 1
windowPixels = img[iLow:iUp, jLow:jUp]
#
if windowPixels.shape == (maskSize, maskSize):
#print(i,j)
out[i,j] = convolution(mask, windowPixels, 1)
return out
def non_maxima_suppression(gradMag, gradAng, gradientSize, gaussianSize):
# Setting mask parameters
out = np.zeros(gradMag.shape, dtype='uint8')
# Variable to hold undefined area from gaussian filtering
# To process 8-connected neighbors of NMS,1 more pixel is added to border
undefPixels = floor(gaussianSize/2) + floor(gradientSize/2) + 1
# Getting img dimensions
n, m = gradMag.shape
# Starting gaussian masking
for i in range(undefPixels, n-undefPixels):
for j in range(undefPixels, m-undefPixels):
# Sector 0 neighbors
if (337.5<=gradAng[i][j]<=360) or (0<=gradAng[i][j]<22.5) or (157.5<=gradAng[i][j]<202.5):
neighbor1 = gradMag[i][j+1]
neighbor2 = gradMag[i][j-1]
# Sector 1 neighbors
elif (22.5<=gradAng[i][j]<67.5) or (202.5<=gradAng[i][j]<247.5):
neighbor1 = gradMag[i-1][j+1]
neighbor2 = gradMag[i+1][j-1]
# Sector 2 neighbors
elif (67.5<=gradAng[i][j]<112.5) or (247.5<=gradAng[i][j]<292.5):
neighbor1 = gradMag[i-1][j]
neighbor2 = gradMag[i+1][j]
# Sector 3 neighbors
elif (112.5<=gradAng[i][j]<157.5) or (292.5<=gradAng[i][j]<337.5):
neighbor1 = gradMag[i-1][j-1]
neighbor2 = gradMag[i+1][j+1]
# Default case
else:
neighbor1 = 255
neighbor2 = 255
# Comparing current pixel with its neighbors
if gradMag[i][j] > neighbor1 and gradMag[i][j] > neighbor2:
out[i][j] = gradMag[i][j]
return out
def thresholding(nmsImg, gradMag, gradAng, t, gradientSize, gaussianSize):
# Setting mask parameters
# nmsImg = resultNMS
# gradMag = resultGradMag
# gradAng = resultGradAng
# t = 50
# gradientSize = 3
# gaussianSize = 7
out = np.zeros(nmsImg.shape, dtype='uint8')
# Setting threshold values
t1 = t
t2 = 2*t
# Variable to hold undefined area from gaussian filtering
# To process 8-connected neighbors of NMS,1 more pixel is added to border
undefPixels = floor(gaussianSize/2) + floor(gradientSize/2) + 1
# Getting img dimensions
n, m = nmsImg.shape
# Starting gaussian masking
for i in range(undefPixels, n-undefPixels):
for j in range(undefPixels, m-undefPixels):
#i=8
#j=10
# Case 1 with pixel less than T1
if nmsImg[i][j] < t1:
out[i][j] = 0
# Case 2 with pixel between T1 and T2
elif t1 <= nmsImg[i][j] <= t2:
# Extracting 8-neighbor magnitudes
neighborMags = [gradMag[i-1,j-1], gradMag[i-1,j], gradMag[i-1,j+1],
gradMag[i,j-1], gradMag[i,j+1],
gradMag[i+1,j-1], gradMag[i+1,j], gradMag[i+1,j+1]]
# Extracting 8-neighbor angles
neighborAngs = [gradAng[i-1,j-1], gradAng[i-1,j], gradAng[i-1,j+1],
gradAng[i,j-1], gradAng[i,j+1],
gradAng[i+1,j-1], gradAng[i+1,j], gradAng[i+1,j+1]]
# Comparing the 8-neighbors of the current pixel with T2
val = 0
for k in range(8):
#print(neighborMags[k], abs(neighborAngs[k] - gradAng[i][j]))
if neighborMags[k] > t2 and abs(neighborAngs[k] - gradAng[i][j]) <= 45:
val = 255
#print(val)
break
out[i][j] = val
# Case 3 with pixel greater than T2
elif nmsImg[i][j] > t2:
out[i][j] = 255
return out
# MAIN
path = 'data/chess.bmp'
#path = 'data/Zebra-crossing-1.bmp'
img = imread(path, 0)
resultGauss = gaussian_smoothing(img, 7)
resultGradX = gradient_operation(resultGauss, 'x', 3, 7)
resultGradY = gradient_operation(resultGauss, 'y', 3, 7)
resultGradMag, resultGradAng = gradient_mag_and_angle(resultGradX, resultGradY, 3, 7)
resultNMS = non_maxima_suppression(resultGradMag, resultGradAng, 3, 7)
resultThresholding = thresholding(resultNMS, resultGradMag, resultGradAng, 25, 3, 7)
imshow('image', img)
imshow('image', resultGauss)
imshow('image', resultGradX)
imshow('image', resultGradY)
imshow('image', resultGradMag)
imshow('image', resultNMS)
imshow('image', resultThresholding)
waitKey(0)
imwrite('result2.bmp', resultThresholding)
# 1. Threshold t1 and t2 for the images to be inserted into the report.
# 2. In thresholding, for case-2, we will use the 8 connected neighbors of gradMag or nmsMag matrices?
# 3. Total number of undefined pixels on edges? 3 (gauss)+1(sobel)+1(nms)=5?
# 4. Should the nmsMag be normalized to 0 or 255?