Matlab学习13-图像处理之可视化GUI程序
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});
endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
elsegui_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==0msgbox('选择图像失败','error');return
elseim=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)<3msgbox('处理失败,请选择RGB图像','error');return;
elseprompt={'输入椒盐噪声:'};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==0msgbox('处理失败,请选择图像','error');return;
elseaxes(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==0msgbox('没有可保存的图像','error');%弹窗提示return;else[filename,pathname,filterindex]=uigetfile({'*.bmp';'*.tif';'*.png';'*.jpg';'*.jpeg'},'save picture');%存储图片路径endif filterindex==0return;%如果取消操作,返回elsestr=[pathname,filename];%合成路径+文件名axes(handles.axes2);%使用第二个axesimwrite(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)>2handles.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)>2handles.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)>2handles.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)>2handles.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)>2handles.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)>2handles.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)>2handles.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);
点击获取源码
Matlab学习13-图像处理之可视化GUI程序相关推荐
- Matlab学习------------带有右键菜单的GUI学习实例
实例步骤: 须要设置UIContextMenu,否则点击右键不显示. 右键点击第一个菜单之后:(在菜单中加入对应的回调函数) function r1_Callback(hObject, eventda ...
- MATLAB实现大家来找茬GUI程序
一. 实验目的 编程实现大家来找茬GUI设计 二. 实验内容与要求 实现大家来找茬的界面设置: 对GUI界面进行优化: 实现大家来找茬的基本功能: 实现找茬结果的输出: 三.设计与实现: 设计思路: ...
- Matlab学习笔记(3)—GUI程序设计与图像处理基本操作
Matlab学习笔记(3)-GUI程序设计与图像处理基本操作 一.GUI程序设计 如果想要开始使用Matlab进行GUI编程,首先需要进入Matlab的GUI界面对GUI有一个基本的认识. 1.gui ...
- pythongui程序,python第一个GUI程序
第一个GUI程序 截止目前,我们的python基本语法就已经讲完了,但是python的应用确实无比之广,不同的应用领域需要学习不同的Python库,比如爬虫的urllib模块,科学计算numpy模块, ...
- Caffe学习系列(13):数据可视化环境(python接口)配置
原文有更新: Caffe学习系列(13):数据可视化环境(python接口)配置 - denny402 - 博客园 http://www.cnblogs.com/denny402/p/5088399. ...
- Matlab——GUI程序操作说明
Matlab图像处理创新实践-实验1[图像滤波基础(1)] Matlab图像处理创新实践-实验2[图像滤波基础(2)] Matlab图像处理创新实践-实验3[图像锐化] Matlab图像处理创新实践- ...
- 文本框赋值guide matlab,科学网-Matlab: 学习GUI (使用GUIDE时需注意的几个问题)-刘磊的博文...
在博文<Matlab:学习GUI(一个简单的例子)>(介绍的方法是完全用代码来建立一个GUI,实际上Matlab本身有一个设计GUI的交互系统--GUIDE,用户可以使用该系统更方便的建立 ...
- matlab怎样编程形成软件_Matlab编程笔记之GUI程序转exe
Matlab同样可以制作出和VC++技术一样的基于对话框的GUI界面. 在Matlab软件中编写GUI程序的方法是:命令行中输入guide,即可进入. Matlab是强大的数学建模软件,我们可以通过M ...
- 数字图像处理MATLAB学习笔记(五)
数字图像处理MATLAB学习笔记(五) Color Image Processing 1 Color Image Representation in MATLAB 这里不多说了,彩色图片在计算机中以R ...
最新文章
- 也许是史上最不良心的低阶计算几何讲解和习题集??
- Linux 软件的安装
- 七.Hystrix Timeout机制
- 自然语言处理笔记4-哈工大 关毅
- virtualenv模块使用
- MyBatis源码之:MapperMethod
- jquery ajax 访问 mysql_使用ajax+jquery+php访问mysql数据库,并且达到不跳转页面的效果。。。...
- KVM(二)CPU 和内存虚拟化
- 游戏筑基开发之广度优先搜索算法(C语言)
- Java 杨辉三角的简单实现
- 美区苹果id被禁用原因和解除限制方法
- 词云python_词牌名大全
- [四年前写的诗]夜有流星兩
- linux suse11 sp3安装,SUSE Linux Enterprise Server 11 SP3安装教程详解
- PMBOK6相关方:权利利益方格
- 7.5ElGamal算法
- cin.tie() 输入加速器
- 全国大学生大数据技能竞赛(Hadoop集群搭建)
- Mathorcup数学建模竞赛第六届-【妈妈杯】A题:淡水养殖池塘水华发生及池水自净化研究(附一等奖获奖论文、matlab和SAS代码)
- 卡尔曼滤波之目标跟踪