Image not displayed properly in GUI
2 views (last 30 days)
Show older comments
I have created a Matlab GUI where I would display multiple images from a folder in the image axes at GUI. I could read all the image files. Unfortunately, the image axes in the GUI is blank as shown in the figure below. Any help would be appreciated.
function varargout = dm(varargin)
% DM MATLAB code for dm.fig
% DM, by itself, creates a new DM or raises the existing
% singleton*.
%
% H = DM returns the handle to a new DM or the handle to
% the existing singleton*.
%
% DM('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DM.M with the given input arguments.
%
% DM('Property','Value',...) creates a new DM or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before dm_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to dm_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 dm
% Last Modified by GUIDE v2.5 06-Apr-2021 01:25:06
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @dm_OpeningFcn, ...
'gui_OutputFcn', @dm_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 dm is made visible.
function dm_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 dm (see VARARGIN)
% Choose default command line output for dm
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes dm wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = dm_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 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)
myFolder = 'D:\regionGrowing_MLT\Images';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray = imread(fullFileName);
axes(handles.axes1);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
handles.imageArray=imageArray;
end
% Update handles structure
guidata(hObject, handles);
3 Comments
Rik
on 6 Apr 2021
You're overwriting handles.imageArray every iteration, and you're using the same axes object to show every image. I would also suggest passing the handle to axes directly to imshow, instead of what you are currently doing to ensure the proper parent object.
As for your folder selection: I would suggest splitting it into a different function that uses a while loop to check if the folder is valid.
Also, the attached images are just 1 pixel. Why do you expect to notice them anyway?
And I have probably linked this before, but GUIDE is about to be removed from Matlab. Anything non-trivial should not be designed with GUIDE. For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!