前言
那本蔡利梅的《matlab图像处理 理论、算法与实例分析》讲GUI部分偏重于实例。但对于我这种0基础的人来说,需要知道更弱智的东西,比如怎么运行.fig文件,怎么把.fig文件和.m文件关联起来,生成的.m文件自带的那么多代码是啥意思等等等
推荐这一本书—作者讲的非常详细!
1. GUIDE布局
刚接触这个GUIDE布局,我写了代码不知道怎么使用如下这个界面,保存了这个界面好像也没有任何变化。在b站看了好一会视频才知道。。。点那个绿的三角形键,就能显示出熟悉的图窗!
2. 自定义菜单设计
3. 属性设置
给菜单的每个选项设置了tag标记,保存GUIDE布局后会在M文件自动生成格式为‘tag值_Callback’样式的函数
4. 实现代码
function varargout = spImage(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @spImage_OpeningFcn, ...
'gui_OutputFcn', @spImage_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 spImage is made visible.
function spImage_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 spImage (see VARARGIN)
% Choose default command line output for spImage
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes spImage wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = spImage_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)
global imageOriginal;
fmt = {'*.jpg', 'JPEG image(*.jpg)'; '*.png', 'PNG image(.*png)'; ...
'*.bmp', 'BMP image(.*bmp)'; '*.gif', 'GIF image(.*gif)';}; %自定义可打开得的图像的文件类型
[filename, filepath] = uigetfile(fmt, '打开图像', '*.jpg'); %这是使用uigetfile的固定格式,打开的图片显示默认文件名*.jpg
if ~isequal([filename, filepath], [0,0])
filefullname = strcat(filepath, filename);
else
return;
end
imageOriginal = imread(filefullname);
axes(handles.axes1);
imshow(imageOriginal );
axis off;
% 保存图像
function saveImage_Callback(hObject, eventdata, handles)
global imageOriginal;
fmt = {'*.jpg', 'JPEG image(*.jpg)'; '*.png', 'PNG image(.*png)'; ...
'*.bmp', 'BMP image(.*bmp)'; '*.gif', 'GIF image(.*gif)';}; %自定义可保存得的图像的文件类型
[filename, filepath] = uiputfile(fmt, '保存图像'); %设置文件保存对话框,标题为‘保存图像’,并返回保存的文件名和文件路径
fullfilename = strcat(filepath, filename);
imwrite(imageOriginal, fullfilename);
% 退出图窗
function quit_Callback(hObject, eventdata, handles)
close(gcf);
5. 效果
-
初始界面
-
打开图像功能
红线部分均为自己设计的
- 保存图像功能