MATLAB Answers

Using GUI, use one push button as an image browser and another to process the image that was chosen?

92 views (last 30 days)
Sean
Sean on 28 Feb 2015
Answered: Ka Mirul on 20 Nov 2017
Hi I'm creating a GUI for part of an image processing project at university. To this point I've created a push button which allows me to browse through my working directory and select either a 'jpg' or 'bmp' image: below is the code:
% --- Executes on button press in pushbutton1(Image Browser).
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';'*.bmp'},'File Selector');
image = strcat(pathname, filename);
axes(handles.axes1);
imshow(image)
set(handles.edit1,'string',filename);
set(handles.edit2,'string',image);
I'm looking to create another push button which will plot a histogram of the image I read in, I'm not sure how to define the image under the new function as the image that was chosen previously, could any one help, below is the code I have at the minute for the histogram push button but it is giving out errors;
% --- Executes on button press in pushbutton2 (Histogram plot).
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)
axes(handles.axes1);
histogram(double(image(:)),'Normalization','pdf');
could any one help me out?

  0 Comments

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 1 Mar 2015
Sean - image is the name of a built-in MATLAB function so it is not a good idea to name your variable image and so you should rename it to something else.
As for having your second callback be "aware" of the image that was chosen in the first callback, note the comment for the handles structure input
% handles structure with handles and user data (see GUIDATA)
You can update this structure with the image (user data) in the first callback so that any other callbacks that receive handles as an input will have access to it. Try something like
function pushbutton1_Callback(hObject, eventdata, handles)
[filename pathname] = uigetfile({'*.jpg';'*.bmp'},'File Selector');
handles.myImage = strcat(pathname, filename);
axes(handles.axes1);
imshow(handles.myImage)
set(handles.edit1,'string',filename);
set(handles.edit2,'string',image);
% save the updated handles object
guidata(hObject,handles);
In your second callback, just reference the loaded image as handles.myImage as
function pushbutton2_Callback(hObject, eventdata, handles)
if isfield(handles,'myImage')
% do stuff
end
Try the above and see what happens!

  4 Comments

Show 1 older comment
Sean
Sean on 2 Mar 2015
I got it working! thanks , I made a typo with respect to you correction! Thanks for the help again, much appreciated.
ayushi
ayushi on 4 May 2016
sir m not getting how to do that suppose i have a following code for button1:
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)
set(handles.edit3, 'Visible','off');
% Build the complete filename
[filename, pathname]=uigetfile( {'*.jpg';'*.jpeg';'*.gif';'*.png';'*.bmp'},'Select file');
MyImage = strcat(pathname, filename);
%This code checks if the user pressed cancel on the dialog.
if isequal(filename,0) || isequal(pathname,0)
uiwait(msgbox ('User pressed cancel','failed','modal') )
hold on;
else
uiwait(msgbox('User selected image sucessfully','sucess','modal'));
hold off;
imshow(MyImage,'Parent',handles.axes2);
end
handles.output = hObject;
guidata(hObject, handles);
and in second button i want to apply further processing on the browsed image like:
function pushbutton2_Callback(hObject, eventdata,handles)
%hold off;
if isfield(handles,'MyImage')
axes2(handles.axes);
hold on;
% Convert RGB image to gray scale image
image=rgb2gray(MyImage);
hold off;
imshow(image,'Parent',handles.axes2);
%gaussian filter:
hold on;
Iblur1 = imgaussfilt(image,2);
Iblur2 = imgaussfilt(image,4);
Iblur3 = imgaussfilt(image,8);
Display the original image and all the filtered images.
%figure(3)
hold off;
imshow(image,handles.axes2)
%title('Original image')
hold on;
%figure(4)
hold off;
imshow(Iblur1,handles.axes2)
%title('Smoothed image, \sigma = 2')
hold on;
%figure(5)
hold off;
imshow(Iblur2,handles.axes2)
%title('Smoothed image, \sigma = 4')
hold on;
%figure(6)
hold off;
imshow(Iblur3,handles.axes2)
%title('Smoothed image, \sigma = 8')
end
its not showing image after processing in axes why?how can we resolve that error?please guide

Sign in to comment.

More Answers (1)

Ka Mirul
Ka Mirul on 20 Nov 2017
I found a video that help me, it is about creating GUI to browse an image and display the image and its name. It should help you : https://youtu.be/7EmFShs5y9I

  0 Comments

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!

Translated by