基于MATLAB语言的数字图像处理实验GUI.docx
- 文档编号:3378746
- 上传时间:2022-11-22
- 格式:DOCX
- 页数:57
- 大小:9.65MB
基于MATLAB语言的数字图像处理实验GUI.docx
《基于MATLAB语言的数字图像处理实验GUI.docx》由会员分享,可在线阅读,更多相关《基于MATLAB语言的数字图像处理实验GUI.docx(57页珍藏版)》请在冰豆网上搜索。
基于MATLAB语言的数字图像处理实验GUI
数字图像处理实验报告
——基于MATLAB语言的图像处理软件
姓名:
班级:
学号:
专业:
1.设计目的
利用MATLAB的GUI程序设计一个简单实用的图像处理程序。
该程序应具备图像处理的常用功能,以满足要求。
2.设计要求
设计程序有以下基本功能:
1)图像的读取和保存
2)图像转化为灰度图像
3)底片处理(反色)
4)截图
5)亮度和对比度度调节
6)图像的翻转与旋转
7)添加噪声
8)平滑和锐化
9)直方图均衡化处理
10)图像的腐蚀和膨胀
11)边缘检测
3.总体设计
软件的总体设计界面布局如上图所示,主要分为2个区域:
显示区域与操作区域。
显示区域:
显示原图像,以及效果图,即处理前与处理后的图像。
操作区域:
通过功能键实现对图像的各种处理。
在图中可见,界面左边和下方为一系列功能按键如“转为灰度图像”、“撤销”、“还原”等等;界面正中部分为图片显示部分。
设计完成后运行的软件界面如下:
4.模块设计
以下介绍各个功能模块的功能与实现
4.1图像的读取、保存和程序退出
通过MenuEditor
创建如下菜单,通过以下菜单来实现“载入图像”、“保存图像”、“退出”的功能。
4.1.1载入图像
利用MATLAB中“uigetfile”、“imread”、“imshow”实现图像文件的读取与显示。
functionInput_Callback(hObject,eventdata,handles)
[filename,pathname]=uigetfile({'*.jpg';'*.bmp';'*.tif'},'选择图片');
str=[pathnamefilename];
globalS%设计一个全局变量S,保存初始图像路径,以便之后的还原操作
S=str;
A=imread(str);
set(handles.axes1,'HandleVisibility','ON');
axes(handles.axes1);
imshow(A);
set(handles.axes1,'HandleVisibility','OFF');
axes(handles.axes2);
imshow(A);
cla(handles.axes2);
handles.img=A;
guidata(hObject,handles);
end
程序关键部分:
通过[filename,pathname]=uigetfile({'*.jpg';'*.bmp';'*.tif'},'选择图片')选择相应路径打开的图像;
通过str=[pathnamefilename];A=imread(str);读取选中的图像;
最后,通过imshow(A)在显示区域上显示图像。
4.1.2保存图像
利用“uiputfile”、“imwrite”函数实现图像文件的保存。
functionSave_Callback(hObject,eventdata,handles)
[sfilename,sfilepath]=uiputfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'保存图像文件','untitled.jpg');
if~isequal([sfilename,sfilepath],[0,0])
sfilefullname=[sfilepath,sfilename];
imwrite(handles.img,sfilefullname);
else
msgbox('取消保存','保存失败');
end
程序关键部分:
通过[sfilename,sfilepath]=uiputfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'保存图像文件','untitled.jpg')选择图像文件保存的路径与格式;
然后,通过sfilefullname=[sfilepath,sfilename];
imwrite(handles.img,sfilefullname);实现对图像的保存。
4.1.3退出程序
functionExit_Callback(hObject,eventdata,handles)
clc;
closeall;
close(gcf);
clear;
4.2图像转化为灰度图像
由于在MATLAB中较多的图像处理函数支持对灰度图像进行处理,故对图像进行灰度转化十分必要。
可利用rgb2gray(X)函数对其他图像进行灰度图像的转化。
实现程序段如下:
functionto_gray_Callback(hObject,eventdata,handles)
globalT%设计一个全局变量T,保存初始图像路径,以便之后的撤销操作
axes(handles.axes2);
T=handles.img;
img=handles.img;
ifnumel(size(img))>2
C=rgb2gray(img);
else
C=img;
end
imshow(C);
handles.img=C;
guidata(hObject,handles);
4.3底片处理(反色)
将图像变为底片并显示。
实现程序段如下:
functionnegative_Callback(hObject,eventdata,handles)
globalT
axes(handles.axes2);
T=handles.img;
I=imcomplement(handles.img);
imshow(I);
handles.img=I;
guidata(hObject,handles);
4.4截图
通过imcrop(x)函数来实现对图片某一区域的截取,截取的图片在右框中显示。
结合“保存为…”,可把截图处理后的图片保存在指定路径。
实现程序段如下:
functionCut_Callback(hObject,eventdata,handles)
globalT
axes(handles.axes2);
T=handles.img;
x=imcrop(handles.img);%截图
imshow(x);
handles.img=x;
guidata(hObject,handles);
4.5亮度和对比度度调节
4.5.1亮度调节
实现程序段如下:
functionlight_Callback(hObject,eventdata,handles)
globalT
axes(handles.axes2);
T=handles.img;
prompt={'调整倍数([0,1]:
明[1,~):
暗)'};
defans={'1'};
p=inputdlg(prompt,'input',1,defans);
p1=str2num(p{1});
y=imadjust(handles.img,[],[],p1);%亮度调节
imshow(y);
handles.img=y;
guidata(hObject,handles);
4.5.2对比度调节
实现程序段如下:
functioncontrast_Callback(hObject,eventdata,handles)
globalT
str=get(handles.contrast1,'value');
axes(handles.axes2);
T=handles.img;
prompt={'请输入参数:
'};
defans={'1'};
p=inputdlg(prompt,'input',1,defans);
p1=str2num(p{1});
switchstr
case1
f=immultiply(handles.img,p1);
imshow(f);
handles.img=f;
guidata(hObject,handles);
case2
f=imdivide(handles.img,p1);
imshow(f);
handles.img=f;
guidata(hObject,handles);
end
4.6图像的翻转与旋转
4.6.1翻转
实现程序段如下:
functionturn_Callback(hObject,eventdata,handles)
globalT
str=get(handles.fanzhuan,'value');
axes(handles.axes2);
I=handles.img;
ifnumel(size(I))>2
switchstr
case1
T=handles.img;
b=I(:
:
1);
c=I(:
:
2);
d=I(:
:
3);
I1(:
:
1)=fliplr(b);
I1(:
:
2)=fliplr(c);
I1(:
:
3)=fliplr(d);
case2
T=handles.img;
b=I(:
:
1);
c=I(:
:
2);
d=I(:
:
3);
I1(:
:
1)=flipud(b);
I1(:
:
2)=flipud(c);
I1(:
:
3)=flipud(d);
end
imshow(I1);
handles.img=I1;
guidata(hObject,handles);
else
switchstr
case1
T=handles.img;
f=fliplr(I);
case2
T=handles.img;
f=flipud(I);
end
imshow(f);
handles.img=f;
guidata(hObject,handles);
end
程序关键部分:
通过f=fliplr(handles.img);f=flipud(handles.img);分别实现左右镜像翻转与上下镜像翻转。
4.6.2旋转
实现程序段如下:
functionrotate_Callback(hObject,eventdata,handles)
globalT
T=handles.img;
str=get(handles.clockwise,'value');
axes(handles.axes2);
prompt={'请输入旋转角度:
'};
defans={'0'};
p=inputdlg(prompt,'input',1,defans);
p1=str2num(p{1});
switchstr
case1
p1=-p1;
case2
p1=p1;
end
f=imrotate(handles.img,p1,'bilinear','crop');
imshow(f);
handles.img=f;
guidata(hObject,handles);
程序关键部分:
通过p=inputdlg(prompt,'input',1,defans);p1=str2num(p{1})来输入旋转参数。
通过函数f=imrotate(handles.img,p1,'bilinear','crop');实现翻转。
4.7添加噪声
通过imnoise(I,type,parameters)来加入各种噪声。
实现程序段如下:
functionADDNoise_Callback(hObject,eventdata,handles)
I=handles.img;
globalT
str=get(handles.Noise1,'value');
axes(handles.axes2);
switchstr
case1
T=handles.img;
prompt={'输入椒盐噪声参数1:
'};
defans={'0.02'};
p=inputdlg(prompt,'input',1,defans);
p1=str2num(p{1});
f=imnoise(handles.img,'salt&pepper',p1);
imshow(f);
handles.img=f;
guidata(hObject,handles);
case2
T=handles.img;
prompt={'输入高斯噪声1:
','输入高斯噪声2'};
defans={'0','0.02'};
p=inputdlg(prompt,'input',1,defans);
p1=str2num(p{1});
p2=str2num(p{2});
f=imnoise(handles.img,'gaussian',p1,p2);
imshow(f);
handles.img=f;
guidata(hObject,handles);
case3
T=handles.img;
prompt={'输入乘性噪声1:
'};
defans={'0.02'};
p=inputdlg(prompt,'input',1,defans);
p1=str2num(p{1});
f=imnoise(handles.img,'speckle',p1);
imshow(f);
handles.img=f;
guidata(hObject,handles);
end
4.8平滑和锐化
4.8.1平滑滤波器
实现程序段如下:
functionsmooth_Callback(hObject,eventdata,handles)
globalT
str=get(handles.pinghua,'value');
axes(handles.axes2);
switchstr
case1
T=handles.img;
prompt={'请输入模版维度:
'};
defans={'3'};
p=inputdlg(prompt,'input',1,defans);
p1=str2num(p{1});
h1=fspecial('average',[p1p1]);
I=imfilter(handles.img,h1);
imshow(I);
handles.img=I;
guidata(hObject,handles);
case2
T=handles.img;
prompt={'请输入模版维度:
'};
defans={'3'};
p=inputdlg(prompt,'input',1,defans);
p1=str2num(p{1});
ifnumel(size(handles.img))>2
A=handles.img;
R=A(:
:
1);
G=A(:
:
2);
B=A(:
:
3);
GP(:
:
1)=medfilt2(R,[p1p1]);
GP(:
:
2)=medfilt2(G,[p1p1]);
GP(:
:
3)=medfilt2(B,[p1p1]);
imshow(GP);
handles.img=GP;
else
A=handles.img;
I=medfilt2(A,[p1p1]);
imshow(I);
handles.img=I;
end
guidata(hObject,handles);
end
4.8.2锐化滤波器
实现程序段如下:
functionsharpen_Callback(hObject,eventdata,handles)
globalT
str=get(handles.ruihua,'value');
axes(handles.axes2);
T=handles.img;
g=handles.img;
switchstr
case1
h=fspecial('sobel');
case2
h=fspecial('prewitt');
case3
h=fspecial('laplacian');
end
ifnumel(size(g))>2
R=g(:
:
1);
G=g(:
:
2);
B=g(:
:
3);
R1=imfilter(R,h);
GP(:
:
1)=imadd(R,R1);
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;
end
guidata(hObject,handles);
4.9直方图均衡化处理
4.9.1灰度图像
实现程序段如下:
functiongray_histogram_Callback(hObject,eventdata,handles)
globalT
T=handles.img;
img=handles.img;
ifnumel(size(img))>2
C=rgb2gray(img);
else
C=img;
end
B=histeq(C);
axes(handles.axes2);imshow(B);
handles.img=B;
guidata(hObject,handles);
4.9.2RGB图像
实现程序段如下:
functionrgb_histogram_Callback(hObject,eventdata,handles)
globalT
axes(handles.axes2);
T=handles.img;
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腐蚀
实现程序段如下:
functioncorrode_Callback(hObject,eventdata,handles)
temp={'腐蚀圆盘半径:
'};
defans={'0'};
p=inputdlg(temp,'请输入',1,defans);
p1=str2num(p{1});
se=strel('disk',p1,0);
globalT
axes(handles.axes2);
T=handles.img;
I1=imerode(handles.img,se);
imshow(I1);
handles.img=I1;
guidata(hObject,handles);
4.10.2膨胀
实现程序段如下:
functiondilate_Callback(hObject,eventdata,handles)
temp={'膨胀结构长度:
'};
defans={'0'};
p=inputdlg(temp,'请输入',1,defans);
p1=str2num(p{1});
se=strel('line',p1,0);
I1=imdilate(handles.img,se);
globalT
axes(handles.axes2);
T=handles.img;
imshow(I1);
handles.img=I1;
guidata(hObject,handles);
4.11边缘检测
实现程序段如下:
functionedge_Callback(hObject,eventdata,handles)
globalT
str=get(handles.edge2,'value');
axes(handles.axes2);
ifnumel(size(handles.img))>2
I=handles.img;
switchstr
case1
T=handles.img;
img=I;
[xyz]=size(img);
ifz==1
rslt=edge(img,'Roberts');
elseifz==3
img1=rgb2ycbcr(img);
dx1=edge(img1(:
:
1),'Roberts');
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);
case2
T=handles.img;
img=I;
[xyz]=size(img);
ifz==1
rslt=edge(img,'sobel');
elseifz==3
img1=rgb2ycbcr(img);
dx1=edge(img1(:
:
1),'sobel');
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);
case3
T=handles.img;
img=I;
[xyz]=size(img);
ifz==1
rslt=edge(img,'Prewitt');
elseifz==3
img1=rgb2ycbcr(img);
dx1=edge(img1(:
:
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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 MATLAB 语言 数字图像 处理 实验 GUI