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

% Last Modified by GUIDE v2.5 30-Nov-2003 03:05:40

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

 cmenu=uimenu(gcf,'label','figure color');
   uimenu(cmenu,...
      'label','grey',...
      'callback','set(gcf,''color'',''default'')');
   uimenu(cmenu,...
      'label','white',...
      'callback','set(gcf,''color'',''white'')');
   uimenu(cmenu,...
      'label','black',...
      'callback','set(gcf,''color'',''black'')');
  

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

% Update handles structure
guidata(hObject, handles);

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


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


% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (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 listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (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 listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1

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



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

X=handles.mydata;


% This function is a segmentation function using k-mean
% It takes two parameters, one is the name of the image file, the other is how 
% many clusters it will be segmented into.

%function segmentation(name,number)

%a = imread('fig_1.jpg');

[i, j, k] = size(X);
% Reshape the 3-dimension image matrix into a two dimension matrix.
b = reshape(X, i*j, 3);

cc = double(X);
c = double(b);

% Using k-mean functions to segment the picture
% Since it takes a while to compute the distance value in k-means1 function and it is 
% never used in this program, you can just comment out the last several lines in k-means1
% function without affecting the execution of my segmentation function.

 [distance,label,tse] = kmeans1(c,3);

% Reshape the label matrix which is obtained from k-mean function into a 
% 3-dimension matrix again
d = reshape(label,i,j);

[i, j]=size(d);

% initialize
for r=1:3
    c_r(r,1) = 0;
    c_g(r,1) = 0;
    c_b(r,1) = 0;
    cluster(r,1) = 0;
end

% To compute the mean value of Red, Green and Blue value for each cluster
for r=1:i
    for t=1:j
        temp = d(r,t);
        c_r(temp,1) = c_r(temp,1)+cc(r,t,1);
        c_g(temp,1) = c_g(temp,1)+cc(r,t,2);
        c_b(temp,1) = c_b(temp,1)+cc(r,t,3);
        cluster(temp,1) = cluster(temp,1) + 1;
    end
end

for r=1:3
       c_r(r,1) = c_r(r,1)/cluster(r,1);
       c_g(r,1) = c_g(r,1)/cluster(r,1);
       c_b(r,1) = c_b(r,1)/cluster(r,1);
end

% Construct the result picture according to the label matrix and according RGB value
for r= 1:i
    for t = 1:j
        temp = d(r, t);
        result(r, t, 1) = c_r(temp,1);
        result(r, t, 2) = c_g(temp,1);
        result(r, t, 3) = c_b(temp,1);
    end
end

% Display the picture
k=uint8(result);
axes(handles.axes2)
image(k);




% --- 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
