-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathelderzuckeredge_base.m
393 lines (338 loc) · 11.1 KB
/
elderzuckeredge_base.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
%% ELDERZUCKEREDGE_BASE - Base function for ELDERZUCKEREDGE.
%
%% Syntax
% [edgemap, scmap, blur] = ELDERZUCKEREDGE_BASE(I, sigma);
%
%% See also
% Related:
% <ELDERZUCKEREDGE.html |ELDERZUCKEREDGE|>,
% <CANNYEDGE_BASE.html |CANNYEDGE_BASE|>,
% <EDGECORNER_BASE.html |EDGECORNER_BASE|>,
% <CONGRUENCYEDGE_BASE.html |CONGRUENCYEDGE_BASE|>,
% <COMPASSEDGE_BASE.html |COMPASSEDGE_BASE|>,
% <ROTHWELLEDGE_BASE.html |ROTHWELLEDGE_BASE|>,
% <ANISOEDGE_BASE.html |ANISOEDGE_BASE|>,
% <KOETHEDGE_BASE.html |KOETHEDGE_BASE|>,
% <SDGDEDGE_BASE.html |SDGDEDGE_BASE|>,
% <PETROUEDGE_BASE.html |PETROUEDGE_BASE|>.
% Called:
% <matlab:web(whichpath('EDGE')) |EDGE|>,
% <matlab:web(whichpath('CONV2')) |CONV2|>.
%% Function implementation
function [edgemap,varargout] = elderzuckeredge_base(I, sigma, reduce) %#ok
%%
% dealing with multispectral images
C = size(I,3);
if C>1
edgemap = zeros(size(I));
if nargout>=2, varargout{1} = zeros(size(I)); end
if nargout==3, varargout{2} = zeros(size(I)); end
for c=1:C
[edgemap(:,:,c),tmp1,tmp2] = elderzuckeredge_base(I(:,:,c), sigma);
if nargout>=2, varargout{1}(:,:,c) = tmp1; end
if nargout>=3, varargout{2}(:,:,c) = tmp2; end
end
return;
end
pad = 2;
I = padarray(I,[pad pad],'replicate','both');
[X,Y,C] = size(I); %#ok
%%
% computing edges
%alphaP = 1 - (1 - sigma).^(1./length(I))
alphaP = 2e-7;
mag = zeros(size(I));
ang = zeros(size(I));
map = zeros(size(I));
%%
% creating the different scales: these represent the different scale sizes
% supported here, expressed as the standard deviation of the filter.
sd1 = [16 8 4 2 1 0.5];
%%
% we iterate: for each standard deviation, we compute the 'gradient'- which
% in this case uses the steering functions defined by Elder and Zucker.
for h = 1:length(sd1)
[mg ag map] = gradEZD2(I, sd1(h),sigma,alphaP, map);
mag(mg ~= 0) = 0;
mag = mag + mg;
ang(ag ~= 0) = 0;
ang = ang + ag;
end
%scaind = log2(map) + repmat(2, size(map));
sd2 = [4 2 1 0.5];
scmap = zeros([X,Y]);
lap_of_gau = zeros([X,Y]);
for h = 1:length(sd2)
[l_o_g scmap] = lapEZD2(I,sd2(h),sigma,alphaP, ang, scmap);
lap_of_gau(l_o_g ~= 0) = 0;
lap_of_gau = lap_of_gau + l_o_g;
end
%scaind2 = log2(scmap) + repmat(2, size(scmap));
%%
% create the edgemap
edgemap = edge(lap_of_gau, 'canny');
edgemap = edgemap(pad+1:end-pad,pad+1:end-pad,:);
if nargout>=2
varargout{1} = scmap(pad+1:end-pad,pad+1:end-pad,:);
end
%%
% create the blur map if required
if nargout==3
% compute the distance between extrema
[A,B] = find(edgemap ~= 0);
[Alim, Blim] = size(I);
window = 20;
w = window;
varargout{2} = zeros(X,Y);
for i= 1:length(A),
%define search window
Alower = A(i) - window;
Blower = B(i) - window;
Aupper = A(i) + window;
Bupper = B(i) + window;
%clip to edge of image
if Alower < 1, Alower = 1; %#ok
end
if Blower < 1, Blower = 1; %#ok
end
if Aupper>Alim, Aupper = Alim; %#ok
end
if Bupper > Blim, Bupper = Blim; %#ok
end
currentmax = lap_of_gau(A(i),B(i));
currentmin = currentmax;
nextmin = currentmin;
nextmax = currentmax;
currentpixelx = A(i);
currentpixely = B(i);
currentangle = 45*round(rad2deg(ang(currentpixelx, currentpixely))/45);
if (currentangle == 0 )|| (currentangle == 360 )|| (currentangle == -360),
pixelincx = 1;
pixelincy = 0;
end
if (currentangle == 45 )|| (currentangle == -315),
pixelincx = 1;
pixelincy = 1;
end
if (currentangle == 90 )|| (currentangle == -270),
pixelincx = 0;
pixelincy = 1;
end
if (currentangle == 135 )|| (currentangle == -225),
pixelincx = -1;
pixelincy = 1;
end
if (currentangle == 180 )|| (currentangle == -180),
pixelincx = -1;
pixelincy = 0;
end
if (currentangle == 225 )|| (currentangle == -135),
pixelincx = -1;
pixelincy = -1;
end
if (currentangle == 270 )|| (currentangle == -90),
pixelincx = 0;
pixelincy = -1;
end
if (currentangle == 315 )|| (currentangle == -45),
pixelincx = 1;
pixelincy = -1;
end
counter = 0;
while(nextmax >= currentmax),
nextmaxx = currentpixelx + pixelincx;
nextmaxy = currentpixely + pixelincy;
if nextmaxx < 1 || (nextmaxx > size(I, 1)) || ...
(nextmaxy < 1) || (nextmaxy > size(I,2))
break;
end
nextmax = lap_of_gau(nextmaxx, nextmaxy);
counter = counter +1;
if counter > w,
break;
end
currentpixelx = nextmaxx;
currentpixely = nextmaxy;
end
maxx = currentpixelx;
maxy = currentpixely;
currentpixelx = A(i);
currentpixely = B(i);
counter = 0;
while(nextmin <= currentmin),
nextminx = currentpixelx - pixelincx;
nextminy = currentpixely - pixelincy;
if nextminx < 1 || (nextminx > size(I, 1)) || ...
(nextminy < 1) || (nextminy > size(I,2))
break;
end
nextmin = lap_of_gau(nextminx, nextminy);
counter = counter +1;
if counter > w,
break
end
currentpixelx = nextminx;
currentpixely = nextminy;
end
minx = currentpixelx;
miny = currentpixely;
d = sqrt((maxx-minx)^2 + (maxy-miny)^2);
temp = sqrt((d/2)^2 - scmap(A(i),B(i))^2);
if isreal(temp)
varargout{2}(A(i),B(i)) = temp;
else
varargout{2}(A(i),B(i)) = 0;
end
end
varargout{2} = varargout{2}(pad+1:end-pad,pad+1:end-pad,:);
end
end % end of elderzuckeredge_base
%% Subfunctions
%%
% |GRADEZD2| - Return two matrices the size of |im| that contain the magnitude
% and the angle of the intensity gradient of |im| using a gaussian directional
% derivative filter of standard deviation |sd1|.
% If given, the optional |marker| matrix will be set to |sd1| everywhere the
% gradient magnitude exceeds the critical value function of |sd1| and left
% alone elsewhere. This matrix will then be returned. If no marker matrix
% is given as an argument, the function won't return the third value.
%--------------------------------------------------------------------------
function [mg, ag, marker, crit] = gradEZD2(im,scale,sigma,alphaP, marker)
if (nargin > 5)
error('Wrong number of arguments to gradient.');
end
if (nargin > 4)
if (size(marker) ~= size(im))
error('Marker image not the same size as im.');
end
else
if (nargout > 2)
error('No marker matrix can be returned unless you supply one.');
end
end
% we want 2 sd on either side of the filter.
% this means that in all, our filter is
% 4*scale by 4*scale
tail = ceil(2 * scale);
% construct the filter.
[X Y] = meshgrid(-tail:tail);
gx = g1x(X, Y, scale);
gy = g1x(Y, X, scale);
%convolve with the filter.
% this becomes very expensive with large sized filters.
gimx = conv2(im, gx, 'same');
gimy = conv2(im, gy, 'same');
ag = angle(gimx + 1i*gimy);
mg = steer1(ag, gimx, gimy);
abmag = abs(mg);
% compute the critical threshold for the gaussian of this scale.
% then threshold the magnitude by this value.
crit = c1(scale,sigma,alphaP);
if (nargin > 4)
list = abmag >= crit;
marker(list) = scale;
end
% output magnitude and angle of only those points
% with absolute magnitude of gaussian greater than
% the critical threshold.
list = find(abmag < crit);
mg(list) = 0;
ag(list) = 0;
end % end of gradEZD2
%%
% |LAPEZD2| - Return a matrix representing the laplacian of |im| at the
% angles given by |gau_ang|. If given, the optional |marker| matrix will be
% set to |sd2| everywhere the gradient magnitude exceeds the critical value
% function of |sd2| and left alone elsewhere. This matrix will then be
% returned. If no marker matrix is given as an argument, the function won't
% return the third value.
%--------------------------------------------------------------------------
function [lap_of_gau, marker] = lapEZD2(im,sd2, sigma,alphaP, gau_ang, marker)
if (nargin > 6)
error('Wrong number of arguments to gradient.');
end
if (nargin > 5)
if (size(marker) ~= size(im))
error('Marker image not the same size as im.');
end
else
if (nargout > 1)
error('No marker matrix can be returned unless you supply one.');
end
end
% We want 2 sd on either side.
tail = ceil(2 * sd2);
[X Y] = meshgrid(-tail:tail);
gx = g2x(X, Y, sd2);
gy = g2x(Y, X, sd2);
gxy= g2xy(X, Y, sd2);
gimx = conv2(im, gx, 'same');
gimy = conv2(im, gy, 'same');
gimxy= conv2(im, gxy, 'same');
lap_of_gau = steer2(gau_ang, gimx, gimy, gimxy);
ablog = abs(lap_of_gau);
crit = c2(sd2,sigma,alphaP);
if (nargin > 5)
list = ablog >= crit;
marker(list) = sd2;
end
list = ablog < crit;
lap_of_gau(list) = 0;
end % end of lapEZD2
%%
% |STEER1|
%--------------------------------------------------------------------------
function imout = steer1(alpha, x_grad_im, y_grad_im)
imout = cos(alpha).*x_grad_im + sin(alpha).*y_grad_im;
end % end of steer1
%%
% |STEER2|
%--------------------------------------------------------------------------
function imout = steer2(alpha, xgrd_im, ygrd_im, xygrd_im)
ca = cos(alpha);
sa = sin(alpha);
imout =(ca.^2 .* xgrd_im)+(sa.^2 .* ygrd_im)-(2.*ca.*sa.*xygrd_im);
end % end of steer2
%%
% |G1X|
%--------------------------------------------------------------------------
function g = g1x(x,y,s1)
s1sq = s1.^2;
g = -(x./(2*pi*s1sq.^2)) .* exp(-(x.^2 + y.^2)./(2*s1sq));
end % end of g1x
%%
% |G2X|
%--------------------------------------------------------------------------
function g = g2x(x,y,s2)
sdnorm = 1 ./ (2*pi*s2.^4);
g = sdnorm .* (((x/s2).^2) - 1) .* exp(-(x.^2 + y.^2)./(2*s2.^2));
end % end of g2x
%%
% |G2XY|
%--------------------------------------------------------------------------
function g = g2xy(x,y,s2)
g = ((x.*y)./(2*pi*s2.^6)) .* exp(-(x.^2 + y.^2)./(2*s2.^2));
end % end of g2xy
%%
% |C1|
%--------------------------------------------------------------------------
function out = c1(sd1,sigma,alphaP)
s1 = sigma .* (1 ./ (2.*sqrt(2.*pi).*sd1.^2));
out = s1 .* sqrt(-2.*log(alphaP));
%out = 5 * out;
end % end of c1
%%
% |C2|
%--------------------------------------------------------------------------
function out = c2(sd2,sigma,alphaP)
s2 = sigma .* ((4.*sqrt(pi/3).*sd2.^3).^-1);
out = sqrt(2) .* s2 .* (erfinv(1-alphaP));
% best guess threshold
% out = (6 * sigma) ./ sd2.^3;
end % end of c2
%%
% |RAD2DEG|
%--------------------------------------------------------------------------
function degrees = rad2deg(radians)
degrees = radians * 180 / pi;
end % end of rad2deg