Unable to process multiple files in Matlab GUI

1 view (last 30 days)
Warid Islam
Warid Islam on 4 Apr 2021
Commented: Rik on 8 Apr 2021
Hello Everyone,
I have created a Matlab GUI where I would load multiple images by clicking a button. I would then segment all the images together by clicking the segment button. I could load mutiple images together properly by clicking the Load button . However, I am unable to segment those mutiple images together when I click the Segment button. I have attached few images. Any help would be appreciated. Please find my code below:
function varargout = ngui(varargin)
% NGUI MATLAB code for ngui.fig
% NGUI, by itself, creates a new NGUI or raises the existing
% singleton*.
%
% H = NGUI returns the handle to a new NGUI or the handle to
% the existing singleton*.
%
% NGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in NGUI.M with the given input arguments.
%
% NGUI('Property','Value',...) creates a new NGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ngui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ngui_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 ngui
% Last Modified by GUIDE v2.5 30-Mar-2021 22:59:22
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ngui_OpeningFcn, ...
'gui_OutputFcn', @ngui_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 ngui is made visible.
function ngui_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 ngui (see VARARGIN)
% Choose default command line output for ngui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ngui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ngui_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)
% Specify the folder where the files live.
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);
num_files=length(theFiles);
imgs=cell(1,num_files);
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);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
InputImage=imageArray(:,k);
axes(handles.axes1);
handles.InputImage=InputImage;
end
% Update handles structure
guidata(hObject, handles);
% --- 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)
for k = 1 : length(theFiles)
InputImage=handles.InputImage;
% InputImage=handles.InputImage;
InputImage=rgb2gray(InputImage);
x=300; y=340;
a=imgaussfilt(InputImage,2);
b=adapthisteq(a);
m=regiongrowing_MLT(b,x,y,12);
m=imfill(m,'holes');
% figure,imshow(m)
bw=imbinarize(m);
bw=bwareafilt(bw,1);
% figure,imshow(bw)
I = imresize(InputImage,.5); %-- make image smaller
m1 = imresize(bw,.5); % for fast computation
% subplot(2,2,1); imshow(I); title('Input Image');
% subplot(2,2,2); imshow(m1); title('Initialization');
% subplot(2,2,3); title('Segmentation');
seg = region_seg(I, m1, 30); %-- Run segmentation
% subplot(2,2,4); imshow(seg); title('Global Region-Based Segmentation');
imshow(seg);
axes(handles.axes2);
handles.seg=seg;
end
guidata(hObject, handles);
The following error message is displayed
Unrecognized function or variable 'theFiles'.
Error in ngui>pushbutton2_Callback (line 124)
for k = 1 : length(theFiles)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in ngui (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)ngui('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

Answers (1)

Image Analyst
Image Analyst on 4 Apr 2021
The files is not defined in that pushbutton. Please attach your .fig file so I can fix it.
Your pushbutton1_Callback() should fill up a listbox, not just repeatedly overwrite an array imageArray and then do nothing with it.
  10 Comments
Warid Islam
Warid Islam on 8 Apr 2021
Hi @Rik,
I tried using the APP DESIGNER. I could load the image in the GUI but I am having some errors while segmenting the image by clicking the Segment button.
Error using matlab.ui.control.Image/set.ImageSource (line 123)
ImageSource value must be a valid file path, or an m-by-n-by-3 color data matrix.
Error in new/SegmentButtonPushed (line 69)
app.Image2.ImageSource=finalimg;
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 386)
Error while evaluating Button PrivateButtonPushedFcn.
Rik
Rik on 8 Apr 2021
A GUI is a graphical user interface. You should make sure your code works first, before you build a GUI around it.
Regarding that error message, what don't you understand from it? You wrote the code, so you must understand what you want to do. What are you providing as input? The error message suggest it should be either a path, or an RGB image.

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!