-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathestA.m
65 lines (55 loc) · 1.73 KB
/
estA.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
function [ A ] = estA( img, Jdark, isShowImg)
%ESTABYTRAN Summary of this function goes here
if nargin < 3
isShowImg = 1;
end
% Estimate Airlight of image I
[h,w,c] = size(img);
if isinteger(img)
img = double(img)/255;
end
% Compute number for 0.1% brightest pixels
n_bright = ceil(0.001*h*w);
% Loc contains the location of the sorted pixels
[Y,Loc] = sort(Jdark(:));
%column-stacked version of I
Ics = reshape(img, h*w, 1, 3);
ix = img;
dx = Jdark(:);
%init a matrix to store candidate airlight pixels
Acand = zeros(n_bright,1,3);
%init matrix to store largest norm airlight
Amag = zeros(n_bright,1);
% Compute magnitudes of RGB vectors of A
for i = 1:n_bright
x = Loc(h*w+1-i);
%ix(mod(x,h)+1, floor(x/w)+1, 1) = 1;
%ix(mod(x,h)+1, floor(x/w)+1, 2) = 0;
%ix(mod(x,h)+1, floor(x/w)+1, 3) = 0;
ix(mod(x,h)+1, floor(x/h)+1, 1) = 1;
ix(mod(x,h)+1, floor(x/h)+1, 2) = 0;
ix(mod(x,h)+1, floor(x/h)+1, 3) = 0;
%Jdark(mod(x,h), floor(x/w)+1);
%dx(x);
Acand(i,1,:) = Ics(Loc(h*w+1-i),1,:);
Amag(i) = norm(Acand(i,:));
end
% Sort A magnitudes
[Y2,Loc2] = sort(Amag(:));
% A now stores the best estimate of the airlight
if length(Y2) > 20
A = Acand(Loc2(n_bright-19:n_bright),:);
else
A = Acand(Loc2(n_bright-length(Y2)+1:n_bright),:);
end
% finds the max of the 20 brightest pixels in original image
if size(A,1)~=1
A = max(A);
end
if isShowImg == 1
figure;
imshow(ix);
title('position of the atmospheric light');
imwrite(ix, 'res/position_of_the_atmospheric_light.png');
end
end