MATLAB中GUI在图像处理应用中的设计(包括各种算法)_matlab与gui图像处理
MATLAB中GUI在图像处理应用中的设计(包括各种算法)由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“matlab与gui图像处理”。
用MATLAB 进行图像处理算法的界面设计/
目录
1.设计目的„„„„„„„„„„„„„„„„„„„3 2.题目分析„„„„„„„„„„„„„„„„„„„3 3.总体设计„„„„„„„„„„„„„„„„„„„3 4.具体设计„„„„„„„„„„„„„„„„„„„5 5.结果分析„„„„„„„„„„„„„„„„„„„34 6.心得体会„„„„„„„„„„„„„„„„„„„34 7.附录代码„„„„„„„„„„„„„„„„„„„36
/ 52
1、设计目的:综合运用MATLAB工具箱实现图像处理的GUI程序设计,利用MATLAB图像处理工具箱,设计和实现自己的Photoshop。
2、题目分析
利用matlab的GUI程序设计一个简单实用的图像处理程序。该程序应具备图像处理的常用功能,以满足用户的使用。现设计程序有以下基本功能: 1)图像的读取和保存。
2)设计图形用户界面,让用户能够对图像进行任意的亮度和对比度变化调整,显示和对比变换前后的图像。
3)设计图形用户界面,让用户能够用鼠标选取图像感兴趣区域,显示和保存该选择区域。
4)编写程序通过最近邻插值和双线性插值等算法将用户所选取的图像区域进行放大和缩小整数倍的操作,并保存,比较几种插值的效果。
5)图像直方图统计和直方图均衡,要求显示直方图统计,比较直方图均衡后的效果。
6)能对图像加入各种噪声,并通过几种滤波算法实现去噪并显示结果。7)额外功能。
3、总体设计
/ 52 图一
软件的总体设计界面布局如上图所示,主要分为2个部分:显示区域与操作区域。显示区域:显示载入原图,以及通过处理后的图像。操作区域:通过功能键实现对图像的各种处理。
在截图中可见,左部为一系列功能按键如“还原”、“撤销”、“截图”等等 ;界面正中部分为图片显示部分,界面中下方为系列功能切换选择组。设计完成后运行的软件界面如下:
图二
与图一先比,运行后的界面更为简洁。利用“编辑”菜单可调出相应的功能键。例如:
/ 52
4、具体设计
现介绍各个功能模块的功能与实现。4.1菜单栏的设计。通过Menu Editor
创建如下菜单,通过以下菜单来控制显示或隐藏功能按键
以“编辑”菜单中“图像变形”中的“图像翻转”为例说明实现用户界面功能键“图像翻转”的显示与隐藏。实现该功能的程序段如下:
function tuxiangfanzhuan_Callback(hObject, eventdata, handles)% hObject handle to tuxiangfanzhuan(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)
[filename,pathname]=uigetfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'载入图像');if isequal(filename,0)|isequal(pathname,0)errordlg('没有选中文件','出错');return;else
file=[pathname,filename];global S %设置一个全局变量S,保存初始图像路径,以便之后的还原操作 S=file;x=imread(file);set(handles.axes1,'HandleVisibility','ON');axes(handles.axes1);imshow(x);set(handles.axes1,'HandleVisibility','OFF');axes(handles.axes2);imshow(x);handles.img=x;guidata(hObject,handles);end
程序关键部分:
通过[filename,pathname]=uigetfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'载入图像')选择相应路径打开的图像;通过file=[pathname,filename];x=imread(file);读取选中的图像;最后,通过imshow(x)在显示区域上显示图像。
/ 52
(2)图像保存。
利用“uiputfile”、“imwrite”函数实现图像文件的保存。
function save_Callback(hObject, eventdata, handles)% hObject handle to save(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)clc;close all;close(gcf);clear;
4.3对图像进行任意的亮度和对比度变化调整,显示和对比变换前后的图像。
运行程序后,通过“编辑”菜单中的“常用处理”选中“亮度调节”
在显示出相应的功能键后,通过载入读取图像,比并进行处理,效果如下: 亮度处理前:
/ 52
亮度处理后:
实现程序段如下:
%---Executes on button pre in radiobutton12.function radiobutton12_Callback(hObject, eventdata, handles)% hObject handle to radiobutton12(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global T str=get(hObject,'string');axes(handles.axes2);
switch str case'增强' T=getimage;prompt={'输入参数:'};defans={'1'};p=inputdlg(prompt,'input',1,defans);p1=str2num(p{1});f=immultiply(handles.img,p1);imshow(f);handles.img=f;guidata(hObject,handles);case'减弱' T=getimage;prompt={'输入参数:'};defans={'1'};12 / 52 p=inputdlg(prompt,'input',1,defans);p1=str2num(p{1});f=imdivide(handles.img,p1);imshow(f);handles.img=f;guidata(hObject,handles);end
该程序段主要通过 f=immultiply(handles.img,p1);p=inputdlg(prompt,'input',1,defans);分别实现图像对比度的增强与减弱。
4.4 用鼠标选取图像感兴趣区域,显示和保存该选择区域。
通过imcrop(x)函数来实现对图片某一区域的截取,截取的图片在右框中显示。结合“保存为„”,可把截图处理后的图片保存在指定路径。
实现程序段如下:
%---Executes on button pre in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)
% Hint: get(hObject,'Value')returns toggle state of radiobutton16
global T axes(handles.axes2);14 / 52 T=getimage;x=rgb2gray(handles.img);%RGBͼÏñת»»Îª»Ò¶ÈͼÏñ imshow(x);handles.img=x;guidata(hObject,handles);
4.6对图像进行放大和缩小整数倍的操作。
通过imresize(X,n,mode)函数对图像X进行放大或者缩小。N放大缩小倍数,mode为采用的方式。
通过处理后可发现保存的图片的比原图放大了(缩小了)。实现的程序段如下:
function uipanel9_SelectionChangeFcn(hObject, eventdata, handles)% hObject handle to uipanel9(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global T axes(handles.axes2);T=getimage;h=histeq(handles.img);imshow(h);handles.img=h;guidata(hObject,handles);关键部分:通过 h=histeq(handles.img)进行直方图均衡(2)直方图统计。通过利用imhist(X)函数来实现直方图统计。
/ 52
%---Executes on button pre in pushbutton8.function pushbutton8_Callback(hObject, eventdata, handles)% hObject handle to pushbutton8(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global T str=get(hObject,'string');axes(handles.axes2);switch str case '椒盐噪声' T=getimage;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 '¸高斯噪声' T=getimage;prompt={'输入高斯噪声1:','输入高斯噪声2'};defans={'0','0.02'};21 / 52 p=inputdlg(prompt,'input',1,defans);p1=str2num(p{1});p2=str2num(p{2});f=imnoise(handles.img,'gauian',p1,p2);imshow(f);handles.img=f;guidata(hObject,handles);case '乘性噪声' 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
(2)滤除噪声(椒盐噪声)。滤波前
中值滤波后
/ 52
线性滤波后
自适应滤波后
实现程序段如下:
function uipanel5_SelectionChangeFcn(hObject, eventdata, handles)%图像滤波
% hObject handle to uipanel5(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)
axes(handles.axes2);y1=handles.img;f=double(y1);% 数据类型转换,matlab不支持图像的无符号整型的计算 g=fft2(f);% 傅里叶变换 g=fftshift(g);% 转换数据矩阵 [M,N]=size(g);nn=2;%二阶巴特沃斯低通滤波器 d0=50;%截止频率50 m=fix(M/2);n=fix(N/2);for i=1:M for j=1:N d=sqrt((i-m)^2+(j-n)^2);h=1/(1+0.414*(d/d0)^(2*nn));% 计算低通滤波器传递函数 result(i,j)=h*g(i,j);end
/ 52 end
result=ifftshift(result);y2=ifft2(result);y3=uint8(real(y2));imshow(y3);% 显示处理后的图像
%---Executes on button pre in pushbutton15.function pushbutton15_Callback(hObject, eventdata, handles)% hObject handle to pushbutton15(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)
global S %还原 axes(handles.axes2);y=imread(S);f=imshow(y);handles.img=y;guidata(hObject,handles);4.10 撤销。
撤销上一步的操作。通过另设一个全局变量T保存是上一次操作后的图像。
/ 52 实现程序段如下:
---Executes on button pre in pushbutton11.function pushbutton11_Callback(hObject, eventdata, handles)% hObject handle to pushbutton11(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)str=get(hObject,'string');axes(handles.axes2);global T switch str case '左右翻转' T=handles.img;f=fliplr(handles.img);imshow(f);handles.img=f;guidata(hObject,handles);case '上下翻转' T=handles.img;f=flipud(handles.img);imshow(f);handles.img=f;guidata(hObject,handles);29 / 52 end
程序关键部分:通过f=fliplr(handles.img);f=flipud(handles.img);分别实现左右镜像翻转与上下镜像翻转。
(2)图像旋转。
实现图像的逆时针旋转任意角度。
实现程序段如下:
function pushbutton3_Callback(hObject, eventdata, handles)%图像爱那个旋转
% hObject handle to pushbutton3(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global T axes(handles.axes2);T=getimage;f=imcomplement(handles.img);%图像取反´
/ 52 imshow(f);handles.img=f;guidata(hObject,handles);
程序段关键部分:通过f=imcomplement(handles.img);实现图像取反,形成底片效果。
(2)边缘信息。采取图像的边缘信息。
实现程序段如下:
%---Executes on button pre in pushbutton16.function pushbutton16_Callback(hObject, eventdata, handles)% hObject handle to pushbutton16(see GCBO)
% eventdata reservedDO NOT EDIT gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename,...'gui_Singleton', gui_Singleton,...'gui_OpeningFcn', @tuxiangchuli_OpeningFcn,...'gui_OutputFcn', @tuxiangchuli_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 codeto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)% varargin command line arguments to tuxiangchuli(see VARARGIN)
% Choose default command line output for tuxiangchuli handles.output = hObject;
% Update handles structure guidata(hObject, handles);
% UIWAIT makes tuxiangchuli wait for user response(see UIRESUME)% uiwait(handles.figure1);
%---Outputs from this function are returned to the command line.function varargout = tuxiangchuli_OutputFcn(hObject, eventdata, handles)% varargout cell array for returning output args(see VARARGOUT);% hObject handle to figure
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)
%------function exit_Callback(hObject, eventdata, handles)% hObject handle to exit(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)[filename,pathname]=uigetfile({'*.jpg';'*.bmp';'*.tif';'*.*'},'ÔØÈëͼÏñ');if isequal(filename,0)|isequal(pathname,0)errordlg('ûÓÐÑ¡ÖÐÎļþ','³ö´í');
return;else
file=[pathname,filename];
global S %ÉèÖÃÒ»¸öÈ«¾ÖªäÁ¿S£¬ª£´æ³õʼͼÏñ«¾¶£¬ÒÔªãÖ®ºóµÄ»¹Ô-²Ù¬ô S=file;
x=imread(file);
set(handles.axes1,'HandleVisibility','ON');axes(handles.axes1);imshow(x);
set(handles.axes1,'HandleVisibility','OFF');axes(handles.axes2);imshow(x);handles.img=x;
guidata(hObject,handles);end
%------function about_Callback(hObject, eventdata, handles)% hObject handle to about(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)
%------function save_Callback(hObject, eventdata, handles)% hObject handle to save(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)
% Hint: get(hObject,'Value')returns toggle state of radiobutton12 global T
axes(handles.axes2);T=getimage;prompt={'µôÕûª¶Êý'};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);
%------function uipanel4_SelectionChangeFcn(hObject, eventdata, handles)% hObject handle to uipanel4(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)
% Hint: get(hObject,'Value')returns toggle state of radiobutton16
global T
axes(handles.axes2);T=getimage;
x=rgb2gray(handles.img);%RGBͼÏñ¬ª»»Îª»Ò¶ÈͼÏñ imshow(x);handles.img=x;
guidata(hObject,handles);
/ 52 %---Executes on button pre in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global T
axes(handles.axes2);T=getimage;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);
% Hint: popupmenu 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
/ 52
%------function uipanel5_SelectionChangeFcn(hObject, eventdata, handles)%ͼÏñÂ˲¨ % hObject handle to uipanel5(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)str=get(hObject,'string');
/ 52 axes(handles.axes2);global T switch str
case '¬óÓÒ«-¬ª'
T=handles.img;
f=fliplr(handles.img);imshow(f);handles.img=f;
guidata(hObject,handles);
case 'ÉÏÏ«-¬ª' T=handles.img;
f=flipud(handles.img);imshow(f);handles.img=f;
guidata(hObject,handles);end
%---Executes on button pre in pushbutton7.function pushbutton7_Callback(hObject, eventdata, handles)% hObject handle to pushbutton7(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)axes(handles.axes2);
x=imhist(handles.img);%Öª«½Í¼Í³¼Æ x1=x(1:10:256);horz=1:10:256;bar(horz,x1);
axis([0 255 0 15000]);
set(handles.axes2,'xtick',0:50:255);set(handles.axes2,'ytick',0:2000:15000);
/ 52
%------function uipanel9_SelectionChangeFcn(hObject, eventdata, handles)% hObject handle to uipanel9(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)
global S %»¹Ô-¹¦ÄÜ axes(handles.axes2);y=imread(S);f=imshow(y);handles.img=y;
guidata(hObject,handles);
/ 52
%---Executes on button pre in pushbutton11.function pushbutton11_Callback(hObject, eventdata, handles)% hObject handle to pushbutton11(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global T
str=get(hObject,'string');axes(handles.axes2);
switch str
case'ÔöÇ¿'
T=getimage;prompt={'ÊäÈë²ÎÊý:'};defans={'1'};
p=inputdlg(prompt,'input',1,defans);p1=str2num(p{1});
f=immultiply(handles.img,p1);imshow(f);handles.img=f;
guidata(hObject,handles);
case'¼õÈõ' T=getimage;prompt={'ÊäÈë²ÎÊý:'};defans={'1'};
p=inputdlg(prompt,'input',1,defans);p1=str2num(p{1});
f=imdivide(handles.img,p1);imshow(f);handles.img=f;
guidata(hObject,handles);end
/ 52 %---Executes on button pre in pushbutton12.function pushbutton12_Callback(hObject, eventdata, handles)% hObject handle to pushbutton12(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)
axes(handles.axes2);y1=handles.img;
f=double(y1);% Êý¾ÝÀÐͬª»»£¬MATLAB²»Ö§³ÖͼÏñµÄÎÞ«ûºÅÕûÐ͵ļÆËã g=fft2(f);% ¸µÁ¢Ò¶ªä»» g=fftshift(g);% ¬ª»»Êý¾Ý¾ØÕó [M,N]=size(g);
nn=2;% ¶þ½¬©ÍÌØÎÖ˹(Butterworth)µÍͨÂ˲¨Æô d0=50;%½ØֹƵÂÊΪ50 m=fix(M/2);n=fix(N/2);for i=1:M
for j=1:N
d=sqrt((i-m)^2+(j-n)^2);
h=1/(1+0.414*(d/d0)^(2*nn));% ¼ÆËãµÍͨÂ˲¨Æô´«µÝº¯Êý result(i,j)=h*g(i,j);
end end
result=ifftshift(result);y2=ifft2(result);y3=uint8(real(y2));
imshow(y3);% ÏÔʾÂ˲¨´¦ÀíºóµÄͼÏñ
/ 52 %---Executes on button pre in pushbutton15.function pushbutton15_Callback(hObject, eventdata, handles)% hObject handle to pushbutton15(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)global T
axes(handles.axes2);T=getimage;
f=edge(handles.img,'canny');imshow(f);handles.img=f;
guidata(hObject,handles);
/ 52
%------function edit_Callback(hObject, eventdata, handles)% hObject handle to edit(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)
%------function tianjiazaosheng_Callback(hObject, eventdata, handles)% hObject handle to tianjiazaosheng(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)
%------function changyongchuli_Callback(hObject, eventdata, handles)% hObject handle to changyongchuli(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)
%------function tuxiangfanzhuan_Callback(hObject, eventdata, handles)% hObject handle to tuxiangfanzhuan(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)set(handles.pushbutton3,'Visible','on');if strcmp(get(gcbo, 'Checked'),'on')
set(handles.pushbutton3,'Visible','on');set(gcbo, 'Checked', 'off');
set(handles.pushbutton3,'Visible','off');else
set(gcbo, 'Checked', 'on');end
%------function ditonglvbochuli_Callback(hObject, eventdata, handles)% hObject handle to ditonglvbochuli(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)set(handles.pushbutton15,'Visible','on');if strcmp(get(gcbo, 'Checked'),'on')
set(handles.pushbutton15,'Visible','on');set(gcbo, 'Checked', 'off');
set(handles.pushbutton15,'Visible','off');else
set(gcbo, 'Checked', 'on');end
%------function tuxianglvbo_Callback(hObject, eventdata, handles)% hObject handle to tuxianglvbo(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)set(handles.radiobutton12,'Visible','on');if strcmp(get(gcbo, 'Checked'),'on')
set(handles.radiobutton12,'Visible','on');set(gcbo, 'Checked', 'off');
set(handles.radiobutton12,'Visible','off');else
set(gcbo, 'Checked', 'on');end
/ 52
%------function huidutxiang_Callback(hObject, eventdata, handles)% hObject handle to huidutxiang(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)set(handles.uipanel10,'Visible','on');if strcmp(get(gcbo, 'Checked'),'on')
set(handles.uipanel10,'Visible','on');set(gcbo, 'Checked', 'off');
set(handles.uipanel10,'Visible','off');else
set(gcbo, 'Checked', 'on');end
%------function zhifangtujunheng_Callback(hObject, eventdata, handles)% hObject handle to zhifangtujunheng(see GCBO)
% eventdata reservedto be defined in a future version of MATLAB % handles structure with handles and user data(see GUIDATA)set(handles.pushbutton8,'Visible','on');
/ 52
1. 7以上版本的matlab,控件pushbutton背景颜色修改以后并没有什么明显效果,只是按钮的边缘显示为所设置的背景颜色。 2 .凡是在属性对话框Property Inspector中可以设置的属性,都......
1.function [center, r] = solve_circle(pt1, pt2, pt3) 2.%Effect: solve the circle which acro points 'pt1', 'pt2' and 'pt3' 3.%Inputs: 4.%pt1, pt2, pt3: [x......
Matlab在控制工程中的应用摘要:简要介绍MATLAB软件及其控制系统工具箱的功能,并通过具体实例说明MATLAB软件在《机械控制工程基础》课程教学中的优越性,从多方面探讨在教学过程......
摘 要:随着科技的不断进步,数字技术得到广泛的发展和应用,现实生活中的很多信息都可以以数字形式的数据进行处理和储存,数字图像就是这种以数字形式进行储存和处理的图像。 关键......
基于MATLAB图像处理报告一、设计题目 图片叠加。二、设计要求 将一幅礼花图片和一幅夜景图片做叠加运算,使达到烟花夜景的美图效果。三、设计方案3.1、设计思路利用matlab强......
