Image browser- populate listbox and show image problems
2 ビュー (過去 30 日間)
古いコメントを表示
Based on Image Analyst answer to the question in http://www.mathworks.com/matlabcentral/answers/78421
I was slightly modifying the code (marked as DIFFERENT FROM ORIGINAL) in order to populate listbox with file names in order to make a very simple image browser.
Unfortunately it does not work, neither populating the list and neither showing the file. Any reason why? and how can I fix it?
Code:
function varargout = browser(varargin)
% BROWSER MATLAB code for browser.fig
% BROWSER, by itself, creates a new BROWSER or raises the existing
% singleton*.
%
% H = BROWSER returns the handle to a new BROWSER or the handle to
% the existing singleton*.
%
% BROWSER('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in BROWSER.M with the given input arguments.
%
% BROWSER('Property','Value',...) creates a new BROWSER or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before browser_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to browser_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 browser
% Last Modified by GUIDE v2.5 28-Oct-2013 14:17:15
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @browser_OpeningFcn, ...
'gui_OutputFcn', @browser_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 browser is made visible.
function browser_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 browser (see VARARGIN)
% Choose default command line output for browser
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes browser wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = browser_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 ImageFolder.
function ImageFolder_Callback(hObject, eventdata, handles)
% hObject handle to ImageFolder (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%=====================================================================
% --- Load up the listbox with image files in folder handles.ImageFolder
indir=uigetdir;
handles.ImageFolder =char(indir);
function handles = LoadImageList(handles)
ListOfImageNames = {};
folder = handles.ImageFolder;
if length(folder) > 0
if exist(folder,'dir') == false
msgboxw(['Folder ' folder ' does not exist.']);
return;
end
% fprintf(1, 'Getting list of images in folder: %s\n', folder);
else
fprintf('No folder specified as input for function LoadImageList.\n');
WarnUser('No folder specified as input for function LoadImageList.');
return;
end
% If it gets to here, the folder is good.
ImageFiles = dir([folder '\*.*']);
for Index = 1:length(ImageFiles)
baseFileName = ImageFiles(Index).name;
[folder2, name, extension] = fileparts(baseFileName);
extension = upper(extension);
switch lower(extension)
case {'.png', '.bmp', '.jpg', '.tif'}
% Allow only PNG, TIF, JPG, or BMP images
ListOfImageNames = [ListOfImageNames baseFileName];
otherwise
end
end
set(handles.lstImageList,'string',ListOfImageNames);
% Need to deselect everything otherwise if new folder has fewer files than the last folder used, the listbox won't show up.
set(handles.lstImageList,'value', []);
return; % from LoadImageList()
% --- Executes on selection change in lstImageList.
function lstImageList_Callback(hObject, eventdata, handles)
% hObject handle to lstImageList (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns lstImageList contents as cell array
% contents{get(hObject,'Value')} returns selected item from lstImageList
% Get image name
Selected = get(handles.lstImageList, 'value');
% If more than one is selected, bail out.
if length(Selected) > 1
baseImageFileName = '';
% Change mouse pointer (cursor) to an arrow.
set(gcf,'Pointer','arrow')
drawnow; % Cursor won't change right away unless you do this.
return;
end
% If only one is selected, display it.
ListOfImageNames = get(handles.lstImageList, 'string');
% Get the name of the item in the listbox that they clicked on.
baseImageFileName = cell2mat(ListOfImageNames(Selected));
% Prepend folder.
fullImageFileName = fullfile(handles.ImageFolder, baseImageFileName);
% Display the image. DIFFERENT FROM ORIGINAL
axes(handles.imageAxes);
imshow(fullImageFileName);
% --- Executes during object creation, after setting all properties.
function lstImageList_CreateFcn(hObject, eventdata, handles)
% hObject handle to lstImageList (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
0 件のコメント
回答 (1 件)
Image Analyst
2013 年 10 月 28 日
Right before you execute this line:
set(handles.lstImageList,'string',ListOfImageNames);
what is inside ListOfImageNames?
5 件のコメント
参考
カテゴリ
Help Center および File Exchange で Graphics Object Properties についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!