-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReinhard.m
146 lines (117 loc) · 3.17 KB
/
Reinhard.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
% Getting the Input image
Img = hdrread('memorial.hdr');
[r,c,h] = size(Img);
% Luminance matrix
Lum = zeros(r,c);
for i=1:r
for j=1:c
Lum(i,j) = Img(i,j,1)*0.299 + Img(i,j,2)*0.587 + Img(i,j,3)*0.114;
end
end
% Performing the required operations on the Luminance values
delta = 0.00001;
sum = 0;
for i=1:r
for j=1:c
sum = sum + log(delta + Lum(i,j));
end
end
Lw = exp(sum/(r*c));
L = zeros(r,c);
a = 0.045;
for i=1:r
for j=1:c
L(i,j) = (a/Lw)*Lum(i,j);
end
end
% Defining the constants as directed in the paper
alpha1 = 1/(2*(sqrt(2)));
alpha2 = 1.6*alpha1;
epsilon = 0.05;
phi = 15.0;
R1 = zeros(r,c,9);
R2 = zeros(r,c,9);
for x=1:r
for y=1:c
for s=1:9
sS = 1.6^(s-1);
R1(x,y,s) = (1/(pi*(alpha1*sS)^2))*(exp(-(x^2 + y^2)/(alpha1 * sS)^2));
R2(x,y,s) = (1/(pi*(alpha2*sS)^2))*(exp(-(x^2 + y^2)/(alpha2 * sS)^2));
end
end
end
% Performing the required Fourier Transforms
for s=1:9
V1(:,:,s) = ifft(fft(L).*fft(R1(:,:,s)));
V2(:,:,s) = ifft(fft(L).*fft(R1(:,:,s)));
end
V = zeros(r,c,9);
for x=1:r
for y=1:c
for s=1:9
sS = 1.6^(s-1);
V(x,y,s) = (V1(x,y,s)-V2(x,y,s))/(((2^phi)*((a)/(sS*sS)))+V1(x,y,s));
end
end
end
Ld = zeros(r,c);
for i=1:r
for j=1:c
sS = 1;
for s=1:9
if (abs(V(i,j,s)))<epsilon
break;
end
if s~=9
sS = sS * 1.6;
end
end
p = 1 + round(log(sS)/log(1.6));
Ld(i,j) = L(i,j)/(1+V1(i,j,p));
end
end
% Getting the output Image using the transformations done earlier
ImgOut = zeros(r,c,h);
for i = 1:r
for j = 1:c
for k =1:h
ImgOut(i,j,1) = (((Img(i,j,1))/(Lum(i,j))))*(Ld(i,j));
ImgOut(i,j,2) = (((Img(i,j,2))/(Lum(i,j))))*(Ld(i,j));
ImgOut(i,j,3) = (((Img(i,j,3))/(Lum(i,j))))*(Ld(i,j));
end
end
end
imshow(ImgOut);
% The unscaled Gamma function
function A = unscaledGamma(ImgOutSimple,r,c,h)
ImgOutGamma = zeros(r,c,h);
for i=1:r
for j = 1:c
for k = 1:h
if (ImgOutSimple(i,j,k))<=0.0031308
ImgOutGamma(i,j,k) = 12.92*(ImgOutSimple(i,j,k));
else
ImgOutGamma(i,j,k) = (((ImgOutSimple(i,j,k))^(1/2.4))*1.055 + (-0.055))*1;
end
end
end
end
A = ImgOutGamma;
end
% The scaled gamma function
function A = gamma(ImgOutSimple,r,c,h)
ImgOutGamma = zeros(r,c,h);
for i=1:r
for j = 1:c
for k = 1:h
if (ImgOutSimple(i,j,k)/255)<=0.0031308
ImgOutGamma(i,j,k) = 12.92*(ImgOutSimple(i,j,k)/255);
else
ImgOutGamma(i,j,k) = (((ImgOutSimple(i,j,k)/255)^(1/2.4))*1.055 + (-0.055))*1;
end
ImgOutGamma(i,j,k) = ImgOutGamma(i,j,k)*255;
end
end
end
A = ImgOutGamma;
end