close all; clc
%% =====================================================================================================
% Parameters to be modified

% Read image 1
Im = imread('picture 1.jpg');

% Define two regions of interest (ROI) over which the noise will be
% analyzed. One ROI should be in the white region, while the other one in
% the black region

% x-coordinates for the left ROI
x_start_left = 80;
x_end_left   = 380;

% x-coordinates for the right ROI
x_start_right = 900;
x_end_right   = 1200;

% y-coordinate defining the vertical center of the ROI
y_center_line = 360;

% Define vertical size of the ROI corresponding to the number of lines over
% the data will be averaged.
N = 50;

% Show ROI (1 = show ROI, 0 = hide ROI)
show_ROI = 1;




% XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
%
%                             DO NOT MODIFY THE SCRIPT BEYOND THIS POINT
%
% XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX



%% =====================================================================================================
% Process data

% camera parameters
PixNrx      =       1280;
PixNry      =       720;

% select a channel (here red)
Red_1 = Im(:,:,1);

% format conversion
Red_1 = double(Red_1);


% plot the a line plot from x=(1 x 1600) at the center line at y = 600
CenterLine = Red_1(y_center_line,:);

% select a Region of Interst RIO with  values between 1 and 1600 in x direction, and the center line in y direction (value 600), choose the left area of the image: here from 10 to 400 in x direction 
CenterLine_left = Red_1(y_center_line,x_start_left:x_end_left);
ROI_Red_1_left  = Red_1(floor(y_center_line-N/2):floor(y_center_line+N/2),x_start_left:x_end_left);
%caculate the mean of the RIO over several rows
N_Avg_Red_1_left = mean (ROI_Red_1_left,1);

MEAN_red_left = mean(N_Avg_Red_1_left)
STD_red_left  = std(N_Avg_Red_1_left)

% repeat for the right side
CenterLine_right = Red_1(y_center_line,x_start_right:x_end_right);
ROI_Red_1_right  = Red_1(floor(y_center_line-N/2):floor(y_center_line+N/2),x_start_right:x_end_right);
%caculate the mean of the RIO over several rows
N_Avg_Red_1_right = mean (ROI_Red_1_right,1);

MEAN_red_right = mean(N_Avg_Red_1_right)
STD_red_right  = std(N_Avg_Red_1_right)


%% =====================================================================================================
% Plot data

lw = 2;

figure('color','w','position',[211   58  943  712])

subplot(2,2,1)
imagesc(Im)
hold on
if show_ROI
   rectangle('position',[x_start_left floor(y_center_line-N/2) x_end_left-x_start_left N],'edgecolor','c','linewidth',2) 
   rectangle('position',[x_start_right floor(y_center_line-N/2) x_end_right-x_start_right N],'edgecolor','g','linewidth',2) 
end
axis square
title('Original image with the two ROI')
xlabel('Position x (pixel)')
ylabel('Position y (pixel)')


subplot(2,2,2)
plot(CenterLine,'k','LineWidth',2)
hold on
if show_ROI
   plot(x_start_left *[1 1],[0 255],'c','linewidth',2) 
   plot(x_end_left   *[1 1],[0 255],'c','linewidth',2) 
   plot(x_start_right*[1 1],[0 255],'g','linewidth',2) 
   plot(x_end_right  *[1 1],[0 255],'g','linewidth',2) 
end
axis square
title(['Raw signal at y = ' num2str(y_center_line) ' with the two ROI'])
xlabel('Position x (pixel)')
ylabel('Signal (counts)')
xlim([1 PixNrx])
ylim([0 255])

%plot the averaged line
subplot(2,2,3)
hold on
plot(x_start_left:x_end_left, CenterLine_left ,'k','LineWidth',lw)
plot(x_start_left:x_end_left, N_Avg_Red_1_left,'r','LineWidth',lw)
title('Raw and averaged signals on left ROI')
axis square
xlabel('Position x (pixel)')
ylabel('Signal (counts)')

%plot the averaged line
subplot(2,2,4)
hold on
plot(x_start_right:x_end_right, CenterLine_right ,'k','LineWidth',lw)
plot(x_start_right:x_end_right, N_Avg_Red_1_right,'r','LineWidth',lw)
title('Raw and averaged signals on right ROI')
axis square
xlabel('Position x (pixel)')
ylabel('Signal (counts)')




