matlab GUI为用户图形化界面,可以使用guide命令进入图形化编辑界面
回车弹出GUIDE 快速入门,可以选择所需要的模板进行编辑,默认为Blank GUI
点击确定 进入图像化编辑界面 可在此界面进行编辑创作
最常用到的是按钮和坐标区域(按钮用作函数的绑定等等;坐标区域用作展示图像等等)
双击控件可进入组件的属性编辑,可根据需求进行创作编辑
编辑图形化界面
点击菜单编辑器可对菜单进行编辑 新建菜单 新建菜单项 新建上下文菜单 并对菜单进行 回调函数绑定 设置快捷键 设置标签
编辑界面完成后双击图形化文件 .fig 查看图形界面内查看并在代码文件 .m内编辑回调函数(回调函数内可调用其他函数完成功能的实现)
MainForm.m 代码示例:
function varargout = MainForm(varargin) % 主函数图形化界面编辑后自动生成,且不能修改,修改导致初始化失败
% MAINFORM MATLAB code for MainForm.fig
% MAINFORM, by itself, creates a new MAINFORM or raises the existing
% singleton*.
%
% H = MAINFORM returns the handle to a new MAINFORM or the handle to
% the existing singleton*.
%
% MAINFORM('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MAINFORM.M with the given input arguments.
%
% MAINFORM('Property','Value',...) creates a new MAINFORM or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before MainForm_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to MainForm_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 MainForm
% Last Modified by GUIDE v2.5 29-Dec-2013 17:16:36
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @MainForm_OpeningFcn, ...
'gui_OutputFcn', @MainForm_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 MainForm is made visible.
function MainForm_OpeningFcn(hObject, eventdata, handles, varargin) % 刚打开GUI界面时,执行的初始化操作。handles参数是一个结构体类型,可以通过guidata(hObject, handles);进行更新。
% 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 MainForm (see VARARGIN)
% Choose default command line output for MainForm
handles.output = hObject;
InitFig(hObject, handles); % 调用初始化函数
handles.Img1 = 0;
handles.Img2 = 0;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes MainForm wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = MainForm_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 FileOpen_Callback(hObject, eventdata, handles) % 打开文件回调函数
% hObject handle to FileOpen (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 打开
warning off all;
% 载入图像
[FileName,PathName,FilterIndex] = uigetfile({'*.jpg;*.tif;*.png;*.gif', ... % 加载图像文件
'所有图像文件';...
'*.*','所有文件' },'载入图像',...
'.\images\\sweden_input.jpg');
if isequal(FileName, 0) || isequal(PathName, 0)
return;
end
InitFig(hObject, handles);
Img1 = imread(fullfile(PathName, FileName));
axes(handles.axes1);
imshow(Img1, []);
handles.Img1 = Img1;
handles.Img2 = 0;
guidata(hObject, handles);
% --------------------------------------------------------------------
function Exist_Callback(hObject, eventdata, handles) % 关闭系统 点击菜单栏退出按钮 执行函数退出系统
% hObject handle to Exist (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close();
% --------------------------------------------------------------------
function About_Callback(hObject, eventdata, handles) % 关于系统 点击后显示弹窗
% hObject handle to About (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
msgbox('图像去雾系统,全局及局部直方图均衡化和基于RETINEX理论去雾算法', '提示信息');
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)
% 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
% --------------------------------------------------------------------
function ImageDeFog_Callback(hObject, eventdata, handles) % 菜单项 图像去雾
% hObject handle to ImageDeFog (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function ColorSpaceDeFog_Callback(hObject, eventdata, handles) %
% hObject handle to ColorSpaceDeFog (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function RetinexDeFog_Callback(hObject, eventdata, handles) % retinex 图像增强算法
% hObject handle to RetinexDeFog (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isequal(handles.Img1, 0) % 如果空间中没有加载原图点击算法后弹窗提示未载入图像
msgbox('请载入图像!', '提示信息');
return;
end
Img2 = RemoveFogByRetinex(handles.Img1, 0); % 图像加载后调用retinex 算法处理图像
axes(handles.axes2); imshow(Img2, []); % 将处理后的图像输出至第二个坐标控件
handles.Img2 = Img2;
guidata(hObject, handles);
set(handles.textInfo, 'String', ... % 并将底部算法说明中的文字进行改变
'基于RETINEX理论去雾算法。');
% --------------------------------------------------------------------
function help_Callback(hObject, eventdata, handles) % 帮助菜单
% hObject handle to help (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function Flow_Callback(hObject, eventdata, handles) % 算法流程菜单
% hObject handle to Flow (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
str = '图像去雾系统,首先载入图像并显示,然后选择去雾算法,最后可以观察直方图对比效果。';
msgbox(str, '提示信息');
% --------------------------------------------------------------------
function SaveGUI_Callback(hObject, eventdata, handles) % 菜单截图回调函数
% hObject handle to SaveGUI (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 截图
SnapImage();
% --------------------------------------------------------------------
function SaveResult_Callback(hObject, eventdata, handles) % 保存结果
% hObject handle to SaveResult (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 保存
if isequal(handles.Img2, 0)
msgbox('请进行去雾处理!', '提示信息');
return;
end
SaveImage(handles.Img2);
% --------------------------------------------------------------------
function HistImg_Callback(hObject, eventdata, handles) % 直方图对比
% hObject handle to HistImg (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 直方图对比
if isequal(handles.Img1, 0) % 首先检查是否载入图像
msgbox('请载入图像!', '提示信息');
return;
end
if isequal(handles.Img2, 0) % 然后检查是否进行去雾处理
msgbox('请进行去雾处理!', '提示信息');
return;
end
figure('Name', '直方图对比', 'NumberTitle', 'Off', ... % 前两步都做了以后则创建一个新的页面展示原图与处理后图像及两图像的直方图
'Units', 'Normalized', 'Position', [0.1 0.1 0.5 0.5]);
subplot(2, 2, 1); imshow(handles.Img1); title('原图像', 'FontWeight', 'Bold');
subplot(2, 2, 2); imshow(handles.Img2); title('处理后的图像', 'FontWeight', 'Bold');
Q = rgb2gray(handles.Img1);
W = rgb2gray(handles.Img2);
subplot(2, 2, 3); imhist(Q, 64); title('原灰度直方图', 'FontWeight', 'Bold');
subplot(2, 2, 4); imhist(W, 64); title('处理后的灰度直方图', 'FontWeight', 'Bold');
% --------------------------------------------------------------------
function GlobalHisteq_Callback(hObject, eventdata, handles) % 全局直方图均衡化处理算法
% hObject handle to GlobalHisteq (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isequal(handles.Img1, 0)
msgbox('请载入图像!', '提示信息');
return;
end
Img2 = RemoveFogByGlobalHisteq(handles.Img1, 0);
axes(handles.axes2); imshow(Img2, []);
handles.Img2 = Img2;
guidata(hObject, handles);
set(handles.textInfo, 'String', ...
'全局直方图均衡化实现图像去雾算法。');
% --------------------------------------------------------------------
function LocalHisteq_Callback(hObject, eventdata, handles) % 局部直方图均衡化处理算法
% hObject handle to LocalHisteq (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isequal(handles.Img1, 0)
msgbox('请载入图像!', '提示信息');
return;
end
Img2 = RemoveFogByLocalHisteq(handles.Img1, 0); % 调用函数
axes(handles.axes2); imshow(Img2, []);
handles.Img2 = Img2;
guidata(hObject, handles);
set(handles.textInfo, 'String', ...
'局部直方图均衡化实现图像去雾算法。');
本程序提供三种去雾图像增强方法,以Retinex算法为例:
思想:从原始图像中去除照明的影响来还原图像的本质特征,照明由高斯滤波与原图卷积得到。
function In = RemoveFogByRetinex(f, flag) % 从原始图像中去除照明的影响来还原图像的本质特征,照明由高斯滤波与原图卷积得到。
% Retinex实现图像去雾
% 输入参数:
% f——图像矩阵
% flag——显示标记
% 输出参数:
% In——结果图像
if nargin < 2 % 有输入图象时 flag置1
flag = 1;
end
%提取图像的R、G、B分量
fr = f(:, :, 1);
fg = f(:, :, 2);
fb = f(:, :, 3);
%数据类型归一化
mr = mat2gray(im2double(fr));
mg = mat2gray(im2double(fg));
mb = mat2gray(im2double(fb));
%定义alpha参数
alpha = randi([80 100], 1)*20;
%定义模板大小
n = floor(min([size(f, 1) size(f, 2)])*0.5);
%计算中心
n1 = floor((n+1)/2);
for i = 1:n
for j = 1:n
%高斯函数
b(i,j) = exp(-((i-n1)^2+(j-n1)^2)/(4*alpha))/(pi*alpha);
end
end
%卷积滤波
nr1 = imfilter(mr,b,'conv', 'replicate');
ng1 = imfilter(mg,b,'conv', 'replicate');
nb1 = imfilter(mb,b,'conv', 'replicate');
ur1 = log(nr1);
ug1 = log(ng1);
ub1 = log(nb1);
tr1 = log(mr);
tg1 = log(mg);
tb1 = log(mb);
yr1 = (tr1-ur1)/3;
yg1 = (tg1-ug1)/3;
yb1 = (tb1-ub1)/3;
%定义beta参数
beta = randi([80 100], 1)*1;
%定义模板大小
x = 32;
for i = 1:n
for j = 1:n
%高斯函数
a(i,j) = exp(-((i-n1)^2+(j-n1)^2)/(4*beta))/(6*pi*beta);
end
end
%卷积滤波
nr2 = imfilter(mr,a,'conv', 'replicate');
ng2 = imfilter(mg,a,'conv', 'replicate');
nb2 = imfilter(mb,a,'conv', 'replicate');
ur2 = log(nr2);
ug2 = log(ng2);
ub2 = log(nb2);
tr2 = log(mr);
tg2 = log(mg);
tb2 = log(mb);
yr2 = (tr2-ur2)/3;
yg2 = (tg2-ug2)/3;
yb2 = (tb2-ub2)/3;
%定义eta参数
eta = randi([80 100], 1)*200;
for i = 1:n
for j = 1:n
%高斯函数
e(i,j) = exp(-((i-n1)^2+(j-n1)^2)/(4*eta))/(4*pi*eta);
end
end
%卷积滤波
nr3 = imfilter(mr,e,'conv', 'replicate');
ng3 = imfilter(mg,e,'conv', 'replicate');
nb3 = imfilter(mb,e,'conv', 'replicate');
ur3 = log(nr3);
ug3 = log(ng3);
ub3 = log(nb3);
tr3 = log(mr);
tg3 = log(mg);
tb3 = log(mb);
yr3 = (tr3-ur3)/3;
yg3 = (tg3-ug3)/3;
yb3 = (tb3-ub3)/3;
dr = yr1+yr2+yr3;
dg = yg1+yg2+yg3;
db = yb1+yb2+yb3;
cr = im2uint8(dr);
cg = im2uint8(dg);
cb = im2uint8(db);
% 集成处理后的分量得到结果图像
In = cat(3, cr, cg, cb);
%结果显示
if flag
figure;
subplot(2, 2, 1); imshow(f); title('原图像', 'FontWeight', 'Bold');
subplot(2, 2, 2); imshow(In); title('处理后的图像', 'FontWeight', 'Bold');
% 灰度化,用于计算直方图
Q = rgb2gray(f);
M = rgb2gray(In);
subplot(2, 2, 3); imhist(Q, 64); title('原灰度直方图', 'FontWeight', 'Bold');
subplot(2, 2, 4); imhist(M, 64); title('处理后的灰度直方图', 'FontWeight', 'Bold');
end
需要所有源码可私信