%create_init_images.m  - Author: Stefan Gachter
%
% Compute the mean, mean of squares, standard devitation, median,
%	and median of squares for an image sequence.
%
% See also: main_simulation
%
% 17/02/2001 stefan.gachter@ieee.org

clear all;
close all;

%nb_movie=14;
nb_start_frame=0;
nb_stop_frame=10;%29;

%load_path=['/home.stud/qqgechte/omni/movie/experiment/pan/movie_' n2s(nb_movie) '/'];
%save_path=['/home.stud/qqgechte/omni/movie/experiment/pan/init/movie_' n2s(nb_movie) '/'];
save_path=['g:\omni\motion\'];
%movie_name=['movie_pan_' n2s(nb_movie)];

init_mean_bgd_img_name='init_mean_bgd_img';
init_mean_squared_bgd_img_name='init_mean_squared_bgd_img';
init_std_dev_bgd_img_name='init_std_dev_bgd_img';
init_median_bgd_img_name='init_median_bgd_img';
init_lms_bgd_img_name='init_lms_bgd_img';

map=gray(256);

frame_i.cdata=[];
frame_i.colormap=[];

infodev=giottoopen('LPT1');       %open camea

for j=nb_start_frame:nb_stop_frame
%   file_name=[load_path movie_name '_' fix_format_int_str(j,3) '.bmp'];
%   [frame_i.cdata,frame_i.colormap]=imread(file_name,'bmp');
%   bgd_img=ind2gray(frame_i.cdata,frame_i.colormap);
   bgd_img=giottograb(infodev);
	bgd_img=bgd_img./255;
   %frame_i=rgb2gray(frame_i.cdata);
   bgd_img_buffer(:,:,j-nb_start_frame+1)=bgd_img;
end

init_mean_bgd_img=mean(bgd_img_buffer,3);
init_mean_squared_bgd_img=mean(bgd_img_buffer.^2,3);
init_std_dev_bgd_img=std(bgd_img_buffer,0,3);
init_median_bgd_img=median(bgd_img_buffer,3);
init_lms_bgd_img=scalar_lms_img(bgd_img_buffer,9);

%frame_size=size(frame_i.cdata);
frame_size=size(bgd_img);

figure(gcf+1);
imagesc(init_mean_bgd_img);
title('mean of the background images');
axis off
colormap(map);
%figure(gcf+1);
%set(gca,'Position',[0,0,1,1]);
%left=1;
%bottom=1;
%width=frame_size(2);
%hight=frame_size(1);
%set(gcf,'Position',[left,bottom,width,hight]);
%imagesc(init_mean_bgd_img);
%colormap(map)

%n_1=68;
%n_2=136;
%n_3=236;
n_1=60;
n_2=130;
n_3=220;

set_1=init_std_dev_bgd_img(:,n_1);
set_2=init_std_dev_bgd_img(:,n_2);
set_3=init_std_dev_bgd_img(:,n_3);
init_std_dev_bgd_img(:,n_1)=max(init_std_dev_bgd_img(:));
init_std_dev_bgd_img(:,n_2)=max(init_std_dev_bgd_img(:));
init_std_dev_bgd_img(:,n_3)=max(init_std_dev_bgd_img(:));
figure(gcf+1)
plot(1:frame_size(1),set_1,'blue-',1:frame_size(1),set_2,'red--',1:frame_size(1),set_3,'green-.');
current_axis=axis;
line([1 1 NaN frame_size(1) frame_size(1)], ...
	 [current_axis(3) current_axis(4) NaN current_axis(3) current_axis(4)],'Color',[0 0 0]);
line([1 frame_size(1)],[sqrt(1/12) sqrt(1/12)],'Color',[0 0 0],'LineStyle',':');
xlabel('image coordinate $m$')
ylabel('standard deviation $\sigma$ of the intesity values')
figure(gcf+1);
imagesc(init_std_dev_bgd_img);
title('standard deviation of the background images');
lim=axis;
axis off
colormap(map);

figure(gcf+1);
imagesc(init_median_bgd_img);
title('median of the background images');
axis off
colormap(map);
figure(gcf+1);
imagesc(init_lms_bgd_img);
title('least median of squares of the  background images');
axis off
colormap(map);
close(1);

file_name=[save_path init_mean_bgd_img_name '.mat'];
eval(['save ' file_name ' init_mean_bgd_img map']);
file_name=[save_path init_mean_squared_bgd_img_name '.mat'];
eval(['save ' file_name ' init_mean_squared_bgd_img map']);
file_name=[save_path init_std_dev_bgd_img_name '.mat'];
eval(['save ' file_name ' init_std_dev_bgd_img map']);
file_name=[save_path init_median_bgd_img_name '.mat'];
eval(['save ' file_name ' init_median_bgd_img map']);
file_name=[save_path init_lms_bgd_img_name '.mat'];
eval(['save ' file_name ' init_lms_bgd_img map']);















