您的当前位置:首页正文

MATLAB

来源:独旅网


数字图像处理实验报告

——基于MATLAB语言的图像处理软件

姓 名 : 班 级 : 学 号 : 专 业 : 电子信息工程

1. 设计目的

利用MATLAB的GUI程序设计一个简单实用的图像处理程序。该程序应具备图像处理的常用功能,以满足要求。

2. 设计要求(至少完成以下6项基本功能)

设计程序有以下基本功能:

1) 图像的读取、保存和程序退出(必做,可做成按钮,也可做成菜单) 2) 图像转化为灰度图像 3) 底片处理(反色) 4) 截图(必做)

5) 亮度和对比度度调节(按照设计图必做) 6) 图像的翻转与旋转(按照设计图必做) 7) 添加噪声 8) 平滑和锐化

9) 直方图均衡化处理(必做)

10) 图像的腐蚀和膨胀(按照设计图必做) 11) 边缘检测

12) 还原和撤销(选做)

3. 总体设计

软件的总体设计界面布局如上图所示,主要分为2个区域:显示区域与操作区域。

显示区域:显示原图像,以及效果图,即处理前与处理后的图像。 操作区域:通过功能键实现对图像的各种处理。

在图中可见,界面左边和下方为一系列功能按键如“转为灰度图像”、“撤销”、“还原”等等 ;界面正中部分为图片显示部分。

设计完成后运行的软件界面如下:

4.模块设计

4.1 图像的读取、保存和程序退出 4.2 图像转化为灰度图像 4.3 底片处理(反色) 4.4 截图

4.5 亮度和对比度度调节 4.6 图像的翻转与旋转 4.7 添加噪声 4.8 平滑和锐化

4.9 直方图均衡化处理 4.10 图像的腐蚀和膨胀 4.11 边缘检测 4.12 还原和撤销

5.结果分析

原图:

5.1 转为灰度图像(函数:I=rgb2gray())

5.2 底片处理(函数:I=imcomplement())

5.3 截图(函数: I=imcrop())

亮度和对比度调节(亮度函数: I= imadjust() 对比度函数f=immultiply(T,p1);

f=imdivide(T,p1); ) 亮度:

对比度:

5.5 图像翻转与旋转(翻转函数:左右I=fliplr(); 上下I=flipud(); 旋转函数: I=imrotate(A,angle,method,bbox) ) 上下翻转:

左右翻转:

顺时针60度

逆时针60度

5.6 添加噪声、平滑和锐化(噪声函数:I=imnoise((I,type))

平滑:中值滤波函数I=medfilt2();均值滤波函数:I=conv2(T,D,'same') 锐化函数:I=fspecial('');

噪声:

平滑:

锐化:

5.7 直方图均衡化(函数J=histeq(T, map);) 灰度图:

RGB:

5.8 腐蚀和膨胀 (腐蚀函数I=imerode(T,b); 膨胀函数:I=imdilate(T,b);)

腐蚀:

膨胀:

5.9 边缘检测(函数:I=edge(I,''))

6.心得体会

之前的学习中就已经有接触过MATLAB,通过这次的实践,更加的深入了解了MATLAB这个软件的应用,熟悉和掌握了MATLAB 程序设计方法、MATLAB GUI 程序设计和MATLAB图像处理工具箱,了解了图形用户界面的制作的设计原理和一般步骤,学会了运用MATLAB工具箱对图像进行处理和分析。图形用户界面是包含图形对象如窗口、图标、菜单和文本的用户界面。

通过这次实践的锻炼,老师培养了我们独立自主解决问题的能力,在遇到难解的问题时老师能耐心的解答,才能使得作品左后的完成。

7.附录代码(带注释)

转为灰度图像:

global T % 设计全局变量T,保持初始图像路径,便于之后的撤销操作

axes(handles.axes2); T=handles.img; img=handles.img; if numel(size(img))>2 C=rgb2gray(img); else C=img; end imshow(C); handles.img=C;

guidata(hObject,handles); 底片处理: global T

axes(handles.axes2); T=handles.img;

I=imcomplement(handles.img); imshow(I); handles.img=I;

guidata(hObject,handles); 截图: global T

axes(handles.axes2); T=getimage;

x=imcrop(handles.img); imshow(x); handles.img=x;

guidata(hObject,handles); 撤销:

axes(handles.axes2); global T imshow(T); handles.img=T;

guidata(hObject,handles); 还原:

global S y=imread(S);

axes(handles.axes2); imshow(y); handles.img=y;

guidata(hObject,handles); 亮度: global T

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); 对比度: global T

str=get(handles.popupmenu1,'value'); axes(handles.axes2); T=handles.img;

prompt={'ÇëÊäÈë²ÎÊý:'}; defans={'1'};

p=inputdlg(prompt,'input',1,defans); p1=str2num(p{1}); switch str case 1

f=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); end 翻转: global T

a=get(gcbo,'Value'); T=handles.img; axes(handles.axes2); switch a case 1

T=handles.img;

f=fliplr(handles.img); %左右 imshow(f); handles.img=f;

guidata(hObject,handles); case 2

T=handles.img;

f=flipud(handles.img); %上下

imshow(f); handles.img=f; guidata(hObject,handles); end 旋转: global T

a=get(gcbo,'Value'); T=handles.img; axes(handles.axes2); switch a case 1

T=handles.img;

prompt={'旋转角度:'}; defans={'0'};

p=inputdlg(prompt,'input',1,defans); p1=str2num(p{1});

f=imrotate(handles.img,-p1,'bilinear','crop'); %顺时针 imshow(f); handles.img=f;

guidata(hObject,handles); case 2

T=handles.img; prompt={'旋转角度:'}; defans={'0'};

p=inputdlg(prompt,'input',1,defans); p1=str2num(p{1});

f=imrotate(handles.img,p1,'bilinear','crop'); %逆时针 imshow(f); handles.img=f;

guidata(hObject,handles); end; 噪声: global T

a=get(gcbo,'Value'); T=handles.img; axes(handles.axes2); switch a case 1

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); case 2

T=getimage;

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); case 3

T=getimage;

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; 平滑: global T

a=get(gcbo,'Value'); T=handles.img; axes(handles.axes2); switch a case 1

T=getimage;

T=medfilt2(handles.img); imshow(T); handles.img=T;

guidata(hObject,handles); case 2

T=getimage; T=double(T)/255; D=ones(3)/9; k=conv2(T,D,'same'); imshow(k); handles.img=k;

guidata(hObject,handles); end; 锐化: global T

a=get(gcbo,'Value'); T=handles.img; axes(handles.axes2); switch a case 1

T=getimage; H=fspecial('sobel'); BW=filter2(H,T); imshow(BW) handles.img=T;

guidata(hObject,handles); case 2

T=getimage; H=fspecial('log'); BW=filter2(H,T); imshow(BW) handles.img=T;

guidata(hObject,handles); case 3

T=getimage;

H=fspecial('Laplacian'); BW=filter2(H,T); imshow(BW) handles.img=T;

guidata(hObject,handles); case 4

T=getimage;

H=fspecial('Prewitt'); BW=filter2(H,T); imshow(BW) handles.img=T;

guidata(hObject,handles); case 5

T=getimage; T=double(T); w1=[-1 0;0 1]; w2=[0 -1;1 0];

G1=imfilter(T,w1,'corr','replicate'); G2=imfilter(T,w2,'corr','replicate'); BW=abs(G1)+abs(G2); imshow(BW,[]);

handles.img=T;

guidata(hObject,handles); end;

灰度图像(直方图均衡化): global T

axes(handles.axes2); T=handles.img; imshow(T);

J=histeq(T,64); imshow(J); guidata(hObject,handles); RGB图像(直方图均衡化): global T

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); 腐蚀: global T

axes(handles.axes2); T=handles.img;

b = strel('square',3); f=imerode(handles.img,b); b=strel('ball',3,3); imshow(f); handles.img=f;

guidata(hObject,handles); 膨胀: global T

axes(handles.axes2); T=handles.img;

b = strel('square',3); f=imdilate(handles.img,b); b=strel('ball',3,3); imshow(f); handles.img=f;

guidata(hObject,handles);

边缘检测: global T

a = get(gcbo,'Value'); T=handles.img; axes(handles.axes2); switch a case 1

BW = edge(T,'sobel');

axes(handles.axes2); axes1 = imshow(BW); case 2

BW = edge(T,'prewitt');

axes(handles.axes2); axes1 = imshow(BW); case 3

BW = edge(T,'canny');

axes(handles.axes2); axes1 = imshow(BW); case 4

BW = edge(T,'Roberts');

axes(handles.axes2); axes1 = imshow(BW); case 5

BW = edge(T,'log');

axes(handles.axes2); axes1 = imshow(BW); end; 载入图像:

[filename,pathname]=uigetfile({'*.jpg';'*.bmp';'*.tif'},'选择图片'); str=[pathname filename]; global 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); 保存图像:

[sfilename,sfilepath]=uiputfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'±£´æͼÏñÎļþ','untitled.jpg

');

if ~isequal([sfilename,sfilepath],[0,0]) sfilefullname=[sfilepath,sfilename]; imwrite(handles.img,sfilefullname); else

msgbox('取消保存','保存失败'); end 退出: clc; close all; close(gcf); clear;

因篇幅问题不能全部显示,请点此查看更多更全内容