实验六图形用户界面和图像处理实验 一 实验目的 1. 掌握图形用户界面 (GUI) 的设计 ; 2. 掌握数字图像的读取与显示方法 ; 3. 了解图像灰度变换与直方图方法 ;; 4. 了解图像的增强和滤波方法 ; 5. 了解图像的空间变换方法 ; 6. 了解图像边缘检测与分割方法 二 实验内容 1. 创建一个简单的 GUI 程序, 在图形用户界面上显示如下图界面, 并通过按钮控制显示正弦曲线或余弦曲线 以及关闭 在函数内需添加的程序代码如下 : (1) 在 function sinx_callback(hobject, eventdata, handles) 内添加以下代码 : x = 0:pi/10:2*pi; y = sin(x); plot(x,y); grid on; (2) 在 function cosx_callback(hobject, eventdata, handles) 内添加以下代码 : x = 0:pi/10:2*pi; y = cos(x); plot(x,y,'r'); grid on; (3) 在 function close_callback(hobject, eventdata, handles) 内添加以下代码 : close; 2. 设计一个如下图所示的图形用户界面, 实现包含菜单 按钮 下拉框 坐标轴等控件的图形用户界面 控件属性 : 由上至下 3 个按钮的属性 (String,Tag) 值分别为 (Surf, surf_pushbutton ) ( Mesh, mesh_pushbutton) (Contour, contour _pushbutton), 静态文本的属性 (String,Tag) 值 (Select Data,popup_label), 下拉菜单的属性 Tag 值为 plot_popup, 属性 String 值为 Peaks 1
Membrane sinc 编写回调程序代码为 : 1) 图形用户界面打开时自动运行的函数 simple_gui_openingfcn(), 其具体代码如下 : function simple_gui_openingfcn(hobject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hobject handle to figure % varargin command line arguments to simple_gui (see VARARGIN) %Create the data to plot handles.peaks = peaks(35); handles.membrane = membrane; [x,y] = meshgrid(-8:.5:8); r = sqrt(x.^2+y.^2)+eps; z = sin(r)./r; handles.sinc = z; handles.current_data = handles.peaks; surf(handles.current_data) % Choose default command line output for simple_gui handles.output = hobject; % Update handles structure guidata(hobject, handles); %------------------------------------------------------------ % Call the popup menu callback to set the handles.current_data % field to the current value of the popup plot_popup_callback(handles.plot_popup,[],handles) 2
2) 三个按钮的回调函数 (Callback) 代码如下 : function surf_pushbutton_callback(hobject, eventdata, handles) % hobject handle to surf_pushbutton (see GCBO) z=handles.current_data; surf(z); % --- Executes on button press in mesh_pushbutton. function mesh_pushbutton_callback(hobject, eventdata, handles) % hobject handle to mesh_pushbutton (see GCBO) z=handles.current_data; mesh(z); % --- Executes on button press in contour_pushbutton. function contour_pushbutton_callback(hobject, eventdata, handles) % hobject handle to contour_pushbutton (see GCBO) z=handles.current_data; contour(z); 3) 下拉菜单的回调函数 (Callback) 的代码如下 : function plot_popup_callback(hobject, eventdata, handles) % hobject handle to plot_popup (see GCBO) % Hints: contents = cellstr(get(hobject,'string')) returns plot_popup contents as cell array % contents{get(hobject,'value')} returns selected item from plot_popup val = get(hobject,'value'); str = get(hobject,'string'); switch str{val} case 'peaks' handles.current_data = handles.peaks; case 'membrane' handles.current_data = handles.membrane; case 'sinc' handles.current_data = handles.sinc; end guidata(hobject,handles) 4) 菜单项 Close 的回调函数 (Callback) 的代码如下 : 3
function Mclose_Callback(hObject, eventdata, handles) % hobject handle to Mclose (see GCBO) close; 最后显示结果为 : 3. 数字图像的读取与显示 >> I=imread('football.jpg'); >>imfinfo football.jpg % 本命令显示了哪些信息? >>imshow(i) >>imview(i) % 思考 imshow 和 imview 命令的不同 4. 将 football.jpg 灰度级 0.1~0.6 范围的像素线性变换到 0~1 上, 查看显示效果 p = imread('football.jpg'); g1 = imadjust(p,[0.1 0.6],[]); subplot(2,2,1); imshow(p); xlabel('(a)'); 4
subplot(2,2,2); imshow(g1); xlabel('(b)'); % 将图像 p 向高灰度级变换,gamma 取 0.6 g2 = imadjust(p,[],[],0.6); subplot(2,2,3); imshow(g2); xlabel('(c)'); % 将图像 p 做灰度倒相变换, 即输出源图的负片,gamma 取 1 g3 = imadjust(p,[0 1],[1 0]); subplot(2,2,4); imshow(g3); xlabel('(d)'); 5. 对 tire.tif 进行直方图均衡 p = imread('tire.tif'); subplot(2,2,1); imshow(p); xlabel('(a) '); subplot(2,2,2); imhist(p); xlabel('(b) '); g = histeq(p); subplot(2,2,3); imshow(g); xlabel('(c) '); subplot(2,2,4); imhist(g); xlabel('(d) '); 6. 对添加了椒盐噪声的图像分别进行均值滤波和中值滤波 p = imread('coins.png'); p0 = imnoise(p, 'salt & pepper',0.02); % 为原图添加椒盐噪声 H1 = fspecial('average',[3 4]); % 设计均值滤波器 H1 p1 = imfilter(p0,h1,'replicate'); % 用滤波器 H1 对图像 p0 进行滤波 p2 = medfilt2(p0,[3,3],'zeros'); % 对图像 p0 进行中值滤波 subplot(2,2,1),imshow(p); % 分别显示原图及处理后的图像 subplot(2,2,2),imshow(p0); subplot(2,2,3);imshow(p1); subplot(2,2,4);imshow(p2); 7. 对含有噪声的原图像进行锐化 p0 = imread('moon.tif'); p = im2double(p0); % 将图像数据类型转换为 double 5
H1 = fspecial('laplacian',0); H2 = fspecial('log',[5 5],0.5); hp1 = imfilter(p,h1,'replicate'); hp2 = imfilter(p,h2,'replicate'); p1 = p - hp1; p2 = p - hp2; subplot(1,3,1),imshow(p0); subplot(1,3,2),imshow(p1); subplot(1,3,3);imshow(p2); % 设计拉普拉斯滤波器 H1 % 设计高斯 - 拉普拉斯滤波器 H2 % 用滤波器 H1 对图像 p 进行滤波 % 用滤波器 H2 对图像 p 进行滤波 % 还原灰度色调 % 还原灰度色调 8. 图像的空间几何变换 % 对 liftingbody.png 图像进行缩小 p = imread('liftingbody.png'); w = input('please input a number:'); % 交互输入缩小为 0.2 倍 ps = imresize(p,w); % 缩小变换 subplot(2,2,1); imshow(p); subplot(2,2,2); imshow(ps); % 对 liftingbody.png 图像进行剪切 p = imread('liftingbody.png'); pc = imcrop(p,[80 180 260 220]); % 输入矩形尺度进行选取 subplot(2,2,3); imshow(pc); % 对 liftingbody.png 图像进行旋转 p = imread('liftingbody.png'); angle = input('please input angle:'); % 输入旋转角度为 45 度 pr = imrotate(p,angle,'bilinear'); % 使用双线性插值方法实现旋转变换 subplot(2,2,4); imshow(pr); 9. 对 lena.bmp 图像使用不同的算子进行边缘检测 p=imread('lena.bmp'); e1 = edge(p,'sobel',0.03,'both'); % sobel 检测器 e2 = edge(p,'roberts',0.03,'both'); % roberts 检测器 e3 = edge(p,'prewitt',0.04,'both'); % prewitt 检测器 e4 = edge(p,'log',0.003,2.10); % LoG 检测器 e5 = edge(p,'canny',[0.05 0.12],1.6); % canny 检测器 subplot(2,3,1),imshow(p); subplot(2,3,2),imshow(e1); subplot(2,3,3),imshow(e2); subplot(2,3,4);imshow(e3); subplot(2,3,5),imshow(e4); subplot(2,3,6),imshow(e5); 6