- Matlab图像处理创新实践-实验1【图像滤波基础(1)】
- Matlab图像处理创新实践-实验2【图像滤波基础(2)】
- Matlab图像处理创新实践-实验3【图像锐化】
- Matlab图像处理创新实践-实验4【大实验——GUI综合设计】
- Matlab实验4——GUI程序操作说明
目 录
一、实验目的
二、实验内容
三、实验过程及结果
步骤1:启动Matlab,在命令行窗口中输入“guide”,新建GUI(创建空白GUI)。或者点击“新建”再点击“图形用户界面”,新建GUI。
步骤2:在左边空间栏中选择“Static Text”(静态文本),将控件放入面板中,并右键单击该控件,选择“Property Inspector”(属性检查器)。
步骤3:在弹出的属性对话框中,设置控件的属性值。
步骤4:添加静态文本控件后,再添加:可编辑文本(Edit Text)、轴(Axes)、按钮(Push Button)等控件,右键点开其属性框,修改他们的String属性。右键点击“打开”按钮,选择“查看回调”,点击“Callback”。
步骤5:由于是先创建的“打开”按钮,所以,“打开”按钮对应的控件名称编号是1;控件名称编号为1的下面编号为2的Callback对应的则是“处理”按钮。
步骤6:保存程序代码,单击菜单上的运行图形(Ctrl+T)按钮,实现效果图如下:
步骤7:在GUI图形界面上,添加更多的控件,显示多种滤波器的滤波效果。
步骤8:在GUI图形界面上,添加更多的控件,显示图像的灰度图、锐化效果图。
四、分析与思考
附录:代码汇总
掌握matlab环境下,图形用户界面(GUI)的设计方法;同时将学过的图像处理方法和理论进行综合运用和设计。
第一阶段:根据为你们提供好的GUI说明文档,按照要求一步步进行设计,并最终完成一个基本的具有图像处理功能的可视化程序。
第二阶段:在第一阶段工作基础上,根据自己能力将该程序进行适当的功能扩展。(比如,可以在用户界面上实现其他滤波的操作、或者实现可调节的锐化操作等等)。尽量多发挥创造性,保证程序的个性化和独特性,以防千篇一律导致分数受影响。
相关主要函数说明如下:
[filename pathname] = uigetfile({'*.jpg';'*.bmp';'*.png';'*.*'},'打开图片'); % 打开文件对话框
str = [pathname filename]; % 将两个字符串合并成一个字符串
axes(handles.axes1); % 打开axes1的句柄,对axes1进行操作
【在这里描述下你的实验操作过程和相应代码,以及你得到的最终结果,需图文并茂】
在“打开”按钮的callback函数下编写代码,完成对话框打开文件,并显示在axes1控件框内。然后在“处理”按钮的callback函数下编写代码,让其实现:将两个Edit Text文本框内的数字转换为高斯滤波器的参数,并对打开的图像进行高斯滤波并显示在axes2上。
利用double()将原图像类型转换为浮点型,然后按照“原图像 + 边缘图像”的模式,进行计算。利用uint8()或mat2gray()将得到的结果图像,进行类型转换,并将原图像和锐化后的图像一同显示在窗口中。
lotus.png
【在此描述下你从该实验中的总结内容,得出的结论,并对整个系统功能进行评述,可以查阅资料,最后总结下整个课程的收获。该部分占最终成绩的50%】
此次试验,独立完成,收获甚多。从0开发一个可以对图片进行滤波的GUI程序,令我对“图像处理创新实践”这门课有了更多的认识。一个GUI程序,主要由两个文件所构成(.figure:界面文件;.m:程序文件)。使用Matlab可以对图像进行很多的操作,如:旋转、裁剪、缩小及放大等基本功能,高斯滤波、平滑滤波、锐化滤波等滤波操作。
在GUI程序中,可以添加很多控件,分别实现不同的功能。在步骤8的程序中,设置了5个按钮,分别实现“打开图片”、“设置参数对图片进行滤波”、“显示多种滤波效果”、“灰度处理”、“图像锐化”等功能。在对图像进行处理的同时,给图像处理结果设置标题,可以令使用者对图像处理结果有更加直观的体验。在“设置参数对图片进行滤波”的功能实现中,使用者可以设置滤波器的类型、尺寸以及方差,可以看到多种滤波器的不同滤波效果。
关于此GUI程序的评述:此GUI程序较为稳定,内容较为丰富,实现的功能涉及到本门课程的4篇实验报告,耗时较长,设计合理。
课程收获总结:非常感谢老师的指导,我在本门课程中学到了很多知识,学会了“使用Matlab对图像进行多种滤波处理”“使用Matlab涉及GUI程序”等重点内容。但在课程的学习中,仍有很多不足,课下我会好好学习、多查资料、勤加练习。
评分标准:①是否按时出勤实验课,实验任务是否全部完成,占50%;② 实验代码及报告的格式是否规范,代码是否注释,是否美观,是否详尽,是否真实,占40%;③ 实验作业及报告是否按时提交,占10%。
报告文件的命名格式:完整学号
例如:541807010101
function varargout = experiment04(varargin)
% EXPERIMENT04 MATLAB code for experiment04.fig
% EXPERIMENT04, by itself, creates a new EXPERIMENT04 or raises the existing
% singleton*.
%
% H = EXPERIMENT04 returns the handle to a new EXPERIMENT04 or the handle to
% the existing singleton*.
%
% EXPERIMENT04('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EXPERIMENT04.M with the given input arguments.
%
% EXPERIMENT04('Property','Value',...) creates a new EXPERIMENT04 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before experiment04_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to experiment04_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 experiment04
% Last Modified by GUIDE v2.5 21-Nov-2020 20:06:19
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @experiment04_OpeningFcn, ...
'gui_OutputFcn', @experiment04_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 experiment04 is made visible.
function experiment04_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 experiment04 (see VARARGIN)
% Choose default command line output for experiment04
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes experiment04 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = experiment04_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 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 edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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
% --- 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)
axis off % 去除刻度
% uigetfile:常规打开文件列表函数【filename:文件名;pathname:路径;*.*:对应所有文件】
[filename pathname] = uigetfile({'*.jpg';'*.bmp';'*.png';'*.*'}, '打开图片'); % 函数返回值:文件名称、文件路径
str = [pathname filename]; % 合并字符串(合并文件名称与路径)
global im; % 全局变量,其它函数也可以使用
im = imread(str);
axes(handles.axes1); % 打开axes1(坐标轴名)的句柄,对axes1进行操作 设定图像显示到哪个组件上 axes:显示图像处理效果
str1 = ['图片位置:' str];
imshow(im);
title(str1);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im;
gsize = get(handles.edit1, 'String'); % 通过句柄找到对应的组件-尺寸
gsigma = get(handles.edit2, 'String'); % 通过句柄找到对应的组件-方差
gtype = get(handles.edit3, 'String'); % 通过句柄找到对应的组件-滤波器类型
f = fspecial(gtype, str2num(gsize), str2num(gsigma)); % str2num():字符串转数字
im_f = imfilter(im, f);
axes(handles.axes2);
imshow(im_f);
str = [gtype '滤波器;' '尺寸:' gsize ';方差:' gsigma]; % 合并字符串(合并文件名称与路径)
title(str);
% --- 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)
global im;
x = im; % 读取图片:将图像数据读取到Matlab环境中
smallx = imresize(x, 0.05);
imshow(smallx);
gx = rgb2gray(smallx);
f1 = fspecial('average', 5); % 1、均值滤波器
gx1 = filter2(f1, gx);
f2 = fspecial('gaussian'); % 2、高斯滤波器
gx2 = filter2(f2, gx);
f3 = fspecial('laplacian'); % 3、拉普拉斯滤波器
gx3 = filter2(f3, gx);
f4 = fspecial('sobel'); % 4、sobel滤波器
gx4 = filter2(f4, gx);
f5 = fspecial('prewitt'); % 5、prewitt滤波器
gx5 = filter2(f5, gx);
f6 = fspecial('disk'); % 6、disk滤波器
gx6 = filter2(f6, gx);
f7 = fspecial('log'); % 7、log滤波器
gx7 = filter2(f7, gx);
f8 = fspecial('motion'); % 8、motion滤波器
gx8 = filter2(f8, gx);
axes(handles.axes3);
imshow(smallx);
title('缩小至原来的0.05倍');
axes(handles.axes4);
imshow(uint8(gx1));
title('1、均值滤波器');
axes(handles.axes5);
imshow(uint8(gx2));
title('2、高斯滤波器');
axes(handles.axes6);
imshow(uint8(gx3));
title('3、拉普拉斯滤波器');
axes(handles.axes7);
imshow(uint8(gx4));
title('4、sobel滤波器');
axes(handles.axes8);
imshow(uint8(gx5));
title('5、prewitt滤波器');
axes(handles.axes9);
imshow(uint8(gx6));
title('6、disk滤波器');
axes(handles.axes10);
imshow(uint8(gx7));
title('7、log滤波器');
axes(handles.axes11);
imshow(uint8(gx8));
title('8、motion滤波器');
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im;
x = im; % 读取图片:将图像数据读取到Matlab环境中
gx = rgb2gray(x);
f1 = fspecial('average');
gx1 = filter2(f1, gx);
axes(handles.axes12);
imshow(uint8(gx1));
title('均值滤波器-灰度图像处理');
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im;
x = im; % 读取图片:将图像数据读取到Matlab环境中
gx = rgb2gray(x);
F = fspecial('average', 66); % F = fspecial('prewitt', Size);
y = filter2(F, gx);
xy = double(gx) - y; % 进行数据转换,保证数据类型一致;高频图、轮廓、边缘
sharpx = double(gx) + xy; % 原图+轮廓:清晰度更高
axes(handles.axes13);
imshow(uint8(sharpx));
title('average滤波器图像锐化');
当时写了很久...
自己电脑上的Matlab没办法连接摄像头,有点小遗憾...