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

% Last Modified by GUIDE v2.5 30-Nov-2003 01:51:59

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @First_slide_OpeningFcn, ...
                   'gui_OutputFcn',  @First_slide_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 First_slide is made visible.
function First_slide_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 First_slide (see VARARGIN)
%set(hObject, 'Background','white');

%set(hObject,'Color','white');
handles.mydata=0;
cmenu=uimenu(gcf,'label','figure color');
   uimenu(cmenu,...
      'label','grey',...
      'callback','set(gcf,''color'',''default'')');
   uimenu(cmenu,...
      'label','yellow',...
      'callback','set(gcf,''color'',''yellow'')');
   uimenu(cmenu,...
      'label','black',...
      'callback','set(gcf,''color'',''black'')');
% Choose default command line output for First_slide
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = First_slide_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;




function listbox3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

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


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

%%%%%%%%%%%%%get the value from the listbox %%%%%%%%%%%%%%%%%%%
contents = get(hObject,'String'); 
val=get(hObject,'Value'); 

switch(val)
    
    case 2
         D=imread('fig_1.jpg');
         axes(handles.axes1)
         image(D);
         handles.mydata=D
         guidata(hObject, handles)
    case 3
         D=imread('fig_2.jpg');
         axes(handles.axes1)
         image(D);
         handles.mydata=D
         guidata(hObject, handles)
       case 4
         D=imread('fig_3.jpg');
         axes(handles.axes1)
         image(D);
         handles.mydata=D
         guidata(hObject, handles)
    case 5
         D=imread('fig_4.jpg');
         axes(handles.axes1)
         image(D);
         handles.mydata=D
         guidata(hObject, handles)
    case 6
         D=imread('fig_5.jpg');
         axes(handles.axes1)
         image(D);
         handles.mydata=D
         guidata(hObject, handles)
    case 7
         D=imread('fig_6.jpg');
        axes(handles.axes1)
        
         image(D);
         handles.mydata=D
         guidata(hObject, handles)

 end
     
     
function pushbutton1_Callback(hObject, eventdata, handles)

% hObject    handle to listbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


X = handles.mydata;
I = rgb2gray(X);

%%%%% Use the gradient magnitude as the segmentation function%%%%%%%%%%%
hy = fspecial('sobel');
hx = hy';
Iy = imfilter(double(I), hy, 'replicate');
Ix = imfilter(double(I), hx, 'replicate');
gradmag = sqrt(Ix.^2 + Iy.^2);
%figure, imshow(gradmag,[]), title('gradmag')

%%%%%%%%%%%%%Mark the foreground objects%%%%%%%%%%%%%%%%%
se = strel('disk', 20);
Io = imopen(I, se);
Ie = imerode(I, se);
Iobr = imreconstruct(Ie, I);
Iobrd = imdilate(Iobr, se);
Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr));
Iobrcbr = imcomplement(Iobrcbr);
fgm = imregionalmax(Iobrcbr);
I2 = I;
I2(fgm) = 255;
se2 = strel(ones(5,5));
fgm2 = imclose(fgm, se2);
fgm3 = imerode(fgm2, se2);
fgm4 = bwareaopen(fgm3, 20);
I3 = I;
I3(fgm4) = 255;

%%%%%%%%%%%%Compute background markers%%%%%%%%%%%%%%%%%%%%%%%%%
bw = im2bw(Iobrcbr, graythresh(Iobrcbr));
D = bwdist(bw);
DL = watershed(D);
bgm = DL == 0;
D = bwdist(bw);
DL = watershed(D);
bgm = DL == 0;

%%%%%%%%%%Compute the watershed transform of the segmentation function%%%%%%

gradmag2 = imimposemin(gradmag, bgm | fgm4);
L = watershed(gradmag2);

%%%%%%%%%Visualize the result%%%%%%%%%%%%%%%

I4 = I;
I4(imdilate(L == 0, ones(3, 3)) | bgm | fgm4)= 255;
axes(handles.axes2)
imshow(I4);

%%%%%%%%%%%%display the image%%%%%%%%%%%%%%%%
axes(handles.axes1)
image(X);



% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




     


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

fig = gcf;
   quit_reply = questdlg('Really quit this lame example?');
   if strcmp(quit_reply,'Yes')
      close(fig);

  end