基于MATLAB语言的数字图像处理实验(GUI)_第1页
基于MATLAB语言的数字图像处理实验(GUI)_第2页
基于MATLAB语言的数字图像处理实验(GUI)_第3页
基于MATLAB语言的数字图像处理实验(GUI)_第4页
基于MATLAB语言的数字图像处理实验(GUI)_第5页
已阅读5页,还剩41页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、数字图像处理实验报告基于MATLAB语言的图像处理软件姓 名 : 班 级 : 学 号 : 专 业 : 目录1.设计目的22.设计要求23.总体设计24.模块设计34.1图像的读取、保存和程序退出34.2图像转化为灰度图像54.3底片处理(反色)64.4截图64.5亮度和对比度度调节64.6图像的翻转与旋转74.7添加噪声94.8平滑和锐化104.9直方图均衡化处理114.10图像的腐蚀和膨胀124.11边缘检测134.12还原和撤销165.结果分析175.1转为灰度图像175.2底片处理175.3截图175.4亮度和对比度调节185.5图像翻转与旋转195.6添加噪声、平滑和锐化205.7直方

2、图均衡化235.8腐蚀和膨胀245.9边缘检测256.心得体会267.附录代码271. 设计目的利用MATLAB的GUI程序设计一个简单实用的图像处理程序。该程序应具备图像处理的常用功能,以满足要求。2. 设计要求设计程序有以下基本功能:1) 图像的读取和保存2) 图像转化为灰度图像3) 底片处理(反色)4) 截图5) 亮度和对比度度调节6) 图像的翻转与旋转7) 添加噪声8) 平滑和锐化9) 直方图均衡化处理10) 图像的腐蚀和膨胀11) 边缘检测3. 总体设计软件的总体设计界面布局如上图所示,主要分为2个区域:显示区域与操作区域。显示区域:显示原图像,以及效果图,即处理前与处理后的图像。操

3、作区域:通过功能键实现对图像的各种处理。在图中可见,界面左边和下方为一系列功能按键如“转为灰度图像”、“撤销”、“还原”等等 ;界面正中部分为图片显示部分。设计完成后运行的软件界面如下:4. 模块设计以下介绍各个功能模块的功能与实现4.1 图像的读取、保存和程序退出通过Menu Editor 创建如下菜单,通过以下菜单来实现“载入图像”、“保存图像”、“退出”的功能。4.1.1 载入图像利用MATLAB中 “uigetfile”、“imread”、“imshow”实现图像文件的读取与显示。function Input_Callback(hObject, eventdata, handles)f

4、ilename,pathname=uigetfile('*.jpg''*.bmp''*.tif','选择图片'); str=pathname filename;global S %设计一个全局变量S,保存初始图像路径,以便之后的还原操作S=str;A=imread(str);set(handles.axes1,'HandleVisibility','ON');axes(handles.axes1);imshow(A);set(handles.axes1,'HandleVisibility&#

5、39;,'OFF');axes(handles.axes2); imshow(A);cla(handles.axes2);handles.img=A;guidata(hObject,handles);end程序关键部分: 通过filename,pathname=uigetfile('*.jpg''*.bmp''*.tif','选择图片')选择相应路径打开的图像;通过str=pathname filename; A=imread(str); 读取选中的图像;最后,通过imshow(A)在显示区域上显示图像。4.1.2

6、 保存图像利用“uiputfile”、“imwrite”函数实现图像文件的保存。function Save_Callback(hObject, eventdata, handles)sfilename,sfilepath=uiputfile('*.jpg''*.bmp''*.tif''*.*','保存图像文件','untitled.jpg');if isequal(sfilename,sfilepath,0,0) sfilefullname=sfilepath,sfilename; imwrite(

7、handles.img,sfilefullname);else msgbox('取消保存','保存失败');end程序关键部分:通过sfilename ,sfilepath=uiputfile('*.jpg''*.bmp''*.tif''*.*','保存图像文件','untitled.jpg')选择图像文件保存的路径与格式;然后,通过sfilefullname=sfilepath ,sfilename;imwrite(handles.img,sfilefullname

8、); 实现对图像的保存。4.1.3 退出程序function Exit_Callback(hObject, eventdata, handles)clc;close all;close(gcf);clear;4.2 图像转化为灰度图像由于在MATLAB中较多的图像处理函数支持对灰度图像进行处理,故对图像进行灰度转化十分必要。可利用rgb2gray(X)函数对其他图像进行灰度图像的转化。实现程序段如下:function to_gray_Callback(hObject, eventdata, handles)global T %设计一个全局变量T,保存初始图像路径,以便之后的撤销操作axes(h

9、andles.axes2);T=handles.img;img=handles.img;if numel(size(img)>2 C=rgb2gray(img);else C=img;endimshow(C);handles.img=C;guidata(hObject,handles);4.3 底片处理(反色)将图像变为底片并显示。实现程序段如下:function negative_Callback(hObject, eventdata, handles)global Taxes(handles.axes2);T=handles.img;I=imcomplement(handles.im

10、g); imshow(I);handles.img=I;guidata(hObject,handles);4.4 截图通过imcrop(x)函数来实现对图片某一区域的截取,截取的图片在右框中显示。结合“保存为”,可把截图处理后的图片保存在指定路径。实现程序段如下:function Cut_Callback(hObject, eventdata, handles)global Taxes(handles.axes2);T=handles.img;x=imcrop(handles.img); %截图imshow(x);handles.img=x;guidata(hObject,handles);4

11、.5 亮度和对比度度调节4.5.1 亮度调节实现程序段如下:function light_Callback(hObject, eventdata, handles)global Taxes(handles.axes2);T=handles.img;prompt='调整倍数 (0,1:明 1,):暗)'defans='1'p=inputdlg(prompt,'input',1,defans);p1=str2num(p1);y=imadjust(handles.img, , ,p1); %亮度调节imshow(y);handles.img=y;gui

12、data(hObject,handles);4.5.2 对比度调节实现程序段如下:function contrast_Callback(hObject, eventdata, handles)global Tstr=get(handles.contrast1,'value');axes(handles.axes2);T=handles.img;prompt='请输入参数:'defans='1'p=inputdlg(prompt,'input',1,defans);p1=str2num(p1);switch str case 1 f

13、=immultiply(handles.img,p1); imshow(f); handles.img=f; guidata(hObject,handles); case 2 f=imdivide(handles.img,p1); imshow(f); handles.img=f; guidata(hObject,handles);end4.6 图像的翻转与旋转4.6.1 翻转实现程序段如下:function turn_Callback(hObject, eventdata, handles)global Tstr=get(handles.fanzhuan,'value');a

14、xes(handles.axes2);I=handles.img;if numel(size(I)>2 switch str case 1 T=handles.img; b=I(:,:,1); c=I(:,:,2); d=I(:,:,3); I1(:,:,1)=fliplr(b); I1(:,:,2)=fliplr(c); I1(:,:,3)=fliplr(d); case 2 T=handles.img; b=I(:,:,1); c=I(:,:,2); d=I(:,:,3); I1(:,:,1)=flipud(b); I1(:,:,2)=flipud(c); I1(:,:,3)=fli

15、pud(d);endimshow(I1);handles.img=I1;guidata(hObject,handles);else switch str case 1 T=handles.img; f=fliplr(I); case 2 T=handles.img; f=flipud(I);endimshow(f);handles.img=f;guidata(hObject,handles);end程序关键部分:通过f=fliplr(handles.img); f=flipud(handles.img);分别实现左右镜像翻转与上下镜像翻转。4.6.2 旋转实现程序段如下:function ro

16、tate_Callback(hObject, eventdata, handles)global TT=handles.img;str=get(handles.clockwise,'value');axes(handles.axes2);prompt='请输入旋转角度:'defans='0'p=inputdlg(prompt,'input',1,defans);p1=str2num(p1);switch str case 1 p1=-p1; case 2 p1=p1;endf=imrotate(handles.img,p1,

17、9;bilinear','crop');imshow(f);handles.img=f;guidata(hObject,handles);程序关键部分:通过p=inputdlg(prompt,'input',1,defans);p1=str2num(p1)来输入旋转参数。通过函数f=imrotate(handles.img,p1,'bilinear','crop');实现翻转。4.7 添加噪声通过imnoise(I,type,parameters)来加入各种噪声。实现程序段如下:function ADDNoise_Call

18、back(hObject, eventdata, handles)I=handles.img;global Tstr=get(handles.Noise1,'value');axes(handles.axes2);switch str case 1 T=handles.img; prompt='输入椒盐噪声参数1:' defans='0.02' p=inputdlg(prompt,'input',1,defans); p1=str2num(p1); f=imnoise(handles.img,'salt & pep

19、per',p1); imshow(f); handles.img=f; guidata(hObject,handles); case 2 T=handles.img; prompt='输入高斯噪声1:','输入高斯噪声2' defans='0','0.02' p=inputdlg(prompt,'input',1,defans); p1=str2num(p1); p2=str2num(p2); f=imnoise(handles.img,'gaussian',p1,p2); imshow(f

20、); handles.img=f; guidata(hObject,handles); case 3 T=handles.img; prompt='输入乘性噪声1:' defans='0.02' p=inputdlg(prompt,'input',1,defans); p1=str2num(p1); f=imnoise(handles.img,'speckle',p1); imshow(f); handles.img=f; guidata(hObject,handles); end4.8 平滑和锐化4.8.1 平滑滤波器实现程序段

21、如下:function smooth_Callback(hObject, eventdata, handles)global Tstr=get(handles.pinghua,'value');axes(handles.axes2);switch str case 1 T=handles.img; prompt='请输入模版维度:' defans='3' p=inputdlg(prompt,'input',1,defans); p1=str2num(p1); h1=fspecial('average',p1 p1)

22、; I=imfilter(handles.img,h1); imshow(I); handles.img=I; guidata(hObject,handles); case 2 T=handles.img; prompt='请输入模版维度:' defans='3' p=inputdlg(prompt,'input',1,defans); p1=str2num(p1); if numel(size(handles.img)>2 A=handles.img; R=A(:,:,1); G=A(:,:,2); B=A(:,:,3); GP(:,:,

23、1)=medfilt2(R,p1 p1); GP(:,:,2)=medfilt2(G,p1 p1); GP(:,:,3)=medfilt2(B,p1 p1); imshow(GP); handles.img=GP; else A=handles.img; I=medfilt2(A,p1 p1); imshow(I); handles.img=I; end guidata(hObject,handles); end4.8.2 锐化滤波器实现程序段如下:function sharpen_Callback(hObject, eventdata, handles)global Tstr=get(han

24、dles.ruihua,'value');axes(handles.axes2);T=handles.img;g=handles.img;switch str case 1 h=fspecial('sobel'); case 2 h=fspecial('prewitt'); case 3 h=fspecial('laplacian');endif numel(size(g)>2 R=g(:,:,1); G=g(:,:,2); B=g(:,:,3); R1=imfilter(R,h); GP(:,:,1)=imadd(R,R1

25、); G1=imfilter(G,h); GP(:,:,2)=imadd(G,G1); B1=imfilter(B,h); GP(:,:,3)=imadd(B,B1); imshow(GP); handles.img=GP;else g1=g; g2=imfilter(g1,h); g3=imadd(g2,g1); imshow(g3); handles.img=g3;endguidata(hObject,handles);4.9 直方图均衡化处理4.9.1 灰度图像实现程序段如下:function gray_histogram_Callback(hObject, eventdata, han

26、dles)global TT=handles.img;img=handles.img;if numel(size(img)>2 C=rgb2gray(img);else C=img;endB=histeq(C);axes(handles.axes2); imshow(B);handles.img=B;guidata(hObject,handles);4.9.2 RGB图像实现程序段如下:function rgb_histogram_Callback(hObject, eventdata, handles)global Taxes(handles.axes2);T=handles.img;

27、RGB=handles.img;R=RGB(:,:,1);G=RGB(:,:,2);B=RGB(:,:,3);G1(:,:,1)=histeq(R);G1(:,:,2)=histeq(G);G1(:,:,3)=histeq(B);imshow(G1);handles.img=G1;guidata(hObject,handles);4.10 图像的腐蚀和膨胀4.10.1 腐蚀实现程序段如下:function corrode_Callback(hObject, eventdata, handles)temp='腐蚀圆盘半径:'defans='0'p=inputdlg

28、(temp,'请输入',1,defans);p1=str2num(p1);se=strel('disk',p1,0);global Taxes(handles.axes2);T=handles.img;I1=imerode(handles.img,se);imshow(I1); handles.img=I1;guidata(hObject,handles);4.10.2 膨胀实现程序段如下:function dilate_Callback(hObject, eventdata, handles)temp='膨胀结构长度:'defans='

29、0'p=inputdlg(temp,'请输入',1,defans);p1=str2num(p1);se=strel('line',p1,0);I1=imdilate(handles.img,se);global Taxes(handles.axes2);T=handles.img;imshow(I1);handles.img=I1;guidata(hObject,handles);4.11 边缘检测实现程序段如下:function edge_Callback(hObject, eventdata, handles)global Tstr=get(hand

30、les.edge2,'value');axes(handles.axes2);if numel(size(handles.img)>2 I=handles.img; switch str case 1 T=handles.img; img=I; x y z=size(img); if z=1 rslt=edge(img,'Roberts'); elseif z=3 img1=rgb2ycbcr(img); dx1=edge(img1(:,:,1),'Roberts'); dx1=(dx1*255); img2(:,:,1)=dx1; img

31、2(:,:,2)=img1(:,:,2); img2(:,:,3)=img1(:,:,3); rslt=ycbcr2rgb(uint8(img2); end imshow(rslt); handles.img=rslt; guidata(hObject,handles); case 2 T=handles.img; img=I; x y z=size(img); if z=1 rslt=edge(img,'sobel'); elseif z=3 img1=rgb2ycbcr(img); dx1=edge(img1(:,:,1),'sobel'); dx1=(dx

32、1*255); img2(:,:,1)=dx1; img2(:,:,2)=img1(:,:,2); img2(:,:,3)=img1(:,:,3); rslt=ycbcr2rgb(uint8(img2); end imshow(rslt); handles.img=rslt; guidata(hObject,handles); case 3 T=handles.img; img=I; x y z=size(img); if z=1 rslt=edge(img,'Prewitt'); elseif z=3 img1=rgb2ycbcr(img); dx1=edge(img1(:,

33、:,1),'Prewitt'); dx1=(dx1*255); img2(:,:,1)=dx1; img2(:,:,2)=img1(:,:,2); img2(:,:,3)=img1(:,:,3); rslt=ycbcr2rgb(uint8(img2); end imshow(rslt); handles.img=rslt; guidata(hObject,handles); case 4 T=handles.img; img=I; x y z=size(img); if z=1 rslt=edge(img,'log'); elseif z=3 img1=rgb2

34、ycbcr(img); dx1=edge(img1(:,:,1),'log'); dx1=(dx1*255); img2(:,:,1)=dx1; img2(:,:,2)=img1(:,:,2); img2(:,:,3)=img1(:,:,3); rslt=ycbcr2rgb(uint8(img2); end imshow(rslt); handles.img=rslt; guidata(hObject,handles); case 5 T=handles.img; img=I; x y z=size(img); if z=1 rslt=edge(img,'canny&#

35、39;); elseif z=3 img1=rgb2ycbcr(img); dx1=edge(img1(:,:,1),'canny'); dx1=(dx1*255); img2(:,:,1)=dx1; img2(:,:,2)=img1(:,:,2); img2(:,:,3)=img1(:,:,3); rslt=ycbcr2rgb(uint8(img2); end imshow(rslt); handles.img=rslt; guidata(hObject,handles); endelse g1=handles.img; switch str case 1 T=handles

36、.img; g2=edge(g1,'Roberts'); imshow(g2); handles.img=g2; guidata(hObject,handles); case 2 T=handles.img; g2=edge(g1,'sobel'); imshow(g2); handles.img=g2; guidata(hObject,handles); case 3 T=handles.img; g2=edge(g1,'Prewitt'); imshow(g2); handles.img=g2; guidata(hObject,handles

37、); case 4 T=handles.img; g2=edge(g1,'log'); imshow(g2); handles.img=g2; guidata(hObject,handles); case 5 T=handles.img; g2=edge(g1,'canny'); imshow(g2); handles.img=g2; guidata(hObject,handles); endend4.12 还原和撤销4.12.1 还原通过一个全局变量保存原始图像路径,在需要还原至原始图像时,重新读取该全局变量即可。实现程序段如下:function rst_Ca

38、llback(hObject, eventdata, handles)global S y=imread(S);axes(handles.axes2); imshow(y);handles.img=y;guidata(hObject,handles);4.12.2 撤销撤销上一步的操作。通过另设一个全局变量T保存是上一次操作后的图像。实现程序段如下:function back_Callback(hObject, eventdata, handles)global Thandles.img=T;axes(handles.axes2); imshow(T);guidata(hObject,hand

39、les);5. 结果分析5.1 转为灰度图像5.2 底片处理5.3 截图5.4 亮度和对比度调节5.4.1 亮度调节5.4.2 对比度调节5.5 图像翻转与旋转5.5.1 翻转1) 左右翻转2) 上下翻转5.5.2 旋转1) 顺时针45°2) 逆时针135°5.6 添加噪声、平滑和锐化5.6.1 添加噪声1) 椒盐噪声2) 高斯噪声3) 乘性噪声5.6.2 平滑滤波(添加椒盐噪声后)1) 均值滤波2) 中值滤波5.6.3 锐化滤波1) sobel算子2) Prewitt算子3) Laplacian算子5.7 直方图均衡化5.7.1 灰度图像5.7.2 RGB图像5.8 腐蚀

40、和膨胀5.8.1 腐蚀腐蚀圆盘半径5腐蚀圆盘半径105.8.2 膨胀膨胀结构长度5膨胀结构长度105.9 边缘检测1) Roberts算子2) Sobel算子3) Prewitt算子4) Log算子5) Canny算子6. 心得体会当看到老师所给实验题目时,感到有些无所适从。虽然,曾经学习过MATLAB的使用,但由于所学的MATLAB的知识更多的是用于其它课程,与数字图像处理这门课关系并不密切,所以可以说对MATLAB在数字图像处理方面的运用不是很熟练。本次实验的重点是句柄的使用、GUI的使用以及MATLAB中与图像处理相关的函数的使用。 因此,在开始编程之前,我利用课余时间,通过索引网络上的

41、相关资料,为实验做了较为充分的准备。在参考了相关材料及源程序之后,我对自己要做的实验内容有了进一步的了解,并对MATLAB的使用有了更深的体会。 当然,在课设的进行过程中,我还是遇到了不少问题。例如,起初由于我对句柄使用以及一些函数使用的不恰当。使得在实验中遇到了问题。随着实验的进行,对MATLAB的熟悉度逐步加深。在基本功不断扎实的基础上,我开始进行一些扩张功能的尝试,比如撤销、还原操作、添加菜单等 。 但是,总体来说,此次的设计,还是较为满意的。它不但鞭策着我去巩固MATLAB的基础理论知识,还提高了我对MATLAB的实际操作运用,使得理论与实践相结合,为进一步学习MATLAB打下坚实的基

42、础;同时,在实践中,也让我对数字图像处理这门课所学的内容有了更多的了解和认识,丰富了我对这门课程的认知,对这门课的基础有了更深刻的认识。7. 附录代码function varargout = B_zephyr(varargin)% B_zephyr MATLAB code for B_zephyr.fig% B_zephyr, by itself, creates a new B_zephyr or raises the existing% singleton*.% H = B_zephyr returns the handle to a new B_zephyr or the handle

43、to% the existing singleton*.% B_zephyr('CALLBACK',hObject,eventData,handles,.) calls the local% function named CALLBACK in B_zephyr.M with the given input arguments.% B_zephyr('Property','Value',.) creates a new B_zephyr or raises the% existing singleton*. Starting from the l

44、eft, property value pairs are% applied to the GUI before B_zephyr_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to B_zephyr_OpeningFcn via varargin.% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows

45、only one% instance to run (singleton)".% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help B_zephyr% Last Modified by GUIDE v2.5 22-May-2013 21:15:30% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename

46、, . 'gui_Singleton', gui_Singleton, . 'gui_OpeningFcn', B_zephyr_OpeningFcn, . 'gui_OutputFcn', B_zephyr_OutputFcn, . 'gui_LayoutFcn', , . 'gui_Callback', );if nargin && ischar(varargin1) gui_State.gui_Callback = str2func(varargin1);endif nargout varar

47、gout1:nargout = gui_mainfcn(gui_State, varargin:);else gui_mainfcn(gui_State, varargin:);end% End initialization code - DO NOT EDIT% - Executes just before B_zephyr is made visible.function B_zephyr_OpeningFcn(hObject, eventdata, handles, varargin)% This function has no output args, see OutputFcn.%

48、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 B_zephyr (see VARARGIN)% Choose default command line output for B_zephyrhandles.output = hObject;% Update handles

49、 structureguidata(hObject, handles);% UIWAIT makes B_zephyr wait for user response (see UIRESUME)% uiwait(handles.figure1);% - Outputs from this function are returned to the command line.function varargout = B_zephyr_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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论