function imdemo(action,s);
%IMDEMO General image processing demo.

%	Copyright (c) 1993 by The MathWorks, Inc.
%	$Revision: 1.3 $  $Date: 1993/10/21 12:59:32 $

% Possible actions:
% initialize
% image
% operation

% button callbacks:
% Gray
% Brighten
% Equalize
% GraySlice
% Emboss
% Contrast
% Histogram
% Profile
% Roicolor
% Edges
% Colorbar
% White Noise
% Salt Noise
% Wiener
% Median
% Blur
% FFT
% DCT
% Radon
% Rotate
% Shrink
% Dilate
% Erode
% Perimeter
% Invert
% info


if nargin<1,
    action='initialize';
end;

if strcmp(action,'initialize'),
    figNumber=figure( ...
        'Name','Image Processing Demo', ...
        'NumberTitle','off');

    %==================================
    % Set up the image axes
    axes( ...
        'Units','normalized', ...
        'Position',[0.10 0.35 0.60 0.6], ...
        'XTick',[],'YTick',[], ...
        'Box','on');
    set(figNumber,'defaultaxesposition',[0.10 0.35 0.60 0.6])

    %===================================
    % Set up the Comment Window
    top=0.25;
    left=0.05;
    right=0.725;
    bottom=0.05;
    labelHt=0.035;
    spacing=0.005;
    % First, the MiniCommand Window frame
    frmBorder=0.02;
    frmPos=[left-frmBorder bottom-frmBorder ...
	(right-left)+2*frmBorder (top-bottom)+2*frmBorder];
    uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
	'BackgroundColor',[0.45 0.45 0.45]);
    % Then the text label
    labelPos=[left top-labelHt (right-left) labelHt];
    uicontrol( ...
	'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',[0.45 0.45 0.45], ...
	'ForegroundColor',[1 1 1], ...
        'String','Comment Window');
    % Then the editable text field
    txtPos=[left bottom (right-left) top-bottom-labelHt-spacing];
    txtHndl=uicontrol( ...
	'Style','edit', ...
        'Units','normalized', ...
        'Max',10, ...
        'BackgroundColor',[1 1 1], ...
        'Position',txtPos, ...
        'String',str2mat(' % Choose an image to process',...
                         ' % from the pop-up menu (e.g., trees).'));
    % Save this handle for future use
    set(gcf,'UserData',txtHndl);

    %====================================
    % Information for all buttons (and menus)
    labelColor=[0.8 0.8 0.8];
    yInitPos=0.90;
    menutop=0.95;
    top=0.75;
    left=0.785;
    btnWid=0.175;
    btnHt=0.06;
    % Spacing between the button and the next command's label
    spacing=0.025;
    
    %====================================
    % The CONSOLE frame
    frmBorder=0.02;
    yPos=0.05-frmBorder;
    frmPos=[left-frmBorder yPos btnWid+2*frmBorder 0.9+2*frmBorder];
    h=uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
	'BackgroundColor',[0.45 0.45 0.45]);

    %====================================
    % The IMAGE Menu
    menuNumber=1;
    yPos=menutop-(menuNumber-1)*(btnHt+spacing);
    labelStr='trees|mandrill|clown|gatlin|forest';
    callbackStr='imdemo(''image'');';

    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    imageHndl=uicontrol( ...
        'Style','popupmenu', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Interruptible','yes', ...
        'Callback',callbackStr);

    %====================================
    % The OPERATION Menu
    menuNumber=2;
    yPos=menutop-(menuNumber-1)*(btnHt+spacing);
    labelStr='Enhance|Restore|Analyze|Transform|Morph Ops';
    callbackStr='imdemo(''operation'')';

    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    opHndl=uicontrol( ...
        'Style','popupmenu', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Interruptible','yes', ...
        'Callback',callbackStr);

    %====================================
    % button 1
    btnNumber=1;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Brighten';
    callbackStr='imdemo(''Brighten'');';

    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    btn1Hndl=uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr);

    %====================================
    % button 2
    btnNumber=2;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Equalize';
    % Setting userdata to 1 (=play) will continue the demo.
    callbackStr='imdemo(''Equalize'');';
    
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    btn2Hndl=uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr);

    %====================================
    % button 3
    btnNumber=3;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='GraySlice';
    % Setting userdata to -1 (=stop) will stop the demo.
    callbackStr='imdemo(''GraySlice'');';
    
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    btn3Hndl=uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr);

    %====================================
    % button 4
    btnNumber=4;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Emboss';
    % Setting userdata to -1 (=stop) will stop the demo.
    callbackStr='imdemo(''Emboss'');';
    
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    btn4Hndl=uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr);

    %====================================
    % button 5
    btnNumber=5;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Contrast';
    % Setting userdata to -1 (=stop) will stop the demo.
    callbackStr='imdemo(''Contrast'');';
    
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    btn5Hndl=uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr);

    %====================================
    % The GRAY button
    btnNumber=6;
    yPos=top-(btnNumber-1)*(btnHt+spacing)-spacing;
    labelStr='Make Gray';
    callbackStr='imdemo(''Gray'')';
    % Setting this checkbox will run demo w/o stopping.
    
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    autoHndl=uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'Enable','on', ...
        'String',labelStr, ...
        'Callback',callbackStr);

    %====================================
    % The INFO button
    labelStr='Info';
    callbackStr='imdemo(''info'')';
    helpHndl=uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',[left bottom+btnHt+spacing btnWid btnHt], ...
        'String',labelStr, ...
        'Callback',callbackStr);

    %====================================
    % The CLOSE button
    labelStr='Close';
    callbackStr='close(gcf)';
    closeHndl=uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',[left bottom btnWid btnHt], ...
        'String',labelStr, ...
        'Callback',callbackStr);

    hndlList=[txtHndl imageHndl opHndl btn1Hndl btn2Hndl btn3Hndl btn4Hndl ...
              btn5Hndl autoHndl helpHndl closeHndl];
    set(figNumber, ...
	'Visible','on', ...
	'UserData',hndlList);

    set(gcf,'Pointer','watch');
    drawnow
    load trees
    imshow(X,map)
%    imdemo('operation')
    set(gcf,'Pointer','arrow');
    return
end

% Protect against possible deletion all the whole figure
danger = strcmp(get(gcf,'NextPlot'),'replace');

if strcmp(action,'image'),
    if danger, 
      set(gcf,'nextplot','add');
      h = get(gcf,'children');
      for i=1:length(h),
        if strcmp(get(h(i),'type'),'axes'), delete(h(i)), end
      end
    end
    axHndl=gca;
    hndlList=get(gcf,'Userdata');
    txtHndl=hndlList(1);
    imageHndl=hndlList(2);
    opHndl=hndlList(3);
    btnHandls = hndlList(4:8);
    autoHndl=hndlList(9);
    helpHndl=hndlList(10);
    closeHndl=hndlList(11);
    v = get(imageHndl,'value');
    name = get(imageHndl,'String'); name = deblank(name(v,:));
    commentStr=str2mat([' load ',name],' imshow(X,map)');
    set(txtHndl,'String',commentStr);

    set(gcf,'Pointer','watch');
    load(name)
    imshow(X,map)
    set(gcf,'Pointer','arrow');
    return

elseif strcmp(action,'operation'),
    axHndl=gca;
    hndlList=get(gcf,'Userdata');
    txtHndl=hndlList(1);
    imageHndl=hndlList(2);
    opHndl=hndlList(3);
    btnHndls = hndlList(4:8);
    autoHndl=hndlList(9);
    helpHndl=hndlList(10);
    closeHndl=hndlList(11);
    v = get(opHndl,'value');
    op = get(opHndl,'String'); op = deblank(op(v,:));
    set(btnHndls(2),'Interruptible','no');
    if strcmp(op,'Enhance'),
      labels = str2mat('Brighten','Equalize','GraySlice','Emboss','Contrast');
    elseif strcmp(op,'Analyze'),
      labels = str2mat('Histogram','Profile','Roicolor','Edges','Colorbar');
      set(btnHndls(2),'Interruptible','yes');
    elseif strcmp(op,'Restore'),
      labels = str2mat('White Noise','Salt Noise','Wiener','Median','Blur');
    elseif strcmp(op,'Transform'),
      labels = str2mat('FFT','DCT','Radon','Rotate','Shrink');
    elseif strcmp(op,'Morph Ops'),
      labels = str2mat('Dilate','Erode','Perimeter','Thin','Fatten');
    else
      error('Unknown operation');
    end

    for i=1:length(btnHndls),
      callbackStr = ['imdemo(''',deblank(labels(i,:)),''');'];
      set(btnHndls(i),'String',deblank(labels(i,:)),'Callback',callbackStr)
    end
    
    commentStr=str2mat(' % Press a button to start an operation.',...
     ' % The commands executed by each operation will', ...
     ' % display in this window.');
    set(txtHndl,'String',commentStr);
    return

elseif strcmp(action,'closehelp'),
    % Restore close button help behind helpfun's back
    ch = get(gcf,'ch');
    for i=1:length(ch),
      if strcmp(get(ch(i),'type'),'uicontrol'),
        if strcmp(lower(get(ch(i),'String')),'close'),
          callbackStr = get(ch(i),'callback');
          k = findstr('; imdemo(',callbackStr);
          callbackStr = callbackStr(1:k-1);
          set(ch(i),'callback',callbackStr)
          break;
        end
      end
    end
    ch = get(0,'ch');
    if ~isempty(find(ch==s)), figure(s), end % Make sure figure exists
   
else % Get common information for buttons below
    if danger, 
      haxes = [];
      set(gcf,'nextplot','add');
      h = get(gcf,'children'); 
      for i=1:length(h),
        if strcmp(get(h(i),'type'),'axes'), haxes = [haxes,h(i)]; end
      end
    end
    axHndl=gca;
    hndlList=get(gcf,'Userdata');
    txtHndl=hndlList(1);
    imageHndl=hndlList(2);
    opHndl=hndlList(3);
    btnHandls = hndlList(4:8);
    autoHndl=hndlList(9);
    helpHndl=hndlList(10);
    closeHndl=hndlList(11);
    h = get(axHndl,'children');
    set(gcf,'pointer','watch')
    if strcmp(get(h(1),'type'),'image'),
      X = get(h(1),'cdata');
      map = colormap;
      if all(get(h(1),'UserData')==[0 1])
        I = (X-1)/(size(map,1)-1); grayimage = 1;
      else
        grayimage = 0;
      end
    else
      v = get(imageHndl,'value');
      name = get(imageHndl,'String'); name = deblank(name(v,:));
      load(name)
      grayimage = 0;
      if danger, 
        delete(haxes), imshow(X,map), danger = 0; 
      else
       imshow(X,map)
      end
    end
end

%================= BUTTON CALLBACKS =======================

if strcmp(action,'Gray'),
    if ~grayimage,
      commentStr = str2mat(' I = ind2gray(X,map);',' imshow(I,64)');
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      I = ind2gray(X,map);
      imshow(I,64)
    end

elseif strcmp(action,'Brighten')
    commentStr = ' brighten(.2)';
    set(txtHndl,'String',commentStr), drawnow
    brighten(.2)
    set(gcf,'pointer','arrow')

elseif strcmp(action,'Equalize')
    if grayimage,
      commentStr = ' histeq(I,64)';
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      histeq(I,64)
    else
      commentStr = ' histeq(X,map)';
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      histeq(X,map)
    end
   
elseif strcmp(action,'GraySlice')
    if grayimage,
      commentStr = ' grayslice(I,10)';
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      grayslice(I,10)
    else
      commentStr = str2mat(' I = ind2gray(X,map); % Convert image to gray', ...
       ' grayslice(I,10) % Slice');
      set(txtHndl,'String',commentStr), drawnow
      I = ind2gray(X,map);
      if danger, delete(haxes), end
      grayslice(I,10)
    end

elseif strcmp(action,'Emboss')
    if grayimage,
      commentStr = str2mat(' h = fspecial(''sobel'');', ...
         ' J = mat2gray(filter2(h,I));',' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      h = fspecial('sobel');
      J = mat2gray(filter2(h,I));
      if danger, delete(haxes), end
      imshow(J,64)
    else
      commentStr = str2mat(' I = ind2gray(X,map); % Convert image to gray', ...
       ' h = fspecial(''sobel'');',' J = mat2gray(filter2(h,I));', ...
       ' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      I = ind2gray(X,map);
      h = fspecial('sobel');
      J = mat2gray(filter2(h,I));
      if danger, delete(haxes), end
      imshow(J,64)
    end

elseif strcmp(action,'Contrast')
    if grayimage,
      commentStr = str2mat(' h = fspecial(''unsharp'');', ...
         ' J = mat2gray(filter2(h,I),[0 1]);',' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      h = fspecial('unsharp');
      J = mat2gray(filter2(h,I),[0 1]);
      if danger, delete(haxes), end
      imshow(J,64)
    else
      commentStr = str2mat(' I = ind2gray(X,map); % Convert image to gray', ...
       ' h = fspecial(''unsharp'');',' J = mat2gray(filter2(h,I),[0 1]);', ...
       ' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      I = ind2gray(X,map);
      h = fspecial('unsharp');
      J = mat2gray(filter2(h,I),[0 1]);
      if danger, delete(haxes), end
      imshow(J,64)
    end
    
elseif strcmp(action,'Histogram')
    if grayimage,
      commentStr = ' imhist(I,128)';
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      imhist(I,128)
    else
      commentStr = ' imhist(X,map)';
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      imhist(X,map)
    end
      
elseif strcmp(action,'Profile')
    commentStr = str2mat(' % Profile displays the intensity profile', ...
     ' % along a line in the image.  Select the ', ...
     ' % endpoints of line using mouse.  End with <CR>.','improfile');
    set(txtHndl,'String',commentStr), drawnow
    name = get(gcf,'Name');
    set(gcf,'Name','Profile')
    improfile
    set(gcf,'Name',name)

elseif strcmp(action,'Roicolor')
    if grayimage,
      commentStr = str2mat(' X = gray2ind(I,64); cm = [gray(64);[1 0 0]];',...
      ' d = find(roicolor(I,.25,.5)); X(d) = 65*ones(size(d));', ...
      ' imshow(X,cm)');
      set(txtHndl,'String',commentStr), drawnow
      X = gray2ind(I,64); cm = [gray(64);[1 0 0]];
      d = find(roicolor(I,.25,.5));
      X(d) = 65*ones(size(d));
      if danger, delete(haxes), end
      imshow(X,cm)
    else
      commentStr = str2mat( ...
      ' v = min(size(map,1),255); cm = [map(1:v,:);[1 1 0]];', ...
      ' d = find(roicolor(X,10,30)); X(d) = (v+1)*ones(size(d));', ...
      ' imshow(X,cm)');
      set(txtHndl,'String',commentStr), drawnow
      v = min(size(map,1),255);
      cm = [map(1:v,:);[1 1 0]];
      d = find(roicolor(X,10,30));
      X(d) = (v+1)*ones(size(d));
      if danger, delete(haxes), end
      imshow(X,cm)
    end
    
elseif strcmp(action,'Edges')
    if grayimage,
      commentStr = ' edge(I);';
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      edge(I);
    else
      commentStr = str2mat(' I = ind2gray(X,map); % Convert image to gray', ...
       ' edge(I);');
      set(txtHndl,'String',commentStr), drawnow
      I = ind2gray(X,map);
      if danger, delete(haxes), end
      edge(I);
    end

elseif strcmp(action,'Colorbar')
    commentStr = str2mat(' % Add colorbar to existing image.', ...
    ' colorbar');
    set(txtHndl,'String',commentStr), drawnow
    if danger, set(gcf,'nextplot','replace'), end
    colorbar

elseif strcmp(action,'White Noise')
    if grayimage,
      commentStr = str2mat(' % Add white noise to image.', ...
       ' J = imnoise(I,''gaussian'');',' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      J = imnoise(I,'gaussian');
      if danger, delete(haxes), end
      imshow(J,64)
    else
      commentStr = str2mat(' I = ind2gray(X,map); % Convert image to gray', ...
        ' % Add white noise to image.', ...
       ' J = imnoise(I,''gaussian'');',' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      I = ind2gray(X,map);
      J = imnoise(I,'gaussian');
      if danger, delete(haxes), end
      imshow(J,64)
    end

elseif strcmp(action,'Salt Noise')
    if grayimage,
      commentStr = str2mat(' % Add salt and pepper noise to image.', ...
       ' J = imnoise(I,''salt & pepper'');',' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      J = imnoise(I,'salt & pepper');
      if danger, delete(haxes), end
      imshow(J,64)
    else
      commentStr = str2mat(' I = ind2gray(X,map); % Convert image to gray', ...
        ' % Add salt and pepper noise to image.', ...
       ' J = imnoise(I,''salt & pepper'');',' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      I = ind2gray(X,map);
      J = imnoise(I,'salt & pepper');
      if danger, delete(haxes), end
      imshow(J,64)
    end

elseif strcmp(action,'Wiener')
    if grayimage,
      commentStr = str2mat(' J = wiener2(I,[3 3]);',' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      J = wiener2(I,[3 3]);
      if danger, delete(haxes), end
      imshow(J,64)
    else
      commentStr = str2mat(' I = ind2gray(X,map); % Convert image to gray', ...
        ' J = wiener2(I,[3 3]);',' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      I = ind2gray(X,map);
      J = wiener2(I,[3 3]);
      if danger, delete(haxes), end
      imshow(J,64)
    end
   
elseif strcmp(action,'Median')
    if grayimage,
      commentStr = str2mat(' J = medfilt2(I,[3 3]);',' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      J = medfilt2(I,[3 3]);
      if danger, delete(haxes), end
      imshow(J,64)
    else
      commentStr = str2mat(' I = ind2gray(X,map); % Convert image to gray', ...
        ' J = medfilt2(I,[3 3]);',' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      I = ind2gray(X,map);
      J = medfilt2(I,[3 3]);
      if danger, delete(haxes), end
      imshow(J,64)
    end

elseif strcmp(action,'Blur')
    if grayimage,
      commentStr = str2mat(' h = fspecial(''gaussian'',[5 5]);', ...
       ' J = filter2(h,I);',' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      h = fspecial('gaussian',[5 5]);
      J = filter2(h,I);
      if danger, delete(haxes), end
      imshow(J,64)
    else
      commentStr = str2mat(' I = ind2gray(X,map); % Convert image to gray', ...
       ' h = fspecial(''gaussian'',[5 5]);',' J = filter2(h,I);', ...
       ' imshow(J,64)');
      set(txtHndl,'String',commentStr), drawnow
      I = ind2gray(X,map);
      h = fspecial('gaussian',[5 5]);
      J = filter2(h,I);
      if danger, delete(haxes), end
      imshow(J,64)
    end

elseif strcmp(action,'FFT')
    if grayimage,
      commentStr = str2mat(' F = abs(fftshift(fft2(I,512,512)));', ... 
        ' colormap(jet(64)), imagesc(log(F))',' colorbar');
      set(txtHndl,'String',commentStr), drawnow
      F = abs(fftshift(fft2(I,512,512)));
      if danger, delete(haxes), end
      colormap(jet(64)), imagesc(log(F))
      colorbar
    else
      commentStr = str2mat(' F = abs(fftshift(fft2(X,512,512)));', ... 
        ' colormap(jet(64)), imagesc(log(F))',' colorbar');
      set(txtHndl,'String',commentStr), drawnow
      F = abs(fftshift(fft2(X,512,512)));
      if danger, delete(haxes), end
      colormap(jet(64)), imagesc(log(F))
      colorbar
    end
      
elseif strcmp(action,'DCT')
    if grayimage,
      commentStr = str2mat(' D = abs(dct2(I,128,128));', ... 
        ' colormap(jet(64)), imagesc(log(D))',' colorbar');
      set(txtHndl,'String',commentStr), drawnow
      D = abs(dct2(I,512,512));
      if danger, delete(haxes), end
      colormap(jet(64)), imagesc(log(D))
      colorbar
    else
      commentStr = str2mat(' D = abs(dct2(X,128,128));', ... 
        ' colormap(jet(64)), imagesc(log(D))',' colorbar');
      set(txtHndl,'String',commentStr), drawnow
      D = abs(dct2(X,512,512));
      if danger, delete(haxes), end
      colormap(jet(64)), imagesc(log(D))
      colorbar
    end

elseif strcmp(action,'Radon')
    if grayimage,
      commentStr = str2mat(' p = radon(I,30,256);',' plot(p)');
      set(txtHndl,'String',commentStr), drawnow
      p = radon(I,30,256);
      if danger, delete(haxes), end
      plot(p)
    else
      commentStr = str2mat(' p = radon(X,30,256);',' plot(p)');
      set(txtHndl,'String',commentStr), drawnow
      p = radon(X,30,256);
      if danger, delete(haxes), end
      plot(p)
    end

elseif strcmp(action,'Rotate')
    if grayimage,
      commentStr = ' imrotate(I,30,''bilinear'',''crop'');';
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      imrotate(I,30,'bilinear','crop')
    else
      commentStr = ' imrotate(X,30,''nearest'',''crop'');';
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      imrotate(X,30,'nearest','crop')
    end

elseif strcmp(action,'Shrink')
    if grayimage,
      commentStr = ' imresize(I,.5)';
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      imresize(I,.5)
    else
      commentStr = ' imresize(X,.5)';
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      imresize(X,.5)
    end

elseif strcmp(action,'Dilate')
    if grayimage,
      if ~isbw(I), 
        I = im2bw(I); 
        commentStr = str2mat(' bw = im2bw(I);',' dilate(bw,ones(3))');
      else
        commentStr = ' dilate(bw,ones(3))';
      end
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      dilate(I,ones(3))
    else
      commentStr = str2mat(' bw = im2bw(X,map);',' dilate(bw,ones(3))');
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      I = im2bw(X,map);
      dilate(I,ones(3))
    end

elseif strcmp(action,'Erode')
    if grayimage,
      if ~isbw(I), 
        I = im2bw(I); 
        commentStr = str2mat(' bw = im2bw(I);',' erode(bw,ones(3))');
      else
        commentStr = ' erode(bw,ones(3))';
      end
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      erode(I,ones(3))
    else
      commentStr = str2mat(' bw = im2bw(X,map);',' erode(bw,ones(3))');
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      I = im2bw(X,map);
      erode(I,ones(3))
    end

elseif strcmp(action,'Perimeter')
    if grayimage,
      if ~isbw(I), 
        I = im2bw(I); 
        commentStr = str2mat(' bw = im2bw(I);',' bwperim(bw)');
      else
        commentStr = ' bwperim(bw)';
      end
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      bwperim(I)
    else
      commentStr = str2mat(' bw = im2bw(X,map);',' bwperim(bw)');
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      I = im2bw(X,map);
      bwperim(I)
    end

elseif strcmp(action,'Thin')
    if grayimage,
      if ~isbw(I), 
        I = im2bw(I); 
        commentStr = str2mat(' bw = im2bw(I);',' erode(bw,''thin'')');
      else
        commentStr = ' erode(bw,''thin'')';
      end
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      erode(I,'thin')
    else
      commentStr = str2mat(' bw = im2bw(X,map);',' erode(bw,''thin'')');
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      I = im2bw(X,map);
      erode(I,'thin')
    end
elseif strcmp(action,'Fatten')
    if grayimage,
      if ~isbw(I), 
        I = im2bw(I); 
        commentStr = str2mat(' bw = im2bw(I);',' dilate(bw,''fatten'')');
      else
        commentStr = ' dilate(bw,''fatten'')';
      end
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      dilate(I,'fatten')
    else
      commentStr = str2mat(' bw = im2bw(X,map);',' dilate(bw,''fatten'')');
      set(txtHndl,'String',commentStr), drawnow
      if danger, delete(haxes), end
      I = im2bw(X,map);
      dilate(I,'fatten')
    end

elseif strcmp(action,'info'),
    set(gcf,'pointer','arrow')
    ttlStr = get(gcf,'Name');
	hlpStr1 = [ ...
		 '                                             '  
         ' This demo lets you explore many of the      '  
         ' commands and functionality in the           '
		 ' Image Processing Toolbox.                   '  
         '                                             '  
         ' Choose an image to process from the first   '  
         ' popup menu or insert your own using IMSHOW  '  
         ' from the command line.  Use the second menu '  
         ' to choose an image processing area:         '  
         ' enhancement, restoration, analyzing,        '  
         ' transforms, or morphological operations.    '  
         ' When you select an area, the buttons will   '
		 ' change to reveal operations that you can    '
		 ' perform in the that area.                   '];
	hlpStr2 = [ ...
         '                                             '  
         ' Press any button to affect the currently    '  
         ' displayed mage.  The actual commands        '  
         ' executed are echoed to the comment          '  
         ' window.  To return to an original           '  
         ' image, select it again from the first menu. '  
         '                                             '  
         ' Some of the operations can only be applied  '
		 ' to gray images.  For these operations, the  '
		 ' image will automatically be converted to    '
		 ' gray.                                       '  
         '                                             '  
         '                                             '  
         ' File name: imdemo.m                         '];

    myFig = gcf;
    helpfun(ttlStr,hlpStr1,hlpStr2);

    % Protect against gcf changing -- Change close button behind
    % helpfun's back
    ch = get(gcf,'ch');
    for i=1:length(ch),
      if strcmp(get(ch(i),'type'),'uicontrol'),
        if strcmp(lower(get(ch(i),'String')),'close'),
          callbackStr = [get(ch(i),'callback') ...
            '; imdemo(''closehelp'',' num2str(myFig) ')'];
          set(ch(i),'callback',callbackStr)
          return
        end
      end
    end
    return

end    % if strcmp(action, ...

set(gcf,'pointer','arrow')
