-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHDR_using_Inverted_local_patterns.m
319 lines (145 loc) · 4.31 KB
/
HDR_using_Inverted_local_patterns.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
%HDR image generation using a single image using Inverted Local Patterns
I=imread('src.jpg');%input image
% LAB = rgb2lab(I);
% L = LAB(:,:,1)/100;
% L = adapthisteq(L,'NumTiles',[8 8],'ClipLimit',0.005);
% LAB(:,:,1) = L*100;
% img = lab2rgb(LAB);
img2=imresize(I,[256,256]);%resize image to 256x256 resolution
[x,y,z]=size(img2);
img_grey=rgb2gray(img2);%convert RGB image to grayscale image
[hist,o]=imhist(img_grey);%hist represents luminicance histogram
HB=0;%HB value of original image
for i=1:1:256
HB=HB+abs(hist(i)-256);
end
% here we set some thresholds th1,th2,th3,th4 and thdark
th1=16;
th2=51;
th3=206;
th4=241;
thdark=0;
%Now we calculate values of 'low' and 'high'.
%These will determine whether original image is dark,bright,extreme or
%normal.
low=0;
high=0;
for i=1:1:th1-1
low=low+hist(i)*2;
end
for i=th1:1:th2-1
low=low+hist(i);
end
for i=th3:1:th4-1
high=high+hist(i);
end
for i=th4:1:256
high=high+hist(i)*2;
end
if low>high*2
thdark=100; %dark image
elseif high>low*2
thdark=0; %bright image
elseif high/low<=2 && high/low>=0.5
thdark=50; %extreme image
else
return; %original image is already HDR
end
%Here we apply 2x2 maximum fill on the original image
blk=[2,2];
fun = @(s)max(s.data(:))*ones(blk,class(s.data));
img_max=blockproc(img_grey,blk,fun); %img_max is the 2x2 maximum filled image
%Here we apply a 3x3 low pass on img_max
img_low = imfilter(img_max,fspecial('average',[3 3]));
%Now we apply inverse operation on img_low
%After the inverse operation, bright regions become dark and dark regions
%become bright
img_inv=img_low;
for i=1:1:x
for j=1:1:y
p=double(255-img_low(i,j));
p=p*p;
img_inv(i,j)=p/255;
end
end
%Dark enhancement
%Here we brighten the darker regions of the original image
%If pixel value is less than thdark,then we increase its brightness
%Otherwise it remains same
img_dark=img_grey;
for i=1:1:x
for j=1:1:y
if img_grey(i,j)<thdark
temp=double(img_grey(i,j))+double((thdark-img_grey(i,j))*0.072);
img_dark(i,j)=temp;
else
img_dark(i,j)=img_grey(i,j);
end
end
end
%Mixing operation
%Here we mix the results of inverse operation and dark enhancement
%We use a parameter k which depends on type of image.
%More the darkness,more the value of k and vice-versa.
k=1.82*power(10,-8)*low+0.009;
img_mix=img_dark;
for i=1:1:x
for j=1:1:y
temp=double(img_dark(i,j))*double(img_inv(i,j));
temp=temp*k;
img_mix(i,j)=temp;
end
end
%Brightness enhancement
%Here we increase brightness of img_mix to get our HDR image
%We set a threshold thb for brightness enhancement
%If pixel value is greater than thdb,then we increase its brightness
%Otherwise it remains same
thb=50;
img_hdr=img_mix;
for i=1:1:x
for j=1:1:y
if img_dark(i,j)>=thb
p=double(img_dark(i,j)-thb);
p=p*p;
p=p*0.0068;
img_hdr(i,j)=img_mix(i,j)+p;
else
img_hdr(i,j)=img_mix(i,j);
end
end
end
[hist_hdr,o1]=imhist(img_hdr);%hist_hdr represents luminicance histogram of HDR image
HB1=0;%HB value of output image
for i=1:1:256
HB1=HB1+abs(hist_hdr(i)-256);
end
%Newly included part
%img_hdr2=imsharpen(img_hdr);
%[hist_hdr2,o3]=imhist(img_hdr2);
%HB2=0;
%for i=1:1:256
%HB2=HB2+abs(hist_hdr2(i)-256);
%end
img_des0=imread('res.jpg');%Proposed image
img_des=imresize(img_des0,[256,256]);
img_des=rgb2gray(img_des);
[hist_hdr2,o2]=imhist(img_des);%hist_hdr2 represents luminicance histogram of Proposed image
HB3=0;%HB value of proposed image
for i=1:1:256
HB3=HB3+abs(hist_hdr2(i)-256);
end
%Finally we display original image,output image and proposed image for
%comparison
figure;
imshow(img_grey)
figure;
imshow(img_hdr)
figure;
imshow(img_des)
figure;
imhist(img_grey(:,:,1))
figure;
imhist(img_hdr(:,:,1))
figure;
imhist(img_des(:,:,1))