% Compute the SNR ratio of the digital image
% Shutterfly pictures

clear;
% load the image

I41 = imread('original41.jpg', 'jpg');
I42 = imread('original42.jpg', 'jpg');
I43 = imread('original43.jpg', 'jpg');
I44 = imread('original44.jpg', 'jpg');
I45 = imread('original45.jpg', 'jpg');
I46 = imread('original46.jpg', 'jpg');


% find out the dimension
[ M, N ] = size( I41(:, :, 1) );

% Isolate the R, G, B, components

G41 = double( I41(:, :, 2) );

G42 = double( I42(:, :, 2) );

G43 = double( I43(:, :, 2) );

G44 = double( I44(:, :, 2) );

G45 = double( I45(:, :, 2) );


G46 = double( I46(:, :, 2) );


Noise_G42 = G42 - G41;


Noise_G43 = G43 - G41;


Noise_G44 = G44 - G41;


Noise_G45 = G45 - G41;

Noise_G46 = G46 - G41;


% Large Area Signal to Noise Ratio

incremental_gain = 0.1;
maximum_level = 255;


% Green component
sumNG42 = 0;
sumNG43 = 0;
sumNG44 = 0;
sumNG45 = 0;
sumNG46 = 0;

for counter1 = 1: 1: M
   for counter2 = 1: 1: N
      sumNG42 = sumNG42 + Noise_G42(counter1, counter2) * Noise_G42(counter1, counter2);
      sumNG43 = sumNG43 + Noise_G43(counter1, counter2) * Noise_G43(counter1, counter2);
      sumNG44 = sumNG44 + Noise_G44(counter1, counter2) * Noise_G44(counter1, counter2);
      sumNG45 = sumNG45 + Noise_G45(counter1, counter2) * Noise_G45(counter1, counter2);
      sumNG46 = sumNG46 + Noise_G46(counter1, counter2) * Noise_G46(counter1, counter2);
   end;
end;

sumNG42 = sumNG42 / ( M * N );
sumNG43 = sumNG43 / ( M * N );
sumNG44 = sumNG44 / ( M * N );
sumNG45 = sumNG45 / ( M * N );
sumNG46 = sumNG46 / ( M * N );

AVG_totalNG42 = sqrt( sumNG42 );
AVG_totalNG43 = sqrt( sumNG43 );
AVG_totalNG44 = sqrt( sumNG44 );
AVG_totalNG45 = sqrt( sumNG45 );
AVG_totalNG46 = sqrt( sumNG46 );


SNR_G42 = maximum_level * 0.18 / ( AVG_totalNG42 * incremental_gain );
SNR_G43 = maximum_level * 0.18 / ( AVG_totalNG43 * incremental_gain );
SNR_G44 = maximum_level * 0.18 / ( AVG_totalNG44 * incremental_gain );
SNR_G45 = maximum_level * 0.18 / ( AVG_totalNG45 * incremental_gain );
SNR_G46 = maximum_level * 0.18 / ( AVG_totalNG46 * incremental_gain );

SNR_G = [  SNR_G42 SNR_G43 SNR_G44 SNR_G45 SNR_G46 ]



figure(1);
subplot(2, 1, 1);
plot(SNR_G );
title(' Large Area Signal to Noise Ratio Green component ' );
grid;
subplot( 2, 1, 2);
bar( SNR_G );
title(' Bar garph ');
grid;



% Fixed pattern Signal to Noise Ratio


%Fix_SNR_R41 = maximum_level * 0.18 / ( Avg_fix_NR41 * incremental_gain );
%Fix_SNR_R42 = maximum_level * 0.18 / ( Avg_fix_NR42 * incremental_gain );
%Fix_SNR_R43 = maximum_level * 0.18 / ( Avg_fix_NR43 * incremental_gain );
%Fix_SNR_R44 = maximum_level * 0.18 / ( Avg_fix_NR44 * incremental_gain );
%Fix_SNR_R45 = maximum_level * 0.18 / ( Avg_fix_NR45 * incremental_gain );
%Fix_SNR_R46 = maximum_level * 0.18 / ( Avg_fix_NR46 * incremental_gain );

%Fix_SNR_R = [ Fix_SNR_R41 Fix_SNR_R42 Fix_SNR_R43 Fix_SNR_R44 Fix_SNR_R45 Fix_SNR_R46 ];
%figure(4);
%subplot(2, 1, 1);
%plot( Fix_SNR_R );
%title(' Fixed Pattern Signal to Noise Ratio -- Red  Shutterfly ');
%grid;
%subplot(2, 1, 2 );
%bar( Fix_SNR_R );
%grid;










