GUI程序功能
读取存储图像、退出、重置、边缘检测、颜色亮度调整、添加噪声、空域滤波、频域滤波及其他等常用功能设置。
效果
GUI界面介绍
在MATLAB命令行中输入guide,回车,进入GUI快速入门窗口,如图所示。
Guide快速入门窗口
点击确定,即可打开如图所示的GUI界面。左边两列为基本的控件单元,分别有:按钮、滑动条、单选按钮、复选框、可编辑文本、静态文本、弹出式菜单、列表框、切换按钮、表、坐标区、面板、按钮组、ActiveX控件。网格上方的工具中,常用的按钮有:对齐对象、菜单编辑器、运行图窗。
选择需要的控件,摆放在界面中即可。
双击每个控件,会弹出该控件的检查器,里面有很多属性。每一个控件都有唯一的Tag标注用于区别,在创建好控件之后,对应的Tag值就会存入handles句柄的结构体中。Tag的值会与回调函数的函数名相关联。
实验所用控件
按钮
按钮pushbotton为最基本的控件,在左侧的属性窗口中可修改回调函数名字。在GUI界面中点击按钮之后,就会自动进入该按钮的回调函数,然后执行该回调函数中的内容。图示,在.m文件中查看定位该回调函数的方法,右键—查看回调—Callback。鼠标点击按钮,进入callback回调函数中。
查看按钮的回调函数
代码
function varargout = test(varargin)
% TEST MATLAB code for test.fig
% TEST, by itself, creates a new TEST or raises the existing
% singleton*.
%
% H = TEST returns the handle to a new TEST or the handle to
% the existing singleton*.
%
% TEST('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TEST.M with the given input arguments.
%
% TEST('Property','Value',...) creates a new TEST or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before test_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to test_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
% Last Modified by GUIDE v2.5 29-May-2022 08:24:02
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @test_OpeningFcn, ...
'gui_OutputFcn', @test_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 is made visible.
function test_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 (see VARARGIN)
% Choose default command line output for test
handles.output = hObject;
% set(handles.SaltPepper,'Enable','off');
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes test wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = test_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
%设置窗口标题
set( hObject, 'name', 'CHengYu_ImgHandle' ); % 其中的 hObject 替换成 gcf,效果依然成立
varargout{1} = handles.output;
% -----------------------按钮-----------------------------------------
% 图像文件读取
% --- Executes on button press in ImgRead.
function ImgRead_Callback(hObject, eventdata, handles)
global s;%定义全局变量,为了后面的还原保存数据
[filename,pathname,filterindex]=...
uigetfile({'*.*';'*.bmp';'*.tif';'*.png';'*.jpg';'*.jpeg'},'select picture');%选择图片格式
str=[pathname filename];%合成路径+文件名
s=str;
handles.i=imread(str);
handles.filebig=filterindex;
if filterindex==0
msgbox('选择图像失败','error');
return
else
im=imread(str);%读取图片
end
axes(handles.axes1);%使用第一个axes
imshow(im);%显示图片
handles.img=im;
guidata(hObject,handles);
%。。。。。。。。。。添加噪声
% 椒盐噪声
% --- Executes on button press in SaltPepper.
function SaltPepper_Callback(hObject, eventdata, handles)
global T;
axes(handles.axes1);
imshow(handles.img);
T=handles.img;
mysize=size(handles.img);
if numel(mysize)<3
msgbox('处理失败,请选择RGB图像','error');
return;
else
prompt={'输入椒盐噪声:'};
defans={'0.02'};
p=inputdlg(prompt,'input',1,defans);%prompt是提示语,input是对话框的标题,defans是默认值
p1=str2num(p{1});
f=imnoise(handles.img,'salt & pepper',p1);
end
axes(handles.axes2);
imshow(f);
handles.img=f;
guidata(hObject,handles);
% 高斯噪声
function gaussian_Callback(hObject, eventdata, handles)
handles.img = imnoise(handles.img,'gaussian');
axes(handles.axes2);
cla;
imshow(handles.img);
guidata(hObject,handles);
% 泊松噪声
function poisson_Callback(hObject, eventdata, handles)
handles.img = imnoise(handles.img,'poisson');
axes(handles.axes2); cla; imshow(handles.img);
guidata(hObject,handles);
% 乘性噪声
function speckle_Callback(hObject, eventdata, handles)
handles.img = imnoise(handles.img,'speckle',0.04);
axes(handles.axes2); cla; imshow(handles.img);
guidata(hObject,handles);
% 图像剪切
% --- Executes on button press in ImgShear.
function ImgShear_Callback(hObject, eventdata, handles)
global T;
T=handles.img;
if handles.filebig==0
msgbox('处理失败,请选择图像','error');
return;
else
axes(handles.axes1);
imshow(handles.img);
% I=imcomplement(handles.img);
I=imcrop(handles.img,[200 200 600 600]);
end
axes(handles.axes2);
imshow(I);
handles.img=I;
guidata(hObject,handles);
% 图像反色
% --- Executes on button press in ImgReverseColor.
function ImgReverseColor_Callback(hObject, eventdata, handles)
x=handles.img;
r=x(:,:,1); r=256-r;
g=x(:,:,2); g=256-g;
b=x(:,:,3); b=256-b;
handles.img=cat(3,r,g,b);
axes(handles.axes2);
cla;
imshow(handles.img);
guidata(hObject,handles);
% 二值化
function Binarization_Callback(hObject, eventdata, handles)
thresh = graythresh(handles.img); %自动确定二值化阈值
handles.img = im2bw(handles.img,thresh);
axes(handles.axes2);cla;imshow(handles.img);
guidata(hObject,handles);
% 图像存储
% --- Executes on button press in Imgwrite.
function Imgwrite_Callback(hObject, eventdata, handles)
% true false
if true
% 另存为
[filename,pathname]= uiputfile({'*.bmp';'*.tif';'*.png';'*.jpg';'*.jpeg'},'Save Image as');
save=[pathname,filename]; imwrite(handles.img,save);
msgbox("save success",'success');
return;
else
%覆盖原图
if handles.img==0
msgbox('没有可保存的图像','error');%弹窗提示
return;
else
[filename,pathname,filterindex]=uigetfile({'*.bmp';'*.tif';'*.png';'*.jpg';'*.jpeg'},'save picture');%存储图片路径
end
if filterindex==0
return;%如果取消操作,返回
else
str=[pathname,filename];%合成路径+文件名
axes(handles.axes2);%使用第二个axes
imwrite(handles.img,str);%写入图片信息,即保存图片
end
end
% 退出系统
% --- Executes on button press in exit.
function exit_Callback(hObject, eventdata, handles)
clc
clear
close(gcf)%关闭界面
% 重置
function Reset_Callback(hObject, eventdata, handles)
handles.img=handles.i;%原图数据重新放入handles.img中
axes(handles.axes2); cla; imshow(handles.img);
% cla(handles.axes2);
guidata(hObject,handles);
% -----------------------边缘检测-----------------------------------------
%Canny算子
function Canny_Callback(hObject, eventdata, handles)
mysize=size(handles.img);
if numel(mysize)>2
handles.img=rgb2gray(handles.img);
end
%edge,函数在检测到边缘的地方时为1其他的为0
handles.img=edge(handles.img,'canny');%canny算子边缘检测
axes(handles.axes2);
cla;% 清除当前坐标系
imshow(handles.img);
guidata(hObject,handles);
% Roberts算子
function Roberts_Callback(hObject, eventdata, handles)
mysize=size(handles.img);
if numel(mysize)>2
handles.img=rgb2gray(handles.img);
end
%edge,函数在检测到边缘的地方时为1其他的为0
handles.img=edge(handles.img,'roberts');%roberts算子边缘检测
axes(handles.axes2);
cla;% 清除当前坐标系
imshow(handles.img);
guidata(hObject,handles);
% Sobel算子
function Sobel_Callback(hObject, eventdata, handles)
mysize=size(handles.img);
if numel(mysize)>2
handles.img=rgb2gray(handles.img);
end
%edge,函数在检测到边缘的地方时为1其他的为0
handles.img=edge(handles.img,'sobel');%sobel算子边缘检测
axes(handles.axes2);
cla;% 清除当前坐标系
imshow(handles.img);
guidata(hObject,handles);
% Prewitt算子
function Prewitt_Callback(hObject, eventdata, handles)
mysize=size(handles.img);
if numel(mysize)>2
handles.img=rgb2gray(handles.img);%灰度处理
end
%edge,函数在检测到边缘的地方时为1其他的为0
handles.img=edge(handles.img,'prewitt');%Prewitt算子边缘检测
axes(handles.axes2);
cla;% 清除当前坐标系
imshow(handles.img);
guidata(hObject,handles);
% log算子
function Log_Callback(hObject, eventdata, handles)
mysize=size(handles.img);
if numel(mysize)>2
handles.img=rgb2gray(handles.img);%灰度处理
end
%edge,函数在检测到边缘的地方时为1其他的为0
handles.img=edge(handles.img,'log');%log算子边缘检测
axes(handles.axes2);
cla;% 清除当前坐标系
imshow(handles.img);
guidata(hObject,handles);
% -----------------------设置-----------------------------------------
% 左右翻转
function Flip_LeftRight_Callback(hObject, eventdata, handles)
handles.img=fliplr(handles.img);%左右翻转
axes(handles.axes2);
cla;
imshow(handles.img);
guidata(hObject,handles);
% 上下翻转
function Flip_UpDown_Callback(hObject, eventdata, handles)
handles.img=flipud(handles.img);%上下翻转
axes(handles.axes2);
cla;
imshow(handles.img);
guidata(hObject,handles);
% 灰度处理
function Gray_Callback(hObject, eventdata, handles)
% handles.img = rgb2gray(handles.img);
handles.img = im2gray(handles.img);
axes(handles.axes2);
cla;
imshow(handles.img);
guidata(hObject,handles);
% -----------------------菜单-----------------------------------------
% 滤波
function Filtering_Callback(hObject, eventdata, handles)
% hObject handle to Filtering (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 撤销
function Revoke_Callback(hObject, eventdata, handles)
% hObject handle to Revoke (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 还原
function Retore_Callback(hObject, eventdata, handles)
% hObject handle to Retore (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% -----------------------滑动条-----------------------------------------
% 图像旋转
function myRotate_Callback(hObject, eventdata, handles)
% hObject handle to myRotate (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
rrv=(get(hObject,'Value'));
handles.rot=handles.img;
handles.rot=imrotate(handles.rot,rrv);
axes(handles.axes2); cla; imshow(handles.rot);
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function myRotate_CreateFcn(hObject, eventdata, handles)
% hObject handle to myRotate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function myColorR_Callback(hObject, eventdata, handles)
% hObject handle to myColorR (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
x=get(hObject,'Value');
r=handles.img(:,:,1);
g=handles.img(:,:,2);
b=handles.img(:,:,3);
r1=r+x;
rcon=cat(3,r1,g,b);
axes(handles.axes2);
cla;
imshow(rcon)
handles.img=rcon;
% --- Executes during object creation, after setting all properties.
function myColorR_CreateFcn(hObject, eventdata, handles)
% hObject handle to myColorR (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function myColorG_Callback(hObject, eventdata, handles)
% hObject handle to myColorG (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
x=get(hObject,'Value');
r=handles.img(:,:,1);
g=handles.img(:,:,2); b=handles.img(:,:,3);
g1=g+x; gcon=cat(3,r,g1,b);
axes(handles.axes2); cla; imshow(gcon)
handles.img=gcon;
% --- Executes during object creation, after setting all properties.
function myColorG_CreateFcn(hObject, eventdata, handles)
% hObject handle to myColorG (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function myColorB_Callback(hObject, eventdata, handles)
% hObject handle to myColorB (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
x=get(hObject,'Value');
r=handles.img(:,:,1);
g=handles.img(:,:,2); b=handles.img(:,:,3);
b1=b+x; bcon=cat(3,r,g,b1);
axes(handles.axes2); cla; imshow(bcon)
handles.img=bcon;
% --- Executes during object creation, after setting all properties.
function myColorB_CreateFcn(hObject, eventdata, handles)
% hObject handle to myColorB (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% 亮度
% --- Executes on slider movement.
function myBrightness_Callback(hObject, eventdata, handles)
% hObject handle to myBrightness (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
x=get(hObject,'Value');
img=handles.img;
img=img+x;
axes(handles.axes2); cla; imshow(img)
handles.img=img;
% --- Executes during object creation, after setting all properties.
function myBrightness_CreateFcn(hObject, eventdata, handles)
% hObject handle to myBrightness (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% -----------------------空域滤波与频域滤波-----------------------------------------
% 理想低通滤波
% --- Executes on button press in F_LowPassFiltering.
function F_LowPassFiltering_Callback(hObject, eventdata, handles)
mysize=size(handles.img);
if numel(mysize)>2
handles.img = rgb2gray(handles.img);
end
I=handles.img;
I=im2double(I);
M=2*size(I,1);
N=2*size(I,2);
u=-M/2:(M/2-1);
v=-N/2:(N/2-1);
[U,V]=meshgrid(u,v);
D=sqrt(U.^2+V.^2);
D0=80;
H=double(D<=D0);
J=fftshift(fft2(I,size(H,1),size(H,2)));
K=J.*H;
L=ifft2(ifftshift(K));
L=L(1:size(I,1),1:size(I,2));
handles.img=L;
axes(handles.axes2); cla; imshow(handles.img);
guidata(hObject,handles);
% 巴特沃斯高通滤波
% --- Executes on button press in F_HighPassFiltering.
function F_HighPassFiltering_Callback(hObject, eventdata, handles)
mysize=size(handles.img);
if numel(mysize)>2
handles.img = rgb2gray(handles.img);
end
I=handles.img;
I=im2double(I);
M=2*size(I,1);%滤波器行数
N=2*size(I,2);%滤波器列数
u=-M/2:(M/2-1);
v=-N/2:(N/2-1);
[U,V]=meshgrid(u,v);
D=sqrt(U.^2+V.^2);
D0=30;%截止频率
n=6;%巴特沃斯滤波器阶数
H=1./(1+(D0./D).^(2*n));
J=fftshift(fft2(I,size(H,1),size(H,2)));
K=J.*H;
L=ifft2(ifftshift(K));
L=L(1:size(I,1),1:size(I,2));
handles.img=L;
axes(handles.axes2); cla; imshow(handles.img);
guidata(hObject,handles);
% 均值滤波
% --- Executes on button press in F_MeanFiltering.
function F_MeanFiltering_Callback(hObject, eventdata, handles)
h=fspecial('average');
handles.img=imfilter(handles.img,h,'replicate');
axes(handles.axes2); cla; imshow(handles.img)
guidata(hObject,handles);
% 高斯滤波
% --- Executes on button press in F_GaussianFiltering.
function F_GaussianFiltering_Callback(hObject, eventdata, handles)
hsize=[8 8]; sigma=1.7;
h=fspecial('gaussian',hsize,sigma);
handles.img=imfilter(handles.img,h,'replicate');
axes(handles.axes2); cla; imshow(handles.img);
guidata(hObject,handles);
% 中值滤波
% --- Executes on button press in F_MedianFiltering.
function F_MedianFiltering_Callback(hObject, eventdata, handles)
r=medfilt2(handles.img(:,:,1));
g=medfilt2(handles.img(:,:,2));
b=medfilt2(handles.img(:,:,3));
handles.img=cat(3,r,g,b);
axes(handles.axes2); cla; imshow(handles.img);
guidata(hObject,handles);
点击获取源码