RESET Button in GUI
Show older comments
When I press the reset button, the original image doesn't appear, and there is no visible change (callback 5).
function varargout = GUITask1(varargin)
% GUITASK1 MATLAB code for GUITask1.fig
% GUITASK1, by itself, creates a new GUITASK1 or raises the existing
% singleton*.
%
% H = GUITASK1 returns the handle to a new GUITASK1 or the handle to
% the existing singleton*.
%
% GUITASK1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUITASK1.M with the given input arguments.
%
% GUITASK1('Property','Value',...) creates a new GUITASK1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUITask1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUITask1_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 GUITask1
% Last Modified by GUIDE v2.5 15-Sep-2023 18:29:04
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUITask1_OpeningFcn, ...
'gui_OutputFcn', @GUITask1_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 GUITask1 is made visible.
function GUITask1_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 GUITask1 (see VARARGIN)
% Choose default command line output for GUITask1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUITask1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUITask1_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 pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes3);
imageData = rgb2gray(get(handles.axes3, 'UserData')); % Convert to grayscale
imhist(imageData, 256); % Display histogram with 256 bins
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes3);
imshow(imcomplement(get(handles.axes3, 'UserData')));
% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes3);
edgeImage = edge(rgb2gray(get(handles.axes3, 'UserData')));
imshow(edgeImage);
% --- 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)
[baseFileName, folder] = uigetfile({'*.bmp;*.jpg;*.jpeg', 'Image Files (*.bmp,*.jpg,*.jpeg)'}, 'Select an image file');
if baseFileName == 0
% User canceled the file selection
return;
end
fullFileName = fullfile(folder, baseFileName);
% Store the original image in the UserData of axes3
axes(handles.axes3);
originalImage = imread(fullFileName);
imshow(originalImage);
set(handles.axes3, 'UserData', originalImage);
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes3);
grayImage = rgb2gray(get(handles.axes3, 'UserData'));
imshow(grayImage);
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes3);
binaryImage = imbinarize(rgb2gray(get(handles.axes3, 'UserData')));
imshow(binaryImage);
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes3);
imshow(get(handles.axes3, 'UserData'));
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes3);
imhist(rgb2gray(get(handles.axes3, 'UserData')));
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes3);
complementedImage = imcomplement(get(handles.axes3, 'UserData'));
imshow(complementedImage)
2 Comments
Cris LaPierre
on 18 Sep 2023
It might be easier to help if you attached you mlapp file to your post using the paperclip icon, along with a couple sample images.
Which button is your reset button?
Deema
on 18 Sep 2023
Answers (1)
Cris LaPierre
on 18 Sep 2023
Moved: Cris LaPierre
on 18 Sep 2023
What version of MATLAB are you using? Before you get too far into this project, I'd suggest switching to app designer. I think it will ultimately make things easier for you.
I'm assuming you want your reset button to show the original RGB image. However, each call to imshow clears the axes. When that happens, you lose your saved data, since you are saving it to the axes. You want to save it to your app instead.
In GUIDE, you do that by adding your variable to the handles structure, and updating it using guidata (see here)
% --- 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)
[baseFileName, folder] = uigetfile({'*.bmp;*.jpg;*.jpeg;*.png', 'Image Files (*.bmp,*.jpg,*.jpeg,*.png)'}, 'Select an image file');
if baseFileName == 0
% User canceled the file selection
return;
end
fullFileName = fullfile(folder, baseFileName);
% Store the original image in the UserData of axes3
handles.originalImage = imread(fullFileName);
imshow(handles.originalImage,'Parent',handles.axes3);
% Update handles structure
guidata(hObject, handles);
...
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
imshow(handles.originalImage, 'Parent',handles.axes3);
You'll want to make similar changes to all your callbacks.
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!