-
Notifications
You must be signed in to change notification settings - Fork 0
/
sobelEdge.m
155 lines (135 loc) · 4.21 KB
/
sobelEdge.m
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
function sobelEdge(im)
%Display original image
figure();
imshow(im);
%% 2.1 Gaussian Low Pass Filter
%Make image grayscale
grayImage = im;
%Assign size of Gaussian mask (5x5) and signma value
N = 5;
sigma = .35;
% Gaussian mask
mask = -floor(N/2) : floor(N/2);
[x,y] = meshgrid(mask, mask);
kernel = exp(-(x.^2 + y.^2) / (2*sigma*sigma));
kernel = kernel / sum(kernel(:));
%Convert image and pad it
img = double(grayImage)./255;
pad = padarray(img,[floor(N/2),floor(N/2)]);
%Alocate space for new image
new_img = zeros(size(img,1),size(img,2));
%Convolute
for ii=1:size(pad,1)-4
for jj=1:size(pad,2)-4
val = pad(ii:ii+4,jj:jj+4).*kernel;
new_img(ii,jj) = sum(val(:));
end
end
%Display
figure();
imshow(new_img,[]);
%% 2.2 Compute Sobel Edge Detection
% Gaussian mask [1 -1]
mx = [1 0 -1
2 0 -2
1 0 -1];
my = [1 2 1
0 0 0
-1 -2 -1];
%Alocate space for new edge images
pad = padarray(new_img,[1,1]);
edgex_img = zeros(size(new_img,1),size(new_img,2));
edgey_img = zeros(size(new_img,1),size(new_img,2));
%Convolute
for ii=1:size(pad,1)-2
for jj=1:size(pad,2)-2
val = pad(ii:ii+2,jj:jj+2).*mx;
edgex_img(ii,jj) = sum(val(:));
end
end
for ii=1:size(pad,1)-2
for jj=1:size(pad,2)-2
val = pad(ii:ii+2,jj:jj+2).*my;
edgey_img(ii,jj) = sum(val(:));
end
end
%Display
figure();
imshow(edgex_img,[]);
figure();
imshow(edgey_img,[]);
%% 2.3 Compute Magnitude
%Compute Mag
mag_img = zeros(size(edgex_img,1),size(edgex_img,2));
for ii=1:size(mag_img,1)
for jj=1:size(mag_img,2)
mag = sqrt((edgex_img(ii,jj)*edgex_img(ii,jj))+(edgey_img(ii,jj)*edgey_img(ii,jj)));
mag_img(ii,jj) = mag;
end
end
figure();
imshow(mag_img,[]);
%% 2.4 Compute Threshold
%Convert to int to compute threshold
mag_img = im2uint8(mag_img);
thr_img = zeros(size(mag_img,1),size(mag_img,2));
t_low = 200; %low threshold
t_high = 250; %high threshold
%If magn(x,y) <= t_low then not edge (display as black i.e. 0)
%If magn(x,y) >= t_high then it is a edge
%If t_low < magn(x,y) < t_high then it is a edge
for ii=1:size(mag_img,1)
for jj=1:size(mag_img,2)
if(mag_img(ii,jj) <= t_low)
thr_img(ii,jj) = 0;
elseif (mag_img(ii,jj) >= t_high)
thr_img(ii,jj) = 255;
elseif ((mag_img(ii,jj) > t_low) && (mag_img(ii,jj) < t_high))
thr_img(ii,jj) = 255;
end
end
end
figure();
imshow(thr_img,[]);
%% 2.5 Compute Direction
%Compute direction
dir_img = zeros(size(edgex_img,1),size(edgex_img,2));
for ii=1:size(dir_img,1)
for jj=1:size(dir_img,2)
dir = atand((abs(edgey_img(ii,jj)))/abs((edgex_img(ii,jj))));
dir_img(ii,jj) = dir;
end
end
%Define the bin values
steps = [0 45 90 135];
dirQ = zeros(size(dir_img,1),size(dir_img,2));
%Quantize
for ii=1:size(dir_img,1)
for jj=1:size(dir_img,2)
if dir_img(ii,jj) <= steps(1)
dirQ(ii,jj) = steps(1);
elseif dir_img(ii,jj) > steps(end)
dirQ(ii,jj) = steps(end);
else
for kk = 1 : size(steps,2)
if dir_img(ii,jj) > (steps(kk)-45) && dir_img(ii,jj) < (steps(kk)+45)
dirQ(ii,jj) = steps(kk);
end
end
end
end
end
%Display only horizontal and vertical edges of threshold image
edge_dir = uint8(zeros(size(dir_img,1),size(dir_img,2)));
for ii=1:size(edge_dir,1)
for jj=1:size(edge_dir,2)
if dirQ(ii,jj) == 45 || dirQ(ii,jj) == 135
edge_dir(ii,jj) = thr_img(ii,jj);
else
edge_dir(ii,jj) = 0;
end
end
end
figure();
imshow(edge_dir,[]);
end