MATLAB: Displaying an image on GUIDE created figure

figureguideimage processingMATLAB

Hi,
I created a gui using GUI. Purpose is to open an image and display on the gui itself. It has a menu item says 'File' and inside the 'File', there is a sub option called 'Open'. When I click that it opens a file browser to select a file.
When I load an image for the first time it works. If I try to load another image and display it again, then I looses the menu control. Seems like a new figure has created with the same title. I have posted my code and two images to describe my issue.
When i load an image for the first time,
When I load it for the second time,
As you can see, the 'File' menu is not present anymore.
Following is my code,
function varargout = test_gui(varargin)
% TEST_GUI MATLAB code for test_gui.fig
% TEST_GUI, by itself, creates a new TEST_GUI or raises the existing
% singleton*.
%




% H = TEST_GUI returns the handle to a new TEST_GUI or the handle to
% the existing singleton*.
%
% TEST_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TEST_GUI.M with the given input arguments.
%
% TEST_GUI('Property','Value',...) creates a new TEST_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before test_gui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to test_gui_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 test_gui
% Last Modified by GUIDE v2.5 24-Mar-2017 15:11:35
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @test_gui_OpeningFcn, ...
'gui_OutputFcn', @test_gui_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 test_gui is made visible.
function test_gui_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 test_gui (see VARARGIN)
% Choose default command line output for test_gui
handles.output = hObject;
handles.rgb_img = 0;
handles.rgb_img_set = 0;
handles.gray_img = 0;
handles.gray_img_set = 0;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes test_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = test_gui_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;
% --------------------------------------------------------------------

function file_Callback(hObject, eventdata, handles)
% hObject handle to file (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function openimage_Callback(hObject, eventdata, handles)
% hObject handle to openimage (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;*.jpeg;*.bmp;*.tif', ...
'(*.jpg,*jpeg,*.png,*.bmp,*.tif)'}, ...
'Browse Image');
if filename == 0
handles.rgb_img = 0;
handles.rgb_img_set = 0;
display('no image selected');
else
img = imread(strcat(pathname, filename));
dimension = numel(size(img));
if(dimension == 3)
handles.rgb_img = img;
handles.rgb_img_set = 1;
handles.gray_img = rgb2gray(handles.rgb_img);
handles.gray_img_set = 1;
figure(handles.output), imshow(img);
elseif(dimension == 2)
handles.rgb_img_set = 0;
handles.gray_img = img;
handles.gray_img_set = 1;
figure(handles.output), imshow(img);
else
errordlg('Image dimension invalid');
end
end
guidata(handles.output, handles);
Could some one point out how to load the second image correctly?
Probably I am making a rookie mistake. I will really appreciate any help I can get.
Thank you.

Best Answer

Kasun - look closely at your code. You are creating a figure each time you load a new image
figure(handles.figure1), imshow(omg);
Rather than doing this, I think it would be better to add an axes to your GUI (which is easy since you have used GUIDE) and show the image on that axes. If we assume that the handle to this object is named axes1, then your above code would be replaced with
imshow(img, 'Parent', handles.axes1);
Try the above and see what happens!
Related Question