% Routines for Obtaining Mean and Standard Deviation Values
% Of Macbeth Color Checker Squares Using ISET
% Written by: Joan Moh
% -----------------------------------------------------------
% First section of code is for analysing pixel raw data.
% Running the code will prompt the user to select each of the 
% 24 squares one by one, then save the means and standard deviations
% in the vectors mean_all and stddev_all
% Second section of code is the similar process but for the jpeg
% image data itself.

[val,vci] = vcGetSelectedObject('VCIMAGE');

mean_all = [];
stddev_all = [];
for i=1:24

roiLocs = vcROISelect(vci);
img = imageGet(vci,'result');
selected = vcImageGetROI(img, roiLocs);
mn = mean(vcImageGetROI(img,roiLocs))
stddev = std(vcImageGetROI(img,roiLocs));

mean_all = [mean_all mn'];
stddev_all = [stddev_all stddev'];

end;

save macbethBlue mean_all stddev_all;

% save macbeth mean_all stddev_all;

load macbethBlue;
mean_all = mean_all';
index = 1:1:24;
plot(index, mean_all(:,1), 'r', index, mean_all(:,2), 'g', index, mean_all(:,3));
title('Mean JPEG Values Across The Macbeth Color Checker');
xlabel('Index of Squares');
ylabel('Mean JPEG Values');
legend('Red Channel', 'Green Channel', 'Blue Channel');
hold on;
errorbar(index, mean_all(:,1), stddev_all(1,:)', 'r')
errorbar(index, mean_all(:,2), stddev_all(2,:)', 'g')
errorbar(index, mean_all(:,3), stddev_all(3,:)', 'b')

figure;
stddev_all = stddev_all';
plot(index, stddev_all(:,1), 'r', index, stddev_all(:,2), 'g', index, stddev_all(:,3));
title('Standard Deviation of JPEG Values Across The Macbeth Color Checker');
xlabel('Index of Squares');
ylabel('Standard Deviation (V)');
legend('Red Channel', 'Green Channel', 'Blue Channel');

figure;
every_mean = [mean_all(:,1); mean_all(:,2); mean_all(:,3)];
every_SD = [stddev_all(:,1); stddev_all(:,2); stddev_all(:,3)];
scatter(every_mean, every_SD)
title('Scatter Plot of JPEG Pixel Standard Deviation against Mean (Individual Channels Considered)');
xlabel('JPEG Pixel Mean');
ylabel('JPEG Pixel Standard Deviation');

figure;
pixel_mean = sum(mean_all');
pixel_mean = pixel_mean';
pixel_sd = sum(stddev_all');
pixel_sd = pixel_sd';
scatter(pixel_mean, pixel_sd)
title('Scatter Plot of JPEG Pixel Standard Deviation against Mean (Summed over 3 Channels per pixel)');
xlabel('JPEG Pixel Mean');
ylabel('JPEG Pixel Standard Deviation');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

mean_all = [];
stddev_all = [];

for i=1:24
    

roi = [];
s = sensorStats(roi, 'basic', 'v');
mean_all = [mean_all s.mean'];
stddev_all = [stddev_all s.std'];

end;
save isetRaw mean_all stddev_all;

save measuredBlueRaw mean_all stddev_all;

save measuredRaw mean_all stddev_all;

load measuredRaw;
load measuredBlueRaw;

mean_all = mean_all';
index = 1:1:24;
plot(index, mean_all(:,1), 'r', index, mean_all(:,2), 'g', index, mean_all(:,3));
title('Mean RGB Sensor Readings Across The Macbeth Color Checker');
xlabel('Index of Squares');
ylabel('Mean Sensor Reading (V)');
legend('Red Sensor', 'Green Sensor', 'Blue Sensor');
index_matrix = [index; index; index];
hold on;
errorbar(index, mean_all(:,1), stddev_all(1,:)', 'r')
errorbar(index, mean_all(:,2), stddev_all(2,:)', 'g')
errorbar(index, mean_all(:,3), stddev_all(3,:)', 'b')

figure;
stddev_all = stddev_all';
plot(index, stddev_all(:,1), 'r', index, stddev_all(:,2), 'g', index, stddev_all(:,3));
title('Standard Deviation of RGB Sensor Readings Across The Macbeth Color Checker');
xlabel('Index of Squares');
ylabel('Standard Deviation (V)');
legend('Red Sensor', 'Green Sensor', 'Blue Sensor');

figure;
every_mean = [mean_all(:,1); mean_all(:,2); mean_all(:,3)];
every_SD = [stddev_all(:,1); stddev_all(:,2); stddev_all(:,3)];
scatter(every_mean, every_SD)
title('Scatter Plot of Sensor Reading Standard Deviation against Mean');
xlabel('Sensor Reading Mean');
ylabel('Sensor Reading Standard Deviation');
