我想在matlab中使用按钮和面板创建一个gui。
要求:单击按钮后,对话框必须打开才能选择文件,在从该对话框中选择图像后,图像必须显示在gui的该面板上。
我该怎么做?
如何在gui面板上显示图像?
我目前的状况:
function pushbutton1_Callback(hObject, eventdata, handles)
[filename, pathname] = ...
uigetfile({'*.jpg';'*.png';'*.jpeg';'*.bmp';'*.*'},'File Selector');
set(handles.axes2, 'Visible','on');
imshow(IMG,'Parent',TheAxisHandleToDrawOn)
if isequal(filename,0)
disp('Image upload Canceled')
end
我尝试了这个,但它没有用,怎么做?
答案 0 :(得分:0)
对于callback
,您首先read
使用imread
所选图片;您可以使用fullfile
来构建完整的文件名。
然后使用imread
返回的矩阵作为imshow
函数的输入。
在评论后编辑
要在uipanel
中显示图片,您必须在面板中添加axes
,并在调用parent
时将其用作imshow
。
如果要显示的图像不在当前文件夹中或MATLAB路径中的文件夹中,则必须指定完整路径名(参考imread)。
在下文中,您可以找到我为测试构建的GUI的.m
文件。
GUI包含:
axes
(tag:axes2)uipanel
(tag:uipanel1)axes
uipanel
(tag:axes_up)
radiobuttons
(tag:radiobutton1和radiobutton2)选择要显示图像的轴statictext
字段(标记:text2和text3)以显示所选图像的文件名pushbutton
(tag:pushbutton1)选择要显示的图像pushbutton
(代码:按钮3),以便在选择imcontrast图片的情况下运行.dcm工具该代码已在R2012b和2015b上进行了测试。
function varargout = disp_fig(varargin)
% DISP_FIG MATLAB code for disp_fig.fig
% DISP_FIG, by itself, creates a new DISP_FIG or raises the existing
% singleton*.
%
% H = DISP_FIG returns the handle to a new DISP_FIG or the handle to
% the existing singleton*.
%
% DISP_FIG('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DISP_FIG.M with the given input arguments.
%
% DISP_FIG('Property','Value',...) creates a new DISP_FIG or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before disp_fig_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to disp_fig_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 disp_fig
% Last Modified by GUIDE v2.5 25-Apr-2016 14:18:18
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @disp_fig_OpeningFcn, ...
'gui_OutputFcn', @disp_fig_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 disp_fig is made visible.
function disp_fig_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 disp_fig (see VARARGIN)
% Choose default command line output for disp_fig
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes disp_fig wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = disp_fig_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)
% Select the image to be displayed
[filename, pathname] = ...
uigetfile({'*.jpg';'*.png';'*.jpeg';'*.bmp';'*.*'},'File Selector');
% Build the full filename
img_file=fullfile(pathname,filename)
% Split the filename to get its extension
[the_path,the_name,the_ext]=fileparts(img_file);
% Set the current StaticText uicontrol based on the selected RadioButton
curr_txt=handles.text2;
if(get(handles.radiobutton2,'value'))
curr_txt=handles.text3;
end
% Check for image valid selection
if isequal(filename,0)
disp('Image upload Canceled')
set(curr_txt,'string','Image upload Canceled')
else
% If an image has been selected
set(curr_txt,'string',img_file)
% If the image is a ".dcm"
if(strcmp(the_ext,'.dcm'))
% Read it with dicomread and enable the pushbutton to run the
% IMCONTRAST tool
IMG=dicomread(img_file);
set(handles.pushbutton3,'visible','on')
else
% If the image is a ".jpg", ".bmp", ...
% Read it with imread and disable the pushbutton to run the
% IMCONTRAST tool
IMG=imread(img_file);
set(handles.pushbutton3,'visible','off')
end
% Identify the axes on which to display the image according to the
% selected radiobutton
if(get(handles.radiobutton1,'value'))
the_parent=handles.axes2;
set(handles.axes2, 'Visible','on');
else
the_parent=handles.axes_up;
end
% Showthe image
imshow(IMG,'Parent',the_parent)
end
% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
% If the first radiobutton has been selected, disable the second onre
r1=get(handles.radiobutton1,'value');
if(r1)
set(handles.radiobutton2,'value',0);
end
% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton2
% If the second radiobutton has been selected, disable the first one
r2=get(handles.radiobutton2,'value');
if(r2)
set(handles.radiobutton1,'value',0);
end
% --- 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)
% Run the IMCONTRAST tool to the proper axes according to the selected
% radiobutton
r1=get(handles.radiobutton1,'value');
if(r1)
imcontrast(handles.axes2)
else
imcontrast(handles.axes_up)
end
GUI的结构
GUI ...正常工作
测试.dcm
图片显示我作为示例使用了来自DICOM sample image sets的图片。
希望这有帮助。
Qapla'
答案 1 :(得分:0)
this is the final solution for the problem
function browse_Callback(hObject,eventdata,handles) %hObject要浏览的句柄(参见GCBO) 保留%eventdata - 将在MATLAB的未来版本中定义 %处理带句柄和用户数据的结构(请参阅GUIDATA) [filename,pathname] = uigetfile({' .jpg';' .gif';' .png';& #39; .bmp'},'选择文件'); MyImage = strcat(pathname,filename); %此代码检查用户是否在对话框上按下取消。 if isequal(filename,0)|| ISEQUAL(路径名,0) uiwait(msgbox('用户按下取消','失败','模态')) 坚持,稍等; 其他 uiwait(msgbox('用户选择的图像成功','成功','模态')); 推迟; imshow(MYIMAGE,'父',handles.axes1); 结束 全局图像变量; Imagevariable = MYIMAGE; handles.output = hObject; guidata(hObject,handles);