image thumbnail

Image Section Generator

by

 

21 May 2009 (Updated )

Image Section Generator grabs cursor-designated linescans from microscope images

lbox2(varargin)
%--------------------------------------------------------------------%
%	    Alison Chaiken, sole author and maintainer     			     %
%        ------------------------------------------------------      %
%	    GUI-based Matlab and FEMLAB data analysis, instrument	     %
%	    control, statistical analysis, finite-element modelling,     %
%	    image acquisition and analysis          				     %
%       -------------------------------------------------------      %
%	    alchaiken@gmail.com			                    		     %
%	    http://www.exerciseforthereader.org/         			     %
%	    (001)650-279-5600					                         %
%--------------------------------------------------------------------%

function varargout = lbox2(varargin)
% Employing a listbox format, offer the user a choice of image files in
% compatible formats to process.    Supports temporary change of directory
% to find new images.    Only works with TIFF and JPEG images.
% Case-insensitive.    Based on the Mathworks-supplied lbox2.m example, but
% heavily modified.

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

% Copyright 2000-2006 The MathWorks, Inc.

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

% Last Modified by GUIDE v2.5 28-Apr-2009 10:48:21

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',          mfilename, ...
    'gui_Singleton',     gui_Singleton, ...
    'gui_OpeningFcn',    @lbox2_OpeningFcn, ...
    'gui_OutputFcn',     @lbox2_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 during object creation, after setting all properties.
function lbox2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to lbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Add the current directory to the matlab search path, as the pwd might change thru' the
% gui. Remove the directory from the path when gui is closed
% (See lbox2_DeleteFcn)
setappdata(hObject, 'StartPath', pwd);
addpath(pwd);
% Update handles structure
guidata(hObject, handles);

% --- Executes just before lbox2 is made visible.
function lbox2_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 lbox2 (see VARARGIN)

% Choose default command line output for lbox2
handles.output = hObject;
%create more robust name than "figureN"
handles.lbox2 = hObject;
set(hObject,'Name','Choose an image')
% gcf is used by LowerLeftCurrentWindow
gcf = hObject;
LowerLeftCurrentWindow

% Update handles structure
guidata(hObject, handles);

% Check to see the main gui is passed in
dontOpen = false;
mainGuiInput = find(strcmp(varargin, 'ImageSectionGenerator'));

if (isempty(mainGuiInput)) || (length(varargin) <= mainGuiInput) || (~ishandle(varargin{mainGuiInput+1}))
    dontOpen = true;
end


if dontOpen
    disp('-----------------------------------------------------');
    disp('Improper input arguments. Pass a property value pair')
    disp('whose name is "ImageSectionGenerator" and value is the handle')
    disp('to the main figure.');
    disp('-----------------------------------------------------');
else
    if nargin == 5,
        INITIAL_DIR = pwd;
    elseif nargin > 6
        if strcmpi(varargin{1},'dir')
            if exist(varargin{2},'dir')
                INITIAL_DIR = varargin{2};
            else
                errordlg('Input argument must be a valid directory','Input Argument Error!')
                return
            end
        else
            errordlg('Unrecognized input argument','Input Argument Error!');
            return;
        end
    end


    % Remember the handle, and adjust our position
    handles.ImageSelectorMain = varargin{mainGuiInput+1};

    % Set the initial text
    mainHandles = guidata(handles.ImageSelectorMain);
    set(handles.NewImageFileName, 'String', get(mainHandles.ImageFileName, 'String'));
    
    % Populate the listbox
    load_listbox(INITIAL_DIR,handles)
    % Return figure handle as first output argument

    %lbox2() is modal
    uiwait(hObject);
end


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

% ------------------------------------------------------------
% Read the current directory and sort the names
% ------------------------------------------------------------
function load_listbox(dir_path,handles)
cd (dir_path)
dir_struct = dir(dir_path);
[sorted_names,sorted_index] = sortrows({dir_struct.name}');

j=1;
numfiles=max(sorted_index);
%plus acts as an effective 'or' since we only care whether values in the
%arrays jpgs and tifs are zero or not
jpgs=strfind(sorted_names,'jpg');
JPGS=strfind(sorted_names,'JPG');
tifs=strfind(sorted_names,'tif');
TIFS=strfind(sorted_names,'TIF');
new_names{numfiles}=[];
new_dir_struct=dir_struct;
new_dir_struct(:)=[];
for i=1:numfiles
    if ((~isempty(jpgs{i})) || (~isempty(JPGS{i})) || (~isempty(tifs{i})) ...
            || (~isempty(TIFS{i})) || (dir_struct(i).isdir == 1))
        %if ~isempty(jpgs{i})
        new_names{j} = sorted_names{i};
        new_dir_struct(j) = dir_struct(i);
        j = j + 1;
    end
end
%handles.file_names = sorted_names;
handles.file_names = new_names;
handles.is_dir = [new_dir_struct.isdir];
handles.sorted_index = sorted_index;
guidata(handles.lbox2,handles)
set(handles.listbox1,'String',handles.file_names,'Value',1)
set(handles.text1,'String',pwd)


% ------------------------------------------------------------
% Callback for list box - open .fig with guide, otherwise use open
% ------------------------------------------------------------
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

%'SelectionType' refers to mouse action; 'normal' is left-click
get(handles.lbox2,'SelectionType');
if strcmp(get(handles.lbox2,'SelectionType'),'normal')
    index_selected = get(handles.listbox1,'Value');
    file_list = get(handles.listbox1,'String');
    filename = file_list{index_selected};
    if  handles.is_dir(handles.sorted_index(index_selected))
        cd (filename)
        load_listbox(pwd,handles)
    else
        set(handles.NewImageFileName,'String',filename)
        %     [path,name,ext,ver] = fileparts(filename);
        %     switch ext
        %         case '.fig'
        %             guide (filename)
        %         otherwise
        %             try
        %                 open(filename)
        %             catch
        %                 errordlg(lasterr,'File Type Error','modal')
        %             end
        %     end
        set(handles.FileSelectionDoneButton,'BackgroundColor','green')
    end
end


% --- 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, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
%usewhitebg = 1;
%if usewhitebg
    set(hObject,'BackgroundColor','white');
%else
%    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
%end


% --- Executes on button press in FileSelectionDoneButton.
function FileSelectionDoneButton_Callback(hObject, eventdata, handles)
% hObject    handle to FileSelectionDoneButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%set(handles.ImageFileName,'String',filename)

text = get(handles.NewImageFileName, 'String');
main = handles.ImageSelectorMain;
if(ishandle(main))
    mainHandles = guidata(main);
    set(mainHandles.ImageFileName, 'String', text)
    %store full path name in appdata of ImageFileName in case we have cd'ed
    %within lbox2
    setappdata(mainHandles.ImageFileName, 'ImageFileDirectory', pwd);
    % Update handles structure
    guidata(handles.ImageSelectorMain, mainHandles);
end
figure_CloseRequestFcn(hObject, eventdata, handles)
%uiresume(mainHandles.ImageSectionGenerator);


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

figure_CloseRequestFcn(hObject, eventdata, handles)



% --- Executes when user attempts to close figure.
function figure_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

start = getappdata(handles.lbox2, 'StartPath');
cd(start)
% Remove the directory added to the path in the lbox2_CreateFcn.
%rmpath(start);

close(handles.lbox2);

lbox2_DeleteFcn(hObject, eventdata, handles)
%uiresume(handles.ImageSelectorMain);


% --- Executes during object deletion, before destroying properties.
% All graphics objects have delete functions.   Figures have close request
% functions in addition.
function lbox2_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to lbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% don't have to request close of figure, since user has requested deletion
% (presumably by clicking upper right-hand corner X)
uiresume(handles.ImageSelectorMain);


Contact us