%%Choisir le fichier contenant les informations pour le géoréférencement
[filename,pathname] = selectfile('Fichier utilisé pour le géoréférencement (mytform.mat)','off');
load ([pathname '\' filename]);

%%Pour la caméra 1
% load 'D:\Travail\Traitement_Matlab\Routines_Matlab\routines_Matlab\données\homologues\Cam1\mytform.mat'
clear filename pathname fname;

filename = multiselectimages;
Nbre_images=length(cellstr(filename));

image = imread(char(cellstr(filename(1))),'jpg');
imshow(image);
[u_pixel,v_pixel,value]=improfile;
u=int32(u_pixel);
v=int32(v_pixel);
close;
clear image;

%%%Conversion de coordonnées image en distance
u1=double(u');
v1=double(v');
[X,Y]=tformfwd(mytform,u1,v1);

distance = sqrt((max(X)-min(X))^2+(max(Y)-min(Y))^2);

I=eye(length(v),length(u),'double');

for i = 1:Nbre_images
    name=char(cellstr(filename(i)));
    name=name(length(name)-26:end);
    fname(i)=cellstr(filename(i));
    annee = str2double(name(1:4));
      mois = str2double(name(6:7));
      jour = str2double(name(9:10));
      heure = str2double(name(12:13));
      minute = str2double(name(15:16));
      seconde = str2double(name(18:23));
      date(i) = datenum([annee mois jour heure minute seconde]);
      
    [Photo]=eval(['imread(char(fname(i)),''jpg'');']);
time_intensity_blue(i,:,1)=uint8(transpose(nonzeros(I.*double(Photo(v,u,3)))));
time_intensity_green(i,:,1)=uint8(transpose(nonzeros(I.*double(Photo(v,u,2)))));
time_intensity_red(i,:,1)=uint8(transpose(nonzeros(I.*double(Photo(v,u,1)))));
time_intensity_ratioRB(i,:,1)=transpose(nonzeros(I.*(double(Photo(v,u,1))./max(double(Photo(v,u,3)),1))));
time_intensity_ratioRV(i,:,1)=transpose(nonzeros(I.*(double(Photo(v,u,1))./max(double(Photo(v,u,2)),1))));
time_intensity_RGB(i,:,1)=time_intensity_red(i,:,1);
time_intensity_RGB(i,:,2)=time_intensity_green(i,:,1);
time_intensity_RGB(i,:,3)=time_intensity_blue(i,:,1);

limite_ratioRB(i,:) = diff(double(time_intensity_ratioRB(i,:)));
indice_limite_ratioRB(i) = find(limite_ratioRB(i,:)==min(limite_ratioRB(i,:)),1);
limite_rouge(i,:)=diff(double(time_intensity_red(i,:)));
limite_vert(i,:)=diff(double(time_intensity_green(i,:)));
limite_bleu(i,:)=diff(double(time_intensity_blue(i,:)));
end

 %duree = etime(date(1),date(end));
 duree = (date(end)-date(1))*24*3600; % en secondes
 
 %axes du graphique
 espace = 0:5:distance;
 temps = 0:5:duree;

image(espace,temps,time_intensity_RGB)
axis 'image'
xlabel('Distance en mètres')
ylabel('Temps en secondes')
title('Time-stack du jet de rive')

 indice_limite_rouge(1) = find(limite_rouge(1,:)==min(limite_rouge(1,:)),1);
 indice_limite_bleu(1) = find(limite_bleu(1,:)==min(limite_bleu(1,:)),1);

test_blue=imhmin(time_intensity_blue,60);
test_red=imhmin(time_intensity_red,60);
test_green=imhmin(time_intensity_green,60);

%limite_vert=gradient(double(test));

  for i = 1:Nbre_images
%      maximum=min(indice_limite_bleu(i)+20,size(limite_bleu,2));
%      minimum=max(indice_limite_bleu(i)-20,1);
%      test(i)=find(limite_bleu(i,minimum:maximum)==min(limite_bleu(i,minimum:maximum)),1);
%     limite_vert(i,:)=gradient(double(test(i,:)));
%       indice_limite_bleu(i) = min(indice_limite_bleu(i-1) - 21 + test(i),size(limite_bleu,2)); 
%       indice_limite_vert(i) = find(limite_vert(i,:)==min(limite_vert(i,:)),1);
% %  
limite_test_red(i,:)=gradient(double(test_red(i,:)));
indice_limite_test_red(i,:) = find(limite_test_red(i,:)==min(limite_test_red(i,:)),1,'last');

   end


 hold on;
 line(indice_limite_test_red,1:Nbre_images)