-
Notifications
You must be signed in to change notification settings - Fork 0
/
aniso_subspace_decomp.m
130 lines (91 loc) · 2.86 KB
/
aniso_subspace_decomp.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
'New'
ig = imread('lena.jpg');
if ndims(ig)==3
ig = rgb2gray(ig);
end
[wA1,wH1,wV1,wD1] = dwt2(ig,'db4');
im = imnoise(ig,'gaussian',0,0.01);
[psnr(uint8(im),uint8(ig))]
niter = 25;
kappa = 20;
option = 2;
lambda = 0.50;
[rows,cols] = size(ig);
[wA,wH,wV,wD] = dwt2(im,'db4');
wA = double(wA);
wH = double(wH);
wV = double(wV);
wD = double(wD);
% wA = medfilt2(wA,[3,3]);
% wH = medfilt2(wH,[3,1]);
% wV = medfilt2(wV,[1,3]);
% wD = medfilt2(wD,[2,2]);
imgH = idwt2(wA,wH,zeros(size(wV)),zeros(size(wD)),'db4');
imgV = idwt2(wA,zeros(size(wH)),wV,zeros(size(wD)),'db4');
imgD = idwt2(wA,zeros(size(wH)),zeros(size(wV)),wD,'db4');
for j = 1:niter
diff_H = imgH;
for i = 1:j
diffl = zeros(rows+2, cols+2);
diffl(2:rows+1, 2:cols+1) = diff_H;
% North, South diffusion
deltaN = diffl(1:rows,2:cols+1) - diff_H;
deltaS = diffl(3:rows+2,2:cols+1) - diff_H;
if option == 1
cN = exp(-(deltaN/kappa).^2);
cS = exp(-(deltaS/kappa).^2);
elseif option == 2
cN = 1./(1 + (deltaN/kappa).^2);
cS = 1./(1 + (deltaS/kappa).^2);
end
diff_H = diff_H + lambda*(cN.*deltaN + cS.*deltaS);
end
diff_V = imgV;
for k = 1:j
diffl = zeros(rows+2, cols+2);
diffl(2:rows+1, 2:cols+1) = diff_V;
deltaE = diffl(2:rows+1,3:cols+2) - diff_V;
deltaW = diffl(2:rows+1,1:cols) - diff_V;
if option == 1
cE = exp(-(deltaE/kappa).^2);
cW = exp(-(deltaW/kappa).^2);
elseif option == 2
cE = 1./(1 + (deltaE/kappa).^2);
cW = 1./(1 + (deltaW/kappa).^2);
end
diff_V = diff_V + lambda*(cE.*deltaE + cW.*deltaW);
end
diff_D = imgD;
for k = 1:j
dx = 1;
dy = 1;
dd = sqrt(2);
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];
deltaNE = imfilter(diff_D,hNE,'conv');
deltaSE = imfilter(diff_D,hSE,'conv');
deltaSW = imfilter(diff_D,hSW,'conv');
deltaNW = imfilter(diff_D,hNW,'conv');
% Diffusion function.
if option == 1
cNE = exp(-(deltaNE/kappa).^2);
cSE = exp(-(deltaSE/kappa).^2);
cSW = exp(-(deltaSW/kappa).^2);
cNW = exp(-(deltaNW/kappa).^2);
elseif option == 2
cNE = 1./(1 + (deltaNE/kappa).^2);
cSE = 1./(1 + (deltaSE/kappa).^2);
cSW = 1./(1 + (deltaSW/kappa).^2);
cNW = 1./(1 + (deltaNW/kappa).^2);
end
% Discrete PDE solution.
diff_D = diff_D + ...
lambda*(...
(1/(dd^2))*cNE.*deltaNE + (1/(dd^2))*cSE.*deltaSE + ...
(1/(dd^2))*cSW.*deltaSW + (1/(dd^2))*cNW.*deltaNW );
end
img_Rec = (0.8*diff_H +0.8* diff_V +1.4* diff_D)/3;
[psnr(uint8(anisodiff(im,j,kappa,lambda,option)),uint8(ig)),psnr(uint8(img_Rec),uint8(ig))]
end