-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhdr.m
93 lines (64 loc) · 3.28 KB
/
hdr.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
% Generates a hdr radiance map from a set of pictures
%
% parameters:
% filenames: a list of filenames containing the differently exposed
% pictures used to make a hdr from
% gRed: camera response function for the red color channel
% gGreen: camera response function for the green color channel
% gBlue: camera response function for the blue color channel
function [ hdr ] = hdr( filenames, gRed, gGreen, gBlue, w, dt )
numExposures = size(filenames,2);
% read the first image to get the width and height information
image = imread(filenames{1});
% pre-allocate resulting hdr image
hdr = zeros(size(image));
sum = zeros(size(image));
for i=1:numExposures
fprintf('Adding picture %i of %i \n', i, numExposures);
image = double(imread(filenames{i}));
wij = w(image + 1);
sum = sum + wij;
m(:,:,1) = (gRed(image(:,:,1) + 1) - dt(1,i));
m(:,:,2) = (gGreen(image(:,:,2) + 1) - dt(1,i));
m(:,:,3) = (gBlue(image(:,:,3) + 1) - dt(1,i));
% If a pixel is saturated, its information and
% that gathered from all prior pictures with longer exposure times is unreliable. Thus
% we ignore its influence on the weighted sum (influence of the
% same pixel from prior pics with longer exposure time ignored as
% well)
saturatedPixels = ones(size(image));
saturatedPixelsRed = find(image(:,:,1) == 255);
saturatedPixelsGreen = find(image(:,:,2) == 255);
saturatedPixelsBlue = find(image(:,:,3) == 255);
% Mark the saturated pixels from a certain channel in *all three*
% channels
dim = size(image,1) * size(image,2);
saturatedPixels(saturatedPixelsRed) = 0;
saturatedPixels(saturatedPixelsRed + dim) = 0;
saturatedPixels(saturatedPixelsRed + 2*dim) = 0;
saturatedPixels(saturatedPixelsGreen) = 0;
saturatedPixels(saturatedPixelsGreen + dim) = 0;
saturatedPixels(saturatedPixelsGreen + 2*dim) = 0;
saturatedPixels(saturatedPixelsBlue) = 0;
saturatedPixels(saturatedPixelsBlue + dim) = 0;
saturatedPixels(saturatedPixelsBlue + 2*dim) = 0;
% add the weighted sum of the current pic to the resulting hdr radiance map
hdr = hdr + (wij .* m);
% remove saturated pixels from the radiance map and the sum (saturated pixels
% are zero in the saturatedPixels matrix, all others are one)
hdr = hdr .* saturatedPixels;
sum = sum .* saturatedPixels;
end
% For those pixels that even in the picture with the smallest exposure time still are
% saturated we approximate the radiance only from that picture instead
% of taking the weighted sum
saturatedPixelIndices = find(hdr == 0);
% Don't multiply with the weights since they are zero for saturated
% pixels. m contains the logRadiance value from the last pic, that one
% with the longest exposure time.
hdr(saturatedPixelIndices) = m(saturatedPixelIndices);
% Fix the sum for those pixels to avoid division by zero
sum(saturatedPixelIndices) = 1;
% normalize
hdr = hdr ./ sum;
hdr = exp(hdr);