function varargout = guip(varargin)
% GUIP M-file for guip.fig
%      GUIP, by itself, creates a new GUIP or raises the existing
%      singleton*.
%
%      H = GUIP returns the handle to a new GUIP or the handle to
%      the existing singleton*.
%
%      GUIP('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUIP.M with the given input arguments.
%
%      GUIP('Property','Value',...) creates a new GUIP or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before guip_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to guip_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 guip

% Last Modified by GUIDE v2.5 20-Sep-2012 01:00:03

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @guip_OpeningFcn, ...
                   'gui_OutputFcn',  @guip_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 guip is made visible.
function guip_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 guip (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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





% --- Outputs from this function are returned to the command line.
function varargout = guip_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 play.
function play_Callback(hObject, eventdata, handles)
% hObject    handle to play (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% imagen_video=handles.img;
% A=get (imagen_video);
% frames=A.NumberOfFrames;

M=getframe('nombre.avi');
set(handles.text9,'string',num2str(M));



% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
% hObject    handle to stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global parar
parar = 1;




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

%makemid
[nombre dire]=uigetfile('*.*','Abrir');
if nombre==0
    return
end
imagen_video=mmreader(fullfile(dire,nombre));
imagencolor=read(imagen_video,20);
axes(handles.axes2)

imshow(imagencolor)
axis off
handles.img=imagen_video;
guidata(hObject,handles);


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1
global seleccion

str = get(hObject, 'String');
val = get(hObject,'Value');

switch str{val};

    case 'detector'
        axes(handles.axes2)
        imagencolor2=handles.im2;
        guidata(hObject, handles);
        imshow(imagencolor2)
        seleccion=1;          
    case 'proceso1'
        axes(handles.axes2)
        bw_fin=handles.bw;
        guidata(hObject, handles);
        imshow(bw_fin)
         seleccion=2;
    case 'proceso2'
        axes(handles.axes2)
        bw3=handles.bw3;
        guidata(hObject, handles);
        imshow(bw3)
  %  case 'proceso3'
        axes(handles.axes2)
        bw4=handles.bw4;
        guidata(hObject, handles);
        imshow(bw4)
   % case 'proceso4'
        axes(handles.axes2)
        bw5=handles.bw5;
        guidata(hObject, handles);
        imshow(bw5)
    case 'histograma'
        axes(handles.axes2)
        hist=handles.hist;
        guidata(hObject, handles);
        plot(hist)
end
    

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

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on key press with focus on abrir and none of its controls.
function abrir_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to abrir (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)


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


% --- Executes on button press in procesar.
function procesar_Callback(hObject, eventdata, handles)

% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global parar
global seleccion
global umbral
global level
global bw



seleccion=1;
parar=0;
imagen_video=handles.img
A=get (imagen_video);
frames=A.NumberOfFrames;
tiempohumo=5;
area3=0;
areas=zeros(1,frames);
areas2=zeros(1,frames);
i=1;
f=1;

 
    while(i<=frames-20)
        
        
      if parar==1 
          break;          
      end
     tic;
        imagencolor=read(imagen_video,20);
        
        imagencolor2=read(imagen_video,i+20);
        handles.im2=imagencolor2;
        guidata(hObject,handles);
        
        imagen_double = im2double(imagencolor);
        imagen_double2 = im2double(imagencolor2);
      
        imagen_resta=imagen_double2-imagen_double;
      
        imagen_resta_gray =rgb2gray(imagen_resta);
      
         level =	graythresh(imagen_resta);
         bw=im2bw(imagen_resta,level);
        
       % bw=im2bw(imagen_resta,umbral);
        handles.bw=bw;
         axes(handles.axes6)
         imshow(bw)
       
        se = strel('disk',4);        
        
        erodedBW = imerode(bw,se);
        bw_fin = imdilate(erodedBW,se);
        handles.bw=bw_fin;
        guidata(hObject,handles);
       

        a=imhist(imagen_resta_gray);
        handles.hist=a;
        guidata(hObject,handles);
   
     
         b=a(1:80);

         t=min(b);


         [L,num] = bwlabel(bw_fin);
         s  = regionprops(L,'all');
         bw3= ismember(L, find([s.Area] >= 80));
         handles.bw3=bw3;
         axes(handles.axes7)
         imshow(bw3)
          

      if t>0.01*255
          axes(handles.axes1)
          imshow(imagencolor2)
             hold on
             c=imcontour(bw_fin,1,'b');
          
             for j=1:num
                    areaaux(j)=s(j).Area;
                    
             end
             area=sum(areaaux);
             area2=area;
             x=0;
             n=0;
             while ((area*10>=area2) && (n~=1));

                bw3 = imdilate(bw3,se);
                %handles.bw4=bw4;
                %guidata(hObject,handles);

                [K,n] = bwlabel(bw3);
                s2  = regionprops(K,'all');
                for j=1:n
                    areaaux(j)=s2(j).Area;
                end
                area2=sum(areaaux);
                
                x=x+1;
             end
         
             
             for k=1:x
                bw3 = imerode(bw3,se);
                %handles.bw5=bw5;
                %guidata(hObject,handles);
             end

             [K3,n3] = bwlabel(bw3);
             s3  = regionprops(K3,'all');
            
             
             for j=1:n3
                area3(j)=s3(j).Area;
             end
            
             areas(f)=sum(area3);
             
           
         else
        
          
      end
     if f>tiempohumo+1
      k=0;
        if areas(f)>1000
          k(i)= areas(f)-areas(f-tiempohumo);
          if k(i)>100
              i;
         
              imshow(imagencolor2)
              title 'HAY HUMO'
               hold on
             c=imcontour(bw_fin,3,'r');
              
              areas2(f)=areas(f);
          else
              imshow(imagencolor2)
               hold on
             c=imcontour(bw_fin,2,'g');
          end
        end
     end
      
      tiempo(f)=toc;
      
      i=i+round((A.NumberOfFrames/A.Duration).*tiempo(f));
      f=f+1;
      
      switch seleccion
          case 1
              axes(handles.axes2)
              imshow(imagencolor2)        
          case 2
              
      
      end
    end % del while
    
    areas=areas(1:f);
    areas2=areas2(1:f);
            
    axes(handles.axes2)
    hold on
    plot (areas,'b');
    plot (find(areas2>0),areas2(find(areas2>0)),'r');
    hold off
   
    
    %title('Evolución de las Areas')
    %ylabel('Area del humo')
    %xlabel('frames')
 guidata(hObject,handles);




function nframes_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double






function retardo_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double




function umbral_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double



function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function umbral_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double

global umbral

umbral= get(handles.umbral,'string');
umbral=floor(umbral);

if isempty(umbral)
    msgbox('introduce un número correcto', error, 'error');
    
else
end



function umbral_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double

global umbral
global level
global bw

umbral= str2num(get(handles.umbral,'string'));
umbral=floor(umbral);

if isempty(umbral)
     
    
        level =	graythresh(imagen_resta);

        
        bw=im2bw(imagen_resta,umbral);
    %msgbox('introduce un número correcto', error, 'error');
    
else
end



function retardo_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double

global retardo

retardo= str2num(get(handles.retardo,'string'));
retardo=floor(retardo);

if isempty(retardo)
    msgbox('introduce un número correcto','error', 'error');
    
else
end



function frames_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double

global framess

framess= str2num(get(handles.framess,'string'));
framess=floor(framess);

if isempty(framess)
    msgbox('introduce un número correcto', 'error', 'error');
    
else
end



function frameini_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double

global frameini

frameini= str2num(get(handles.frameini,'string'));
frameini=floor(frameini);

if isempty(frameini)
    msgbox('introduce un número correcto', 'error', 'error');
    
else
end


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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function framefin_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double

global framefin

framefin= str2num(get(handles.framefin,'string'));
framefin=floor(framefin);

if isempty(framefin)
    msgbox('introduce un número correcto', 'error', 'error');
    
else
end

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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
