-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtonemapping.m
265 lines (200 loc) · 6.77 KB
/
tonemapping.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
% Reading the Input HDR image. Change it as per the needs of yours.
Img = hdrread('memorial.hdr');
[r,c,h] = size(Img);
maxRGB = max(max(Img));
minRGB = min(min(Img));
ImgOutSimple = zeros(r,c,h);
ImgOutLow = zeros(r,c,h);
ImgOutHigh = zeros(r,c,h);
ImgOutGamma = zeros(r,c,h);
% Linear rescaling
for i=1:r
for j = 1:c
for k = 1:h
ImgOutSimple(i,j,k) = ((Img(i,j,k)- minRGB(k))/(maxRGB(k)-minRGB(k)))*255;
ImgOutLow(i,j,k) = ((Img(i,j,k)- minRGB(k))/(maxRGB(k)-minRGB(k)))*100;
ImgOutHigh(i,j,k) = ((Img(i,j,k)- minRGB(k))/(maxRGB(k)-minRGB(k)))*1000;
end
end
end
% Applying the Gamma correction for the Linearly rescaled value of the
% range 0 - 255
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
% Luminance matrix
ImgOutLum = zeros(r,c,h);
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
% Log luminance matrix
Lumlog = zeros(r,c);
for i=1:r
for j = 1: c
Lumlog(i,j) = log10(Lum(i,j));
end
end
maxVal = max(max(Lumlog));
minVal = min(min(Lumlog));
% Scaling the Log luminance to the range -1 -> 1 => Luminance is scaled as 0.1 -> 10
for i=1:r
for j = 1: c
Lumlog(i,j) = ((Lumlog(i,j)-minVal)/(maxVal-minVal))*2-1;
Lumlog(i,j) = 10^(Lumlog(i,j));
end
end
% Using the constancy of R/L, B/L, G/L to recover the colored image from
% the scaled Luminance matrix
for i = 1:r
for j = 1:c
for k =1:h
ImgOutLum(i,j,1) = (((Img(i,j,1))/(Lum(i,j))))*(Lumlog(i,j));
ImgOutLum(i,j,2) = (((Img(i,j,2))/(Lum(i,j))))*(Lumlog(i,j));
ImgOutLum(i,j,3) = (((Img(i,j,3))/(Lum(i,j))))*(Lumlog(i,j));
end
end
end
% Writing the corrected image
imshow(ImgOutLum);
%Convolution matrices for the Different filters
A = 1.5;
Sharpen = [0,-1,0;-1,5,-1;0,-1,0];
Laplace = [0,1,0; 1 , -4,1; 0,1,0];
Highboost = [-1,-1,-1;-1,A+8,-1;-1,-1,-1];
Sobel = [-1,-2,-1;0,0,0;1,2,1];
% Using the Unsharp Masking technique
ImgSharpen = convol(ImgOutLum,r,c,Sharpen);
%imshow(unscaledGamma(ImgSharpen,r-2,c-2,h));
% Using the Laplace Technique
ImgLap = convol(ImgOutLum, r,c,Laplace);
%imshow(unscaledGamma(ImgLap,r-2,c-2,h));
% Using the Highboost filtering
ImgBoost = convol(ImgOutSimple,r,c,Highboost);
%imshow(ImgBoost);
% Using the Gradient filter
ImgGrad = convol(ImgOutLum,r,c,Sobel);
%imshow(unscaledGamma(ImgGrad,r-2,c-2,h));
% Using contrast stretching
imshow(contrast(ImgOutLum,r,c,h,255/3,2*255/3,255/6,5*255/6));
%imshow(unscaledGamma(contrast(ImgOutSimple,r,c,h,255/3,2*255/3,255/6,5*255/6),r,c,h));
%Using Histogram equalization
imshow(unscaledGamma(histo(ImgOutLum,r,c,h),r,c,h));
% Convoluting Image Bmat by the filter Cmat
function A = convol(Bmat,r,c,Cmat)
Res = zeros(r-2,c-2,3);
for i=1:(r-2)
for j = 1:(c-2)
for k=1:3
sum = 0;
for u=0:2
for v=0:2
sum = sum + Bmat(i+u,j+v,k)*Cmat(3-u,3-v);
end
end
Res(i,j,k) = sum;
end
end
end
A = Res;
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
% The function performing Contrast stretching. The scale is divided by four points
% They are: (0,0), (r1,s1), (r2,s2), and (255,255). Linear division takes place
function A = contrast(Img,r,c,h,r1,r2,s1,s2)
A = zeros(r,c,h);
for i=1:r
for j=1:c
for k=1:h
if Img(i,j,k)<r1
A(i,j,k)=Img(i,j,k)/s1;
elseif Img(i,j,k)<r2
A(i,j,k) = s1 + ((Img(i,j,k)-r1)/(r2-r1))*(s2-s1);
else
A(i,j,k) = s2 + ((Img(i,j,k)-r2)/(255-r2))*(255-s2);
end
end
end
end
end
% The function performing Histogram Equalisation
function A = histo(Img,r,c,h)
minVal = floor(min(min(Img)));
maxVal = floor(max(max(Img)));
arr1 = zeros(maxVal(1)-minVal(1)+1);
arr2 = zeros(maxVal(2)-minVal(2)+1);
arr3 = zeros(maxVal(3)-minVal(3)+1);
A = zeros(r,c,h);
for i=1:r
for j=1:c
arr1(floor(Img(i,j,1))-minVal(1)+1) = arr1(floor(Img(i,j,1))-minVal(1)+1)+1;
arr2(floor(Img(i,j,2))-minVal(2)+1) = arr2(floor(Img(i,j,2))-minVal(2)+1)+1;
arr3(floor(Img(i,j,3))-minVal(3)+1) = arr3(floor(Img(i,j,3))-minVal(3)+1)+1;
end
end
for i=1:(maxVal(1)-minVal(1)+1)
if i~=1
arr1(i) = arr1(i-1)+arr1(i);
end
end
for i=1:(maxVal(2)-minVal(2)+1)
if i~=1
arr2(i) = arr2(i-1)+arr2(i);
end
end
for i=1:(maxVal(3)-minVal(3)+1)
if i~=1
arr3(i) = arr3(i-1)+arr3(i);
end
end
for i=1:r
for j=1:c
A(i,j,1) = ((arr1(floor(Img(i,j,1))-minVal(1)+1)-arr1(1))/((r*c)-arr1(1)))*255;
A(i,j,2) = ((arr2(floor(Img(i,j,2))-minVal(2)+1)-arr2(1))/((r*c)-arr2(1)))*255;
A(i,j,3) = ((arr3(floor(Img(i,j,3))-minVal(3)+1)-arr3(1))/((r*c)-arr3(1)))*255;
end
end
end
% 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