-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_awb.m
More file actions
61 lines (46 loc) · 1.73 KB
/
simple_awb.m
File metadata and controls
61 lines (46 loc) · 1.73 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
49
50
51
52
53
54
55
56
57
58
59
60
61
% =========================================================================
% Project: Simple Auto-White Balance (Gray World Algorithm)
% Author: Pranav Aravindhan V
% =========================================================================
clc; clear;
inputImage = imread('test_image.jpg');
I = im2double(inputImage);
R = I(:,:,1);
G = I(:,:,2);
B = I(:,:,3);
avgR = mean(R, 'all');
avgG = mean(G, 'all');
avgB = mean(B, 'all');
fprintf("Average R: %.4f | Average G: %.4f | Average B: %.4f\n", avgR, avgG, avgB);
globalMean = (avgR + avgG + avgB)/3;
% Calculate the gain factors for each channel
gainR = globalMean / avgR;
gainG = globalMean / avgG;
gainB = globalMean / avgB;
fprintf("Gains Applied: R: %.4f | G: %.4f | B: %.4f\n", gainR, gainG, gainB);
balR = R * gainR;
balG = G * gainG;
balB = B * gainB;
%clipping
balR(balR > 1) = 1;
balG(balG > 1) = 1;
balB(balB > 1) = 1;
balancedImage = cat(3, balR, balG, balB);
figure('Name', 'AWB Algorithm');
subplot(1,2,1);
imshow(I);
title('Original (Color Cast)');
subplot(1,2,2);
imshow(balancedImage);
title('Corrected (Gray World AWB)');
figure('Name', 'Channel Histograms');
subplot(2, 1, 1);
histogram(R, 'FaceColor', 'r', 'EdgeColor', 'none', 'FaceAlpha', 0.5); hold on;
histogram(G, 'FaceColor', 'g', 'EdgeColor', 'none', 'FaceAlpha', 0.5);
histogram(B, 'FaceColor', 'b', 'EdgeColor', 'none', 'FaceAlpha', 0.5);
title('Original Histogram (Misaligned)');
subplot(2, 1, 2);
histogram(balR, 'FaceColor', 'r', 'EdgeColor', 'none', 'FaceAlpha', 0.5); hold on;
histogram(balG, 'FaceColor', 'g', 'EdgeColor', 'none', 'FaceAlpha', 0.5);
histogram(balB, 'FaceColor', 'b', 'EdgeColor', 'none', 'FaceAlpha', 0.5);
title('Corrected Histogram (Aligned)');