-
Notifications
You must be signed in to change notification settings - Fork 0
/
fanisodiff.m
228 lines (179 loc) · 7.56 KB
/
fanisodiff.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
function f_diff = fanisodiff(im, niter, kappa, lambda, option,v,dir)
% ARGUMENT DESCRIPTION:
% IM - gray scale image (MxN).
% NUM_ITER - number of iterations.
% DELTA_T - integration constant (0 <= delta_t <= 1/7).
% Usually, due to numerical stability this
% parameter is set to its maximum value.
% KAPPA - gradient modulus threshold that controls the conduction.
% OPTION - conduction coefficient functions proposed by Perona & Malik:
% 1 - c(x,y,t) = exp(-(nablaI/kappa).^2),
% privileges high-contrast edges over low-contrast ones.
% 2 - c(x,y,t) = 1./(1 + (nablaI/kappa).^2),
% privileges wide regions over smaller ones.
% DIR - Direction in which anisotropic diffusion has to
% be performed
% 1-['AL4']- All four 2-['EW']-East-West 3-['NS']-North-South 4-['D']-Both
% diagonals 5- Individual ['E','W','N','S']
% V - Order of fractional derivative( [-1,-2) )
%
% OUTPUT DESCRIPTION:
% DIFF_IM - (diffused) image with the largest scale-space parameter.
% Convert input image to double.
im = double(im);
% PDE (partial differential equation) initial condition.
f_diff = im;
% Center pixel distances.
dx = 1;
dy = 1;
dd = sqrt(2);
% 2D convolution masks - integral finite differences.
hN = [0 1 0; 0 -1 0; 0 0 0];
hS = [0 0 0; 0 -1 0; 0 1 0];
hE = [0 0 0; 0 -1 1; 0 0 0];
hW = [0 0 0; 1 -1 0; 0 0 0];
hNE = [0 0 1; 0 -1 0; 0 0 0];
hSE = [0 0 0; 0 -1 0; 0 0 1];
hSW = [0 0 0; 0 -1 0; 1 0 0];
hNW = [1 0 0; 0 -1 0; 0 0 0];
% 2D convolution masks - fractional finite differences.
om_2 = v*(v-1)/2;
om_1 = v;
normalize = ((om_1+om_2+1)/2)^10;
hNf = zeros(5,5);
hSf = zeros(5,5);
hEf = zeros(5,5);
hWf = zeros(5,5);
hNf(3,3) =-1; hNf(2,3)=om_1; hNf(1,3) = om_2;
hSf(3,3) =-1; hSf(4,3)=om_1; hSf(5,3) = om_2;
hEf(3,3) =-1; hEf(3,4)=om_1; hEf(3,5) = om_2;
hWf(3,3) =-1; hWf(3,2)=om_1; hEf(3,1) = om_2;
switch dir
case 'AL4'
for t = 1:2
nablaN = imfilter(f_diff,hNf,'conv');
nablaS = imfilter(f_diff,hSf,'conv');
nablaW = imfilter(f_diff,hWf,'conv');
nablaE = imfilter(f_diff,hEf,'conv');
if option == 1
cN = exp(-(nablaN/(normalize*kappa)).^2);
cS = exp(-(nablaS/(normalize*kappa)).^2);
cW = exp(-(nablaW/(normalize*kappa)).^2);
cE = exp(-(nablaE/(normalize*kappa)).^2);
elseif option == 2
cN = 1./(1 + (nablaN/(1/normalize*kappa)).^2);
cS = 1./(1 + (nablaS/(1/normalize*kappa)).^2);
cW = 1./(1 + (nablaW/(1/normalize*kappa)).^2);
cE = 1./(1 + (nablaE/(1/normalize*kappa)).^2);
end
% Discrete PDE solution.
f_diff = f_diff + ...
lambda*(...
(1/(dy^2))*cN.*nablaN + (1/(dy^2))*cS.*nablaS + ...
(1/(dx^2))*cW.*nablaW + (1/(dx^2))*cE.*nablaE);
end
if niter > 2
for t = 2:niter
nablaN = imfilter(f_diff,hN,'conv');
nablaS = imfilter(f_diff,hS,'conv');
nablaW = imfilter(f_diff,hW,'conv');
nablaE = imfilter(f_diff,hE,'conv');
if option == 1
cN = exp(-(nablaN/kappa).^2);
cS = exp(-(nablaS/kappa).^2);
cW = exp(-(nablaW/kappa).^2);
cE = exp(-(nablaE/kappa).^2);
elseif option == 2
cN = 1./(1 + (nablaN/kappa).^2);
cS = 1./(1 + (nablaS/kappa).^2);
cW = 1./(1 + (nablaW/kappa).^2);
cE = 1./(1 + (nablaE/kappa).^2);
end
% Discrete PDE solution.
f_diff = f_diff + ...
lambda*(...
(1/(dy^2))*cN.*nablaN + (1/(dy^2))*cS.*nablaS + ...
(1/(dx^2))*cW.*nablaW + (1/(dx^2))*cE.*nablaE);
end
end
case 'EW'
for t = 1:2
nablaW = imfilter(f_diff,hWf,'conv');
nablaE = imfilter(f_diff,hEf,'conv');
if option == 1
cW = exp(-(nablaW/(normalize*kappa)).^2);
cE = exp(-(nablaE/(normalize*kappa)).^2);
elseif option == 2
cW = 1./(1 + (nablaW/(1/normalize*kappa)).^2);
cE = 1./(1 + (nablaE/(1/normalize*kappa)).^2);
end
f_diff = f_diff + ...
lambda*((1/(dx^2))*cW.*nablaW + (1/(dx^2))*cE.*nablaE );
end
if niter > 2
for t = 2:niter
nablaW = imfilter(f_diff,hW,'conv');
nablaE = imfilter(f_diff,hE,'conv');
if option == 1
cW = exp(-(nablaW/kappa).^2);
cE = exp(-(nablaE/kappa).^2);
elseif option == 2
cW = 1./(1 + (nablaW/kappa).^2);
cE = 1./(1 + (nablaE/kappa).^2);
end
f_diff = f_diff + ...
lambda*((1/(dx^2))*cW.*nablaW + (1/(dx^2))*cE.*nablaE );
end
end
case 'NS'
for t = 1:2
nablaN = imfilter(f_diff,hNf,'conv');
nablaS = imfilter(f_diff,hSf,'conv');
if option == 1
cN = exp(-(nablaN/(normalize*kappa)).^2);
cS = exp(-(nablaS/(normalize*kappa)).^2);
elseif option == 2
cN = 1./(1 + (nablaN/(1/normalize*kappa)).^2);
cS = 1./(1 + (nablaS/(1/normalize*kappa)).^2);
end
f_diff = f_diff + ...
lambda*((1/(dy^2))*cN.*nablaN + (1/(dy^2))*cS.*nablaS);
end
if niter > 2
for t = 2:niter
nablaN = imfilter(f_diff,hN,'conv');
nablaS = imfilter(f_diff,hS,'conv');
if option == 1
cS = exp(-(nablaS/kappa).^2);
cN = exp(-(nablaN/kappa).^2);
elseif option == 2
cS = 1./(1 + (nablaS/kappa).^2);
cN = 1./(1 + (nablaN/kappa).^2);
end
f_diff = f_diff + ...
lambda*((1/(dy^2))*cN.*nablaN + (1/(dy^2))*cS.*nablaS);
end
end
case 'D'
for t=1:niter
nablaNE = imfilter(f_diff,hNE,'conv');
nablaSE = imfilter(f_diff,hSE,'conv');
nablaSW = imfilter(f_diff,hSW,'conv');
nablaNW = imfilter(f_diff,hNW,'conv');
if option == 1
cNE = exp(-(nablaNE/kappa).^2);
cSE = exp(-(nablaSE/kappa).^2);
cSW = exp(-(nablaSW/kappa).^2);
cNW = exp(-(nablaNW/kappa).^2);
elseif option == 2
cNE = 1./(1 + (nablaNE/kappa).^2);
cSE = 1./(1 + (nablaSE/kappa).^2);
cSW = 1./(1 + (nablaSW/kappa).^2);
cNW = 1./(1 + (nablaNW/kappa).^2);
end
f_diff = f_diff + ...
lambda*((1/(dd^2))*cNE.*nablaNE + ...
(1/(dd^2))*cSE.*nablaSE + ...
(1/(dd^2))*cSW.*nablaSW + (1/(dd^2))*cNW.*nablaNW );
end
end