clear;
% load the image
Original = imread('access41.jpg', 'jpg');
Fly = imread('access42.jpg', 'jpg');
OriginalR = Original(:, :, 1);
OriginalG = Original(:, :, 2);
OriginalB = Original(:, :, 3);
R = Fly(:, :, 1);
G = Fly(:, :, 2);
B = Fly(:, :, 3);

R = double(R) - double(OriginalR);
G = double(G) - double(OriginalG);
B = double(B) - double(OriginalB);


[M, N ] = size(R);

% Red component
R_block1 = R( 1: round(M/4), 1: round(N/4));
R_block2 = R( 1: round(M/4), round(N/4): round(N/2));
R_block3 = R( 1: round(M/4), round(N/2): 3*round(N/4));
R_block4 = R( 1: round(M/4), 3*round(N/4): N);
R_block5 = R(round(M/4): round(M/2), 1: round(N/4));
R_block6 = R(round(M/4): round(M/2), round(N/4): round(N/2));
R_block7 = R(round(M/4): round(M/2), round(N/2): 3*round(N/4));
R_block8 = R(round(M/4): round(M/2), 3*round(N/4): N);
R_block9 = R(round(M/2): 3*round(M/4), 1: round(N/4));
R_block10 =R(round(M/2): 3*round(M/4), round(N/4): round(N/2));
R_block11= R(round(M/2): 3*round(M/4), round(N/2): 3*round(N/4));
R_block12 =R(round(M/2): 3*round(M/4), 3*round(N/4): N);
R_block13 =R(3*round(M/4): M, 1: round(N/4));
R_block14 =R(3*round(M/4): M, round(N/4): round(N/2));
R_block15 =R(3*round(M/4): M, round(N/2): 3*round(N/4));
R_block16 =R(3*round(M/4): M, 3*round(N/4): N);


% Green component
G_block1 = G( 1: round(M/4), 1: round(N/4));
G_block2 = G( 1: round(M/4), round(N/4): round(N/2));
G_block3 = G( 1: round(M/4), round(N/2): 3*round(N/4));
G_block4 = G( 1: round(M/4), 3*round(N/4): N);
G_block5 = G(round(M/4): round(M/2), 1: round(N/4));
G_block6 = G(round(M/4): round(M/2), round(N/4): round(N/2));
G_block7 = G(round(M/4): round(M/2), round(N/2): 3*round(N/4));
G_block8 = G(round(M/4): round(M/2), 3*round(N/4): N);
G_block9 = G(round(M/2): 3*round(M/4), 1: round(N/4));
G_block10 =G(round(M/2): 3*round(M/4), round(N/4): round(N/2));
G_block11= G(round(M/2): 3*round(M/4), round(N/2): 3*round(N/4));
G_block12 =G(round(M/2): 3*round(M/4), 3*round(N/4): N);
G_block13 =G(3*round(M/4): M, 1: round(N/4));
G_block14 =G(3*round(M/4): M, round(N/4): round(N/2));
G_block15 =G(3*round(M/4): M, round(N/2): 3*round(N/4));
G_block16 =G(3*round(M/4): M, 3*round(N/4): N);

% Blue component
B_block1 = B( 1: round(M/4), 1: round(N/4));
B_block2 = B( 1: round(M/4), round(N/4): round(N/2));
B_block3 = B( 1: round(M/4), round(N/2): 3*round(N/4));
B_block4 = B( 1: round(M/4), 3*round(N/4): N);
B_block5 = B(round(M/4): round(M/2), 1: round(N/4));
B_block6 = B(round(M/4): round(M/2), round(N/4): round(N/2));
B_block7 = B(round(M/4): round(M/2), round(N/2): 3*round(N/4));
B_block8 = B(round(M/4): round(M/2), 3*round(N/4): N);
B_block9 = B(round(M/2): 3*round(M/4), 1: round(N/4));
B_block10 =B(round(M/2): 3*round(M/4), round(N/4): round(N/2));
B_block11= B(round(M/2): 3*round(M/4), round(N/2): 3*round(N/4));
B_block12 =B(round(M/2): 3*round(M/4), 3*round(N/4): N);
B_block13 =B(3*round(M/4): M, 1: round(N/4));
B_block14 =B(3*round(M/4): M, round(N/4): round(N/2));
B_block15 =B(3*round(M/4): M, round(N/2): 3*round(N/4));
B_block16 =B(3*round(M/4): M, 3*round(N/4): N);



% Noise analysis 1. Compute the mean and Standard deviation% 
% statiscal data of the images

% RGB mean values
Avgblock1_R = mean2( R_block1 );
Avgblock2_R = mean2( R_block2 );
Avgblock3_R = mean2( R_block3 );
Avgblock4_R = mean2( R_block4 );
Avgblock5_R = mean2( R_block5 );
Avgblock6_R = mean2( R_block6 );
Avgblock7_R = mean2( R_block7 );
Avgblock8_R = mean2( R_block8 );
Avgblock9_R = mean2( R_block9 );
Avgblock10_R = mean2( R_block10 );
Avgblock11_R = mean2( R_block11 );
Avgblock12_R = mean2( R_block12 );
Avgblock13_R = mean2( R_block13 );
Avgblock14_R = mean2( R_block14 );
Avgblock15_R = mean2( R_block15 );
Avgblock16_R = mean2( R_block16 );

Avgblock1_G = mean2( G_block1 );
Avgblock2_G = mean2( G_block2 );
Avgblock3_G = mean2( G_block3 );
Avgblock4_G = mean2( G_block4 );
Avgblock5_G = mean2( G_block5 );
Avgblock6_G = mean2( G_block6 );
Avgblock7_G = mean2( G_block7 );
Avgblock8_G = mean2( G_block8 );
Avgblock9_G = mean2( G_block9 );
Avgblock10_G = mean2( G_block10 );
Avgblock11_G = mean2( G_block11 );
Avgblock12_G = mean2( G_block12 );
Avgblock13_G = mean2( G_block13 );
Avgblock14_G = mean2( G_block14 );
Avgblock15_G = mean2( G_block15 );
Avgblock16_G = mean2( G_block16 );

Avgblock1_B = mean2( B_block1 );
Avgblock2_B = mean2( B_block2 );
Avgblock3_B = mean2( B_block3 );
Avgblock4_B = mean2( B_block4 );
Avgblock5_B = mean2( B_block5 );
Avgblock6_B = mean2( B_block6 );
Avgblock7_B = mean2( B_block7 );
Avgblock8_B = mean2( B_block8 );
Avgblock9_B = mean2( B_block9 );
Avgblock10_B = mean2( B_block10 );
Avgblock11_B = mean2( B_block11 );
Avgblock12_B = mean2( B_block12 );
Avgblock13_B = mean2( B_block13 );
Avgblock14_B = mean2( B_block14 );
Avgblock15_B = mean2( B_block15 );
Avgblock16_B = mean2( B_block16 );



AVG_R = [ Avgblock1_R Avgblock2_R Avgblock3_R Avgblock4_R; Avgblock5_R Avgblock6_R Avgblock7_R Avgblock8_R;Avgblock9_R Avgblock10_R Avgblock11_R Avgblock12_R; Avgblock13_R Avgblock14_R Avgblock15_R Avgblock16_R ];
AVG_G = [ Avgblock1_G Avgblock2_G Avgblock3_G Avgblock4_G; Avgblock5_G Avgblock6_G Avgblock7_G Avgblock8_G;Avgblock9_R Avgblock10_G Avgblock11_G Avgblock12_G; Avgblock13_G Avgblock14_G Avgblock15_G Avgblock16_G ];
AVG_B = [ Avgblock1_B Avgblock2_B Avgblock3_B Avgblock4_B; Avgblock5_B Avgblock6_B Avgblock7_B Avgblock8_B;Avgblock9_B Avgblock10_B Avgblock11_B Avgblock12_B; Avgblock13_B Avgblock14_B Avgblock15_B Avgblock16_B ];
figure(1);
bar3(AVG_R, 'detached' );
title(' Distribution of Noise Average- Red ');
grid;

figure(2);
bar3(AVG_G, 'detached' );
title(' Distribution of Noise Average- Green ');
grid;

figure(3);
bar3( AVG_B, 'detached' );
title(' Distribution of Noise Average - Blue ');
grid;


% Standard Deviation of the noise
stdblock1_R = std2( R_block1 );
stdblock2_R = std2( R_block2 );
stdblock3_R = std2( R_block3 );
stdblock4_R = std2( R_block4 );
stdblock5_R = std2( R_block5 );
stdblock6_R = std2( R_block6 );
stdblock7_R = std2( R_block7 );
stdblock8_R = std2( R_block8 );
stdblock9_R = std2( R_block9 );
stdblock10_R = std2( R_block10 );
stdblock11_R = std2( R_block11 );
stdblock12_R = std2( R_block12 );
stdblock13_R = std2( R_block13 );
stdblock14_R = std2( R_block14 );
stdblock15_R = std2( R_block15 );
stdblock16_R = std2( R_block16 );

stdblock1_G = std2( G_block1 );
stdblock2_G = std2( G_block2 );
stdblock3_G = std2( G_block3 );
stdblock4_G = std2( G_block4 );
stdblock5_G = std2( G_block5 );
stdblock6_G = std2( G_block6 );
stdblock7_G = std2( G_block7 );
stdblock8_G = std2( G_block8 );
stdblock9_G = std2( G_block9 );
stdblock10_G = std2( G_block10 );
stdblock11_G = std2( G_block12 );
stdblock13_G = std2( G_block13 );
stdblock14_G = std2( G_block14 );
stdblock15_G = std2( G_block15 );
stdblock16_G = std2( G_block16 );

stdblock1_B = std2( B_block1 );
stdblock2_B = std2( B_block2 );
stdblock3_B = std2( B_block3 );
stdblock4_B = std2( B_block4 );
stdblock5_B =std2( B_block5 );
stdblock6_B =std2( B_block6 );
stdblock7_B = std2( B_block7 );
stdblock8_B = std2( B_block8 );
stdblock9_B = std2( B_block9 );
stdblock10_B =std2( B_block10 );
stdblock11_B = std2( B_block11 );
stdblock12_B = std2( B_block12 );
stdblock13_B = std2( B_block13 );
stdblock14_B = std2( B_block14 );
stdblock15_B = std2( B_block15 );
stdblock16_B = std2( B_block16 );



STD_R = [ stdblock1_R stdblock2_R stdblock3_R stdblock4_R; stdblock5_R stdblock6_R stdblock7_R stdblock8_R;stdblock9_R stdblock10_R stdblock11_R Avgblock12_R; stdblock13_R stdblock14_R stdblock15_R stdblock16_R ];
STD_G = [ stdblock1_G stdblock2_G stdblock3_G stdblock4_G; stdblock5_G stdblock6_G stdblock7_G stdblock8_G;stdblock9_R stdblock10_G stdblock11_G Avgblock12_G; stdblock13_G stdblock14_G stdblock15_G stdblock16_G ];
STD_B = [ stdblock1_B stdblock2_B stdblock3_B stdblock4_B; stdblock5_B stdblock6_B stdblock7_B stdblock8_B;stdblock9_B stdblock10_B stdblock11_B Avgblock12_B; stdblock13_B stdblock14_B stdblock15_B stdblock16_B ];

figure(4);
bar3( STD_R, 'detached');
title(' Distribution of Noie Standard Deviation --Red');
grid;

figure(5);
bar3( STD_G, 'detached' );
title('Distribution of Noise Standard Deviation -- Green');
grid;

figure(6);
bar3( STD_B, 'detached' );
title(' Distribution of Noise Standard Deviation -- Blue ');
grid;




