Display different images in multiple MATLAB GUI windows - image

I am creating a program in matlab that at some points will have two windows open. Both windows try to display a different image via:
matlabImage = imread('C:...SetupA.jpg');
imshow(matlabImage);
axis off
axis image
But what ends up happening is sometimes the image from the second GUI window to open will override the image in the first GUI window until it is closed, and the second GUI window displays nothing. I have made sure there are no reused image variable names. What can I do to prevent this?
Edit: so here are the two GUI windows that interfere with each other
function varargout = ClampRef(varargin)
%Function that displays an image with multiple reference points highlighted
%This needs to have an option to stay open while the program continues
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #ClampRef_OpeningFcn, ...
'gui_OutputFcn', #ClampRef_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
function ClampRef_OpeningFcn(hObject, eventdata, handles, varargin)
matlabImage5 = imread('C:...reference.jpg');
imshow(matlabImage5)
axis off
axis image
handles.output = hObject;
guidata(hObject, handles);
set(handles.pushbutton2,'enable','off')
function varargout = ClampRef_OutputFcn(hObject, eventdata, handles)
uiwait();
global valueout;
varargout{1} = valueout;
% --- Executes on button press in pushbutton1.
%This is a continue button, which enables the close button but continues the
%program
function pushbutton1_Callback(hObject, eventdata, handles)
global valueout;
valueout = 'Finished';
set(handles.pushbutton2,'enable','on')
set(handles.pushbutton1,'enable','off')
uiresume();
% --- Executes on button press in pushbutton2. This is the close button
function pushbutton2_Callback(hObject, eventdata, handles)
closereq();
and the second one:
function varargout = OHMgui1(varargin)
%Calculator like gui to take in the electrical test value while showing an
%image that shows where the probe must be placed for the test
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #OHMgui1_OpeningFcn, ...
'gui_OutputFcn', #OHMgui1_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
function OHMgui1_OpeningFcn(hObject, eventdata, handles, varargin)
matlabImage6 = imread('C:...pos1.jpg');
axis image;
imshow(matlabImage6);
handles.output = hObject;
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = OHMgui1_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)
uiwait();
% Get default command line output from handles structure
global valueout;
varargout{1} = valueout;
% --- 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)
global value;
value = strcat(value, '1');
set(handles.answer_staticText,'String',value);
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)
global value;
value = strcat(value,'2');
set(handles.answer_staticText,'String',value);
guidata(hObject, handles);
% --- 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)
global value;
value = strcat(value,'3');
set(handles.answer_staticText,'String',value);
guidata(hObject, handles);
% --- 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)
global value;
value = strcat(value,'4');
set(handles.answer_staticText,'String',value);
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)
global value;
value = strcat(value,'5');
set(handles.answer_staticText,'String',value);
guidata(hObject, handles);
% --- 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)
global value;
value = strcat(value,'6');
set(handles.answer_staticText,'String',value);
guidata(hObject, handles);
% --- 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)
global value;
value = strcat(value,'7');
set(handles.answer_staticText,'String',value);
guidata(hObject, handles);
% --- 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)
global value;
value = strcat(value,'8');
set(handles.answer_staticText,'String',value);
guidata(hObject, handles);
% --- 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)
global value;
value = strcat(value,'9');
set(handles.answer_staticText,'String',value);
guidata(hObject, handles);
% --- 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)
global value;
value = strcat(value,'0');
set(handles.answer_staticText,'String',value);
guidata(hObject, handles);
% --- Executes on button press in pushbutton11.
function pushbutton11_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global value;
value = strcat(value, '.');
set(handles.answer_staticText,'String',value);
guidata(hObject, handles);
% --- Executes on button press in pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton12 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global value;
value = '';
set(handles.answer_staticText,'String',value);
guidata(hObject, handles);
% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton13 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global value;
global valueout;
valueout = value;
value = '';
set(handles.answer_staticText,'String',value);
if strcmp(valueout, '')==1
f = errordlg('Please enter a value','Null Value Error');
elseif length(find(valueout=='.'))>1
f = errordlg('Value has more than 1 decimal','Decimal Value Error');
else
closereq();
end
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global value;
disp (value);
while 1==1
set(hObject,'String', value)
end
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit 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
I tried using figure, imshow() but it still did not resolve the issue

Related

incoming serial data interfering with MATLAB callbacks

I have a code that utilizes information taken over serial, compares it to a stored string, and then changes the color values of a panel in a GUI. this is done for 2 different panels. once the strcmp for both panels is 1, a button is enabled to close the GUI. i want to use a simple closereq() function to close it, but that wasn't working. to try something different, I commented out the closereq() and added in a disp('Success'). When pressing the button, Success will appear in the command window, but only after I manually pause the program from the editor panel.
this is my code:
function varargout = WorkingGUI3(varargin)
% WORKINGGUI3 MATLAB code for WorkingGUI3.fig
% WORKINGGUI3, by itself, creates a new WORKINGGUI3 or raises the existing
% singleton*.
%
% H = WORKINGGUI3 returns the handle to a new WORKINGGUI3 or the handle to
% the existing singleton*.
%
% WORKINGGUI3('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in WORKINGGUI3.M with the given input arguments.
%
% WORKINGGUI3('Property','Value',...) creates a new WORKINGGUI3 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before WorkingGUI3_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to WorkingGUI3_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 WorkingGUI3
% Last Modified by GUIDE v2.5 02-Oct-2019 14:53:57
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #WorkingGUI3_OpeningFcn, ...
'gui_OutputFcn', #WorkingGUI3_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 WorkingGUI3 is made visible.
function WorkingGUI3_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 WorkingGUI3 (see VARARGIN)
handles.uipanels = [handles.uipanel1, handles.uipanel2];
set(handles.pushbutton5, 'enable', 'off');
% Choose default command line output for WorkingGUI3
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes WorkingGUI3 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = WorkingGUI3_OutputFcn(hObject, eventdata, handles)
delete(instrfind('Port', 'COM3'));
tag = serial('COM3');
fopen(tag);
BOX = char(zeros(2,14)); % matrix to be populated with incoming serial data
TrueValueData = 'C:\RfidChipTrueValues.xlsx';
[~,~,TrueValMat] = xlsread(TrueValueData);
% Creates matrix filled with the correct values
% indexed by box, which is the first row
% all proceeding rows are the master value
for i=1:inf
for n = 1:2
if i>10
readData = fscanf(tag);
if length(readData)>12
BOX(str2double(readData(8)),1:14)= readData(11:24);
if strcmp(TrueValMat{2,n}, BOX(n,:))
set(handles.uipanels(n), 'BackgroundColor', 'g');
else
set(handles.uipanels(n), 'BackgroundColor', 'r');
end
drawnow
if strcmp(TrueValMat{2,1}, BOX(1,:))...
&& strcmp(TrueValMat{2,2}, BOX(2,:)) == 1
set(handles.pushbutton5, 'enable', 'on');
end
end
end
end
end
% 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 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)
%closereq();
disp('It Works')
the final section
% --- 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)
%closereq();
disp('Success')
holds the callback to the button. I know its it correctly connected to the .fig because pressing the button makes Success appear, but only after manually pausing the program.
any help would be much appreciated.
As #Rotem showed, adding a pause after the first for loop successfully allowed the program to close.

How to display desired image in MATLAB GUI?

I have a GUI as shown below:
And I would like to apply Otsu threshold on the image displayed after contrast enhancement. But whenever I pressed the apply button for Otsu threshold,the Otsu threshold will be applied on the original image instead of the image after contrast enhancement (refer to the attached GUI and coding). So how do I overcome this problem?
function pushbutton10_Callback(hObject, eventdata, handles)
I = im2double(handles.im);
imshow(I);
% prompts for the two inputs
prompt = {'Enter LOW contrast limit:','Enter HIGH contrast limit:'};
% title of the dialog box
dlg_title = 'Input';
% number of input lines available for each variable
num_lines = 1;
% default answer for each input
defaultans = {'0','1'};
% generate the dialog box and wait for answer
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
% convert string answers into doubles
lo_in = str2double(answer{1});
hi_in = str2double(answer{2});
% apply on image
K = imadjust(I,[lo_in hi_in],[]);
axes(handles.axes2);
imshow(K);
axes(handles.axes3);
imshow(K);
function pushbutton12_Callback(hObject, eventdata, handles)
I = im2double(handles.im);
im = rgb2gray(I);
level = graythresh(im)
a = im2bw(im,level);
axes(handles.axes3);
imshow(a);
axes(handles.axes4);
imshow(a);
I think it better to define some field for handles structure in openingFcn function like below
handles.OriginaImage=0;
handles.afterEnhancement=0;
and when you read Original Image you update handles.OriginalImage as following code
handles.OriginalImage=imread("whatever.whatever");
% don't forget below line because this line update handles structure
guidata(hObject,handles)
and when you apply contrast Enhancement you save it in handles.afterEnhancement and again update handles structure
lo_in = str2double(answer{1});
hi_in = str2double(answer{2});
% apply on image
handles.afterEnhancement = imadjust(handles.OriginalImage,[lo_in hi_in],[]);
guidata(hObject,handles)
then in pushbutton12_callback you can apply otsu method on Enhancement image by handles.afterEnhancement
I = im2double(handles.afterEnhancement);
im = rgb2gray(I);
level = graythresh(im)
a = im2bw(im,level);
axes(handles.axes3);
imshow(a);
I test this codes in my MATLABin my way
Example code
function varargout = Sdfl(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', #Sdfl_OpeningFcn, ...
'gui_OutputFcn', #Sdfl_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
function Sdfl_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.original_image=0;
handles.blur_image=0;
% Update handles structure
guidata(hObject, handles);
function varargout = Sdfl_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function pushbutton1_Callback(hObject, eventdata, handles)
handles.original_image=imread('pout.tif');
guidata(hObject, handles); %this line will update handles structure
axes(handles.axes1)
imshow(handles.original_image)
function pushbutton2_Callback(hObject, eventdata, handles)
handles.blur_image=imadjust(im2double(handles.original_image),[0.3 0.7],[]);
guidata(hObject, handles);
axes(handles.axes2)
imshow(handles.blur_image);
function pushbutton3_Callback(hObject, eventdata, handles)
thresh=graythresh(handles.blur_image);
bw_image=im2bw(handles.blur_image,thresh);
axes(handles.axes3)
imshow(bw_image);
output

create GUI for selecting and filtering of Image

i would like to practice on designing GUI in matlab ,this GUI has two function -one for selecting of image and second for filtering,general structure of such Graphical interface is very simple
and here is two code -one which select image after pressing on select image and second which filters image using simple average filter after clicking on filter image
function select_image_Callback(hObject, eventdata, handles)
% hObject handle to select_image (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.jpg';'*.png'},'File select');
image=strcat(pathname,filename);
axes(handles.axes1);
imshow(image);
and for filtering
function filter_image_Callback(hObject, eventdata, handles)
% hObject handle to filter_image (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
h = ones(5,5) / 25;
Filtered_image = imfilter(image,h);
axes(handles.axes2);
imshow(Filtered_image);
but when i run code, i selected simple this file
i got following error
Error using imfilter
Expected input number 1, A, to be one of these types:
numeric, logical
Instead its type was matlab.graphics.primitive.Image.
Error in imfilter>parse_inputs (line 186)
validateattributes(a,{'numeric' 'logical'},{'nonsparse'},mfilename,'A',1);
Error in imfilter (line 118)
[a, h, boundary, sameSize, convMode] = parse_inputs(varargin{:});
Error in filter_image_filter_image_Callback (line 92)
Filtered_image = imfilter(image,h);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in filter_image (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)filter_image('filter_image_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
why happens this? thanks in advance
based on my question and related to problem, i have decided to include my solution as well ,which i found after researching, here is fragments of my code, first of thanks to # Benoit_11 for his advice and i have changed name from image to selected_image, related to my comment about accessing one variable(in this case image) form another function i used function getimage, so there is my complete solution
function select_image_Callback(hObject, eventdata, handles)
% hObject handle to select_image (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.jpg';'*.png'},'File select');
selected_image=strcat(pathname,filename);
axes(handles.axes1);
imshow(selected_image);
% --- Executes on button press in filter_image.
function filter_image_Callback(hObject, eventdata, handles)
% hObject handle to filter_image (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selected_image=getimage(handles.axes1);
h = ones(5,5) / 25;
Filtered_image = imfilter(selected_image,h);
axes(handles.axes2);
imshow(Filtered_image);

How to display an image selected using PushButton in Matlab

I'm working on basic GUI in Matlab-2012a. I wanted to display the select and display the image using a push button.
Here's my code:
% --- 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)
global TrainingData;
global filenames;
TrainingData={};
[filenames, pathname] = uigetfile({'*.jpg';'*.png';'*.bmp'});
if ~ischar(filenames) % on cancel press you display a message of error with errordlg
errordlg('Error!','No file selected'); % displays an error message by means of errordlg function
return;
end
axes(handles.myaxesImage);
imshow(filenames);
I can browse the image, but I'm not able to display the same. I'm getting the following Error Message:
Reference to non-existent field 'axesImage'.
Error in GUI>pushbutton2_Callback (line 93)
axes(handles.axesImage);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in GUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
#(hObject,eventdata)GUI('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Any Suggestions?
Thank you in advance.
According to your code
axes(handles.myaxesImage);
MATLAB said
Reference to non-existent field 'axesImage'
So there is a problem for MATLAB to recognize object handles.myaxesImage
Because it doesn't exist!
Here is an example
In the .m file of my GUI
I want to show image on axes whose tag is axes1
function openFile_Callback(hObject, eventdata, handles)
% hObject handle to openFile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%== GUI get file in the folder ==%
[FileName,PathName] = uigetfile({'*.tif';'*.jpg';'*.png'},'Select a image file');
%== Create a object handles.img to load image ==%
handles.img = imread(FileName);
%== call axes whose tag is axes1 to show image ==%
axes(handles.axes1);
imshow(handles.img);
guidata(hObject, handles);
Next time you want to use any varable in GUI
Don't forget to check whether that object exists
Hope it helps

How can I pass a loaded image to another Matlab file using pushbutton?

After loading an image into a Matlab GUI, how can I pass that image using pushbutton to another Matlab file? When I push the button in my GUI, the image should be passed to my Matlab code.
Here is my GUI code
% --- 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)
[filename,pathname]=uigetfile(...
{'*.jpg;*.gif;*.png;*.bmp',...
'image file(*.jpg,*.gif,*.png,*.bmp)';'*.*','all files(*.*)'},...
'open the image file to be verified');
fullimagefilename = fullfile(pathname,filename);
axes1 = imread(fullimagefilename);
axes(handles.axes1);
image(axes1);
%axes(handles.axes1);
%imshow('E:\degraded images\3.jpg')
% --- 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)
-pushbutton 1 is used to browse file .
-and by pushbutton2 i want to pass the browsed file to my base file and its code is
im=imread('E:\degraded images\3.jpg');
hsv = rgb2hsv(im);
hueImage = hsv(:,:, 1);
meanHue = mean2(hueImage);
figure, imshow(im);
title(meanHue);
%figure , imshow(im);
%im = imresize(im, 0.5);
%im2 = imread('E:\degraded images\3.jpg') ;
im2= im;
im=im(:,:,1);
sigmaA=8;
sigmaB=10;
sigmaMax=max([sigmaA sigmaB]);
fsz=[sigmaMax, sigmaMax];
If I understand correctly, the GUI uses pushbutton1 to allow the user to choose a file and display it in the axes. When the user presses the other button, pushbutton2, which is presumably on the same GUI, you want the callback for that button to have access to the selected (or browsed) image.
This can be done by creating a field in the handles structure for the selected image data, then storing this data using the guidata function.
In your first callback, do something like the following
function pushbutton1_Callback(hObject, eventdata, handles)
[filename,pathname]=uigetfile(...
{'*.jpg;*.gif;*.png;*.bmp',...
'image file(*.jpg,*.gif,*.png,*.bmp)';'*.*','all files(*.*)'},...
'open the image file to be verified');
% need to handle the case where the user presses cancel
if filename~=0
fullimagefilename = fullfile(pathname,filename);
% create an img field in handles for the image that is being loaded
handles.img = imread(fullimagefilename);
axes(handles.axes1);
image(handles.img);
% save the application data
guidata(hObject,handles);
end
Now in your second callback, you can access this file through the handles struct
function pushbutton2_Callback(hObject, eventdata, handles)
% check to ensure that img exists in handles
if isfield(handles,'img')
% copy the image from the handles structure
im=handles.img;
% continue with your code
hsv = rgb2hsv(im);
% etc.
end
And that is it - the first callback saves the image to the handles structure as a field within it, and the second callback retrieves that image from handles via the img field.
Try it out and see what happens!

Resources