function varargout = programav2(varargin)
% PROGRAMAV2 M-file for programav2.fig
%      PROGRAMAV2, by itself, creates a new PROGRAMAV2 or raises the existing
%      singleton*.
%
%      H = PROGRAMAV2 returns the handle to a new PROGRAMAV2 or the handle to
%      the existing singleton*.
%
%      PROGRAMAV2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PROGRAMAV2.M with the given input arguments.
%
%      PROGRAMAV2('Property','Value',...) creates a new PROGRAMAV2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before programav2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to programav2_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help programav2

% Last Modified by GUIDE v2.5 21-Aug-2012 17:01:55

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @programav2_OpeningFcn, ...
                   'gui_OutputFcn',  @programav2_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before programav2 is made visible.
function programav2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to programav2 (see VARARGIN)

% Choose default command line output for programav2
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes programav2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = programav2_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in CargarForeground.
function CargarForeground_Callback(hObject, eventdata, handles)
% hObject    handle to CargarForeground (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
archiVideo=uigetfile('*.avi','Abrir');
if archiVideo==0
    return
end
VideoF=mmreader(archiVideo);
numFramesF=get(VideoF,'numberOfFrames');
handles.vidforeground=VideoF;
guidata(hObject,handles);

for K=1 : (numFramesF-75)
    foreground=read(VideoF,K);
    axes(handles.axes1)
    imshow(foreground);title('FOREGROUND')
    rx=foreground(:,:,1);
    gx=foreground(:,:,2); 
    bx=foreground(:,:,3);
    axes(handles.axes3)
    imhist(rx);title('ROJO')
    axes(handles.axes4)
    imhist(gx);title('VERDE')
    axes(handles.axes5)
    imhist(bx);title('AZUL')
end


% --- Executes on button press in CargarBackground.
function CargarBackground_Callback(hObject, eventdata, handles)
% hObject    handle to CargarBackground (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
archiVideo=uigetfile('*.avi','Abrir');
if archiVideo==0
    return
end
VideoB=mmreader(archiVideo);
handles.vidbackground=VideoB;
guidata(hObject,handles);
numFramesF=240;
P=1245;

for j=1 : ((numFramesF-75)/3)
    background=read(VideoB,P);
    axes(handles.axes2)
    imshow(background);title('BACKGROUND')
    P=P+7;
end



% --- Executes on button press in ObtenerMascara.
function ObtenerMascara_Callback(hObject, eventdata, handles)
% hObject    handle to ObtenerMascara (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

numFramesF=get(handles.vidforeground,'numberOfFrames');

k=2;
a2=1.018;
a22=0.92;
   while k<= (numFramesF-75)
     ima=read(handles.vidforeground,k-1);
     imb=read(handles.vidforeground,k);
     imc=read(handles.vidforeground,k+1);
     k=k+3;
     
     mascara1=imb(:,:,2)>imb(:,:,3)& imb(:,:,2)>imb(:,:,1)& imb(:,:,2)>65;
     mascara2=(uint8(imc(:,:,2)))>=(uint8(ima(:,:,2))*a2)|(uint8(imb(:,:,2))*a2)<=(uint8(imc(:,:,2)))&(uint8(imb(:,:,2))*a2)<(uint8(ima(:,:,2)))|(uint8(imb(:,:,2)))<=(uint8(ima(:,:,2))*a22 ) | (uint8(imb(:,:,2))*a22)>=(uint8(imc(:,:,2)))|(uint8(ima(:,:,2))*a22)>(uint8(imc(:,:,2))) ;
     mascara3=imb(:,:,1)>115 & imb(:,:,2)>225 & imb(:,:,3)>115;
     mascara3=dilate_mult(~mascara3,13);
     mascara2=mascara2.*mascara3;
     
     mascara= mascara1&mascara2 ;  
   
     axes(handles.axes6) 
     imshow(mascara);
   end



% --- Executes during object creation, after setting all properties.
function axes6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes6


% --- Executes on button press in GenerarComposicion.
function GenerarComposicion_Callback(hObject, eventdata, handles)
% hObject    handle to GenerarComposicion (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


numFramesF=get(handles.vidforeground,'numberOfFrames');


k=2;
a2=1.018;
a22=0.92;
T=1245;
for j=1 : ((numFramesF-75)/3)
    while k<= (numFramesF-75)
     
     ima=read(handles.vidforeground,k-1);
     imb=read(handles.vidforeground,k);
     imc=read(handles.vidforeground,k+1);
     k=k+3;
     
     imd=read(handles.vidbackground,T);     %carga en la variable imd el frame marcado por T
        if size(imd)~=size(ima)
            imd=imresize(imd, [size(handles.vidforeground,1) size(handles.vidforeground,2)]);%Cambia el tamaņo de background por el de foreground
        end
     T=T+10;
     
     mascara1=imb(:,:,2)>imb(:,:,3)& imb(:,:,2)>imb(:,:,1)& imb(:,:,2)>65;
     mascara2=(uint8(imc(:,:,2)))>=(uint8(ima(:,:,2))*a2)|(uint8(imb(:,:,2))*a2)<=(uint8(imc(:,:,2)))&(uint8(imb(:,:,2))*a2)<(uint8(ima(:,:,2)))|(uint8(imb(:,:,2)))<=(uint8(ima(:,:,2))*a22 ) | (uint8(imb(:,:,2))*a22)>=(uint8(imc(:,:,2)))|(uint8(ima(:,:,2))*a22)>(uint8(imc(:,:,2))) ;
     mascara3=imb(:,:,1)>115 & imb(:,:,2)>225 & imb(:,:,3)>115;
     mascara3=dilate_mult(~mascara3,13);
     mascara2=mascara2.*mascara3;
     
     mascara= mascara1&mascara2 ;  
     imFu = cast(mascara, 'uint8'); 
     imFm = cast(~mascara, 'uint8');
     
     imCk2(:,:,1) = imb(:,:,1).*imFm + imFu.*imd(:,:,1);
     imCk2(:,:,2) = imb(:,:,2).*imFm + imFu.*imd(:,:,2);  
     imCk2(:,:,3) = imb(:,:,3).*imFm + imFu.*imd(:,:,3);
     axes(handles.axes7) 
     imshow(imCk2);
     
     F(j)=getframe;              %obtiene el frame anterior y lo almacena
     j=j+1; 
    end  
end
   movie2avi(F, 'Secuencia');
   save Secuencia


% --- Executes on button press in ReproducirSecuencia.
function ReproducirSecuencia_Callback(hObject, eventdata, handles)
% hObject    handle to ReproducirSecuencia (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
implay('Secuencia1.avi')


