-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlightCorrection.m
More file actions
49 lines (35 loc) · 1.3 KB
/
Copy pathlightCorrection.m
File metadata and controls
49 lines (35 loc) · 1.3 KB
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
function lightCorrectedIm = lightCorrection(inputImage)
%Light correction, checks if the lightning in the image is
%sufficient. If not, perform grayworld compensation.
%% Step 1, check the distribution of intensities and the top 5%
SortedPixelValues = reshape(inputImage,[],size(inputImage,3),1);
SortedPixelValues = sort(SortedPixelValues);
NumOfPixels = size(SortedPixelValues,1);
MaxPixelValue = SortedPixelValues(NumOfPixels,:);
%Find the scalar number of the N's pixel from max.
Top5Percent = NumOfPixels - floor(NumOfPixels*0.05);
TopPixels = SortedPixelValues(Top5Percent:NumOfPixels,:);
%Get average valeus for red, green and blue channel.
AR = mean(TopPixels(:,1));
AG = mean(TopPixels(:,2));
AB = mean(TopPixels(:,3));
%% Step 2: Test if there is interferance in color
Value = max(max(AR,AG),AB) / min(min(AR,AG),AB);
Acceptance = 0.01;
if ( abs(1-Value) < Acceptance )
lightCorrectedIm = inputImage;
%fprintf('No Color adjustment was done. \n');
return
end
%% Step 3: Color correction, Gray world compensation
R = inputImage(:,:,1);
G = inputImage(:,:,2);
B = inputImage(:,:,3);
RscalVal=sum(sum(R))/numel(R);
GscalVal=sum(sum(G))/numel(G);
BscalVal=sum(sum(B))/numel(B);
R = R*(127.5/RscalVal);
G = G*(127.5/GscalVal);
B = B*(127.5/BscalVal);
lightCorrectedIm = cat(3,R,G,B);
end