首页 > 其他分享 >基于GUI的简易图像处理系统设计与实现

基于GUI的简易图像处理系统设计与实现

时间:2022-11-28 18:37:51浏览次数:53  
标签:set visible GUI uipanel hObject 简易 handles 图像处理 inv


自定义简易图像处理系统:综合此前内容得出该系统,系统功能包括:图像简单处理、图像的灰度变换、图像直方图均衡化和比特平面分层、空间域滤波、频率域滤波、彩色图像的空间域滤波。界面如图1:

基于GUI的简易图像处理系统设计与实现_简易图像处理系统

图 1

功能方向选择使用下拉列表的方式,通过选择下拉列表中的某一项,下拉列表下面的区域就会显示对应的功能模块。如图2、3:



基于GUI的简易图像处理系统设计与实现_简易图像处理系统_02


图 2


基于GUI的简易图像处理系统设计与实现_功能模块_03



图 3


该系统的使用:首先是点击读入图像,通过下拉列表选择对应的功能模块,在功能模块中选择所需的功能,按照要求输入对应所需的参数。最后点击“处理图像”,即可输出显示结果。如图4:

基于GUI的简易图像处理系统设计与实现_Matlab GUI编程_04

图 4

还可点击“保存”,保存输出结果到当前目录,如图5:

基于GUI的简易图像处理系统设计与实现_下拉列表_05

图 5

当在处理图像的过程中,“处理图像”的按钮不可以再次点击;“状态”由“空闲”变为“繁忙...”,处理结果后,“处理图像”按钮才变为可点击状态,“状态”由“繁忙...”变为“空闲”。在保存输出图像的过程中,“保存”按钮不可以点击,保存结束后才变为可点击状态。文件保存在当前目录下,同时文件名为了不与目录下的文件发生冲突,使用当前时间对文件进行命名。

系统内部图像处理的方法均使用此前内容介绍的程序。在这里这些程序就不特意贴出来了。完整程序,请查看附件。

系统界面设计方面:

使用guide命令,创建gui界面,使用组件“拖拽”的方式创建界面。

(1)界面初始化时,需要进行操作,可以在该自动生成的方法中进行设置:

function Demo_OpeningFcn(hObject, eventdata, handles, varargin)

(2)我想对于除拖拽外,在m文件的编程方面重要的是要学会如何通过句柄对组件相应属性进行设置。格式如下:

set(handles.axes1,'visible','off');

axes1 是 axes组件的Tag属性,一开始通过界面进行设置。在m 文件中我们就可以通过该属性获得对应组件,那么要设置动态属性时,就只要通过类似以上格式进行编写即可。handles.axes1 参数后面跟着需要设置的属性,而第3个参数则是对应的属性值。这样我们就学会了如何在程序中设置组件的属性。

(3)读取输入图像的程序(可进行图像文件的选择):

%读取输入图像
% --- Executes on button press in btn_imread.
function btn_imread_Callback(hObject, eventdata, handles)
% hObject handle to btn_imread (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global srcIma;

[filename, path] = uigetfile({'*.bmp; *.tif; *.jpg','图片类型(*.bmp; *.tif, *.jpg)'},'请选择图片');
if filename~=0
srcIma = imread([path,filename]);
else
return;
end
set(handles.axes1,'visible','on');
axes(handles.axes1);
imshow(srcIma);
end

注意:global关键字表示 srcIma变量为全局变量,允许在其他函数中使用该srcIma变量,使用前仍需使用 global

结果如图6:



基于GUI的简易图像处理系统设计与实现_jar_06


图 6


(4)警告框的使用:

warndlg('对比度拉伸区间不能为空','!! 警告提示 !!');


格式:warndlg(内容,标题);

对于不符合程序的输入,进行提示,如图7:



基于GUI的简易图像处理系统设计与实现_功能模块_07


图 7


(5)“下拉列表”修改选项时,“下拉列表”下的“功能模块”也发生对应的变化。这个是肿么做到的呢?我一开始是将几个面板都叠加在一起,不同面板负责不同的功能模块,在“下拉列表”该组件增加回调方法,修改选项,对应面板进行显示,而其他面板不进行显示。当然这种猜想是非常正确可行的。但是,在我们手动“拖拽”面板进行叠加的过程中会发现,面板不是真正意义上的叠加,而是面板“放到”了另外一个面板中,这样当对面板进行可见性(visible)设置时就会影响到其他面板,所以我们要另外寻找正确叠加的方式。通过网上资料的搜索,发现,可以对不同面板设置 position 属性,在不同面板在未叠加时,通过position属性,设置不同面板在相同位置,这样就可以达到我们想要的效果。操作如图6:



基于GUI的简易图像处理系统设计与实现_功能模块_08


图 6


关于面板切换的程序如下(对下拉列表设置回调方法):

(设置回调方法的方式如图7:

对组件右键,选择对应选项,就会自动生成对应的回调程序



基于GUI的简易图像处理系统设计与实现_功能模块_09


图 7


% --- Executes on selection change in popupmenu_func_select.
function popupmenu_func_select_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu_func_select (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc
% Hints: contents = get(hObject,'String') returns popupmenu_func_select contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu_func_select
type=get(handles.popupmenu_func_select,'value');
if type==1 %选择“图像简单处理”
set(handles.uipanel_simple, 'visible', 'on');
set(handles.uipanel_huiduchange, 'visible','off');
set(handles.uipanel_histPanel, 'visible','off');
set(handles.uipanel_kjArea,'visible','off');
set(handles.uipanel_plArea, 'visible', 'off');
set(handles.uipanel_colorImaKJArea, 'visible','off');
elseif type==2 %选择“图像的灰度变换”
set(handles.uipanel_simple, 'visible', 'off');
set(handles.uipanel_huiduchange, 'visible','on');
set(handles.uipanel_histPanel, 'visible','off');
set(handles.uipanel_kjArea,'visible','off');
set(handles.uipanel_plArea, 'visible', 'off');
set(handles.uipanel_colorImaKJArea, 'visible','off');
elseif type==3 %选择“图像的直方图均衡化和比特平面分层”
set(handles.uipanel_simple, 'visible', 'off');
set(handles.uipanel_huiduchange, 'visible','off');
set(handles.uipanel_histPanel, 'visible','on');
set(handles.uipanel_kjArea,'visible','off');
set(handles.uipanel_plArea, 'visible', 'off');
set(handles.uipanel_colorImaKJArea, 'visible','off');
elseif type==4 %选择“空间域滤波”
set(handles.uipanel_simple,'visible','off');
set(handles.uipanel_huiduchange, 'visible','off');
set(handles.uipanel_histPanel, 'visible','off');
set(handles.uipanel_kjArea,'visible','on');
set(handles.uipanel_plArea, 'visible', 'off');
set(handles.uipanel_colorImaKJArea, 'visible','off');
elseif type==5 %选择“频率域滤波”
set(handles.uipanel_simple,'visible', 'off');
set(handles.uipanel_huiduchange, 'visible','off');
set(handles.uipanel_histPanel, 'visible','off');
set(handles.uipanel_kjArea, 'visible', 'off');
set(handles.uipanel_plArea, 'visible', 'on');
set(handles.uipanel_colorImaKJArea, 'visible','off');
elseif type==6 %选择“彩色图像的空间域滤波”
set(handles.uipanel_simple,'visible', 'off');
set(handles.uipanel_huiduchange, 'visible','off');
set(handles.uipanel_histPanel, 'visible','off');
set(handles.uipanel_kjArea, 'visible', 'off');
set(handles.uipanel_plArea, 'visible', 'off');
set(handles.uipanel_colorImaKJArea, 'visible','on');
end
end


(6)“状态”信息的显示:

set(handles.btn_imshow,'enable','off');
set(handles.text_state,'string','繁忙中...');
pause(.005);
...
set(handles.btn_imshow,'enable','on');
set(handles.text_state,'string','空闲');

“处理图像”按钮被点击后,按钮状态发生改变,“状态”信息发生改变。


附件:


function varargout = Demo(varargin)
% DEMO M-file for Demo.fig
% DEMO, by itself, creates a new DEMO or raises the existing
% singleton*.
%
% H = DEMO returns the handle to a new DEMO or the handle to
% the existing singleton*.
%
% DEMO('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DEMO.M with the given input arguments.
%
% DEMO('Property','Value',...) creates a new DEMO or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Demo_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Demo_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Demo

% Last Modified by GUIDE v2.5 26-Nov-2015 18:39:11

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Demo_OpeningFcn, ...
'gui_OutputFcn', @Demo_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 code - DO NOT EDIT
end

% --- Executes just before Demo is made visible.
function Demo_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% 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 Demo (see VARARGIN)

% Choose default command line output for Demo
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Demo wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% Matlab运行gui时,出现问题:http://www.ilovematlab.cn/thread-86285-1-1.html
set(handles.axes1,'visible','off');
set(handles.axes2,'visible','off');
set(handles.uipanel_simple,'visible','on');
set(handles.uipanel_huiduchange, 'visible','off');
set(handles.uipanel_histPanel, 'visible','off');
set(handles.uipanel_kjArea, 'visible','off');
set(handles.uipanel_plArea, 'visible','off');
set(handles.uipanel_colorImaKJArea, 'visible','off');

end

% --- Outputs from this function are returned to the command line.
function varargout = Demo_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 command line output from handles structure
varargout{1} = handles.output;
end

%读取输入图像
% --- Executes on button press in btn_imread.
function btn_imread_Callback(hObject, eventdata, handles)
% hObject handle to btn_imread (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global srcIma;

[filename, path] = uigetfile({'*.bmp; *.tif; *.jpg','图片类型(*.bmp; *.tif, *.jpg)'},'请选择图片');
if filename~=0
srcIma = imread([path,filename]);
else
return;
end
set(handles.axes1,'visible','on');
axes(handles.axes1);
imshow(srcIma);
end

%显示处理后的输出图像
% --- Executes on button press in btn_imshow.
function btn_imshow_Callback(hObject, eventdata, handles)
% hObject handle to btn_imshow (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc


set(handles.btn_imshow,'enable','off');
set(handles.text_state,'string','繁忙中...');
pause(.005);

global srcIma;
global im;
hasRes=0;
if strcmp(get(handles.uipanel_simple, 'visible'),'on')
if get(handles.rb_readshow, 'value')
im=readShow(); %读取显示图像
hasRes=1;
elseif get(handles.rb_cutIma, 'value')
if get(handles.edit_cut,'string')
str=get(handles.edit_cut,'string');
n=str2double(str);
if n<0
n=0;
elseif n>1
n=1;
end
else
n=1;
end

im=cutIma(n); %截取图像
hasRes=1;
end
end
if strcmp(get(handles.uipanel_huiduchange, 'visible'),'on')
if get(handles.rb_rotation, 'value')
if get(handles.edit_rotateAngle,'string')
str=get(handles.edit_rotateAngle,'string');
angle=str2double(str);
else
angle=0;
end
im=myRotation(angle); %旋转图像
hasRes=1;
elseif get(handles.rb_fanse,'value')
im=myFanse();
hasRes=1;
elseif get(handles.rb_contr_conver,'value')
if size(srcIma,3)==1
warndlg('请输入彩色图像','!! 警告提示 !!');
else
if get(handles.edit_contr_mi,'string')
if get(handles.edit_contr_ma,'string')
str1=get(handles.edit_contr_mi,'string');
str2=get(handles.edit_contr_ma,'string');

mi=str2double(str1);
ma=str2double(str2);
im=myContrConver(mi,ma);
hasRes=1;
else
warndlg('对比度拉伸区间不能为空','!! 警告提示 !!');
end
else
%msgbox('对比度拉伸区间不能为空','警告提示');
warndlg('对比度拉伸区间不能为空','!! 警告提示 !!');
end
end
end
end

if strcmp(get(handles.uipanel_histPanel, 'visible'),'on')
if get(handles.rb_hist, 'value')
[im T]=JunHengHua();
hasRes=1;
elseif get(handles.rb_bitPanel, 'value')
if size(srcIma,3)~=1
warndlg('请输入灰度色图像','!! 警告提示 !!');
else
if get(handles.edit5, 'string')
str=get(handles.edit5, 'string');
pos=str2double(str);
if pos <= 0 || pos >8
warndlg('输入的比特层数应满足1~8这个范围','!! 警告提示 !!');
else
im=get8BitPM2(pos);
hasRes=1;
end
else
warndlg('请输入比特层数','!! 警告提示 !!');
end
end
end
end

if strcmp(get(handles.uipanel_kjArea, 'visible'),'on')
if size(srcIma,3)~=1
warndlg('请输入灰度色图像','!! 警告提示 !!');
else
if get(handles.rb_gsfilter, 'value')
im=mySpatialFilter('高斯均值滤波器',[3 3]);
hasRes=1;
elseif get(handles.rb_centerFilter, 'value')
im=mySpatialFilter('中值滤波器',[3 3]);
hasRes=1;
elseif get(handles.rb_maxFilter, 'value')
im=mySpatialFilter('最大值滤波器',[3 3]);
hasRes=1;
elseif get(handles.rb_minFilter, 'value')
im=mySpatialFilter('最小值滤波器',[3 3]);
hasRes=1;
elseif get(handles.rb_sobel, 'value')
im=mySobelGradCal();
hasRes=1;
end
end
end
if strcmp(get(handles.uipanel_plArea, 'visible'),'on')
if size(srcIma,3)~=1
warndlg('请输入灰度色图像','!! 警告提示 !!');
else
if get(handles.rb_glpf, 'value')
if get(handles.edit_D0, 'string')
str=get(handles.edit_D0, 'string');
D0=str2double(str);
im=myGLPF(D0);
hasRes=1;
else
warndlg('请输入截止频率','!! 警告提示 !!');
end
end
end
end

if strcmp(get(handles.uipanel_colorImaKJArea, 'visible'),'on')
if size(srcIma,3)==1
warndlg('请输入彩色图像','!! 警告提示 !!');
else
if get(handles.rb_rgbTohsi, 'value')
im=RGBtoHSI();
hasRes=1;
elseif get(handles.rb_hsiTorgb, 'value')
im=HSItoRGB();
hasRes=1;
elseif get(handles.rb_mySpatialFilter, 'value')
im=useHSIGS();
hasRes=1;
end
end
end

if hasRes==1
set(handles.axes2,'visible','on');
axes(handles.axes2);
imshow(im);
end

set(handles.btn_imshow,'enable','on');
set(handles.text_state,'string','空闲');
end

% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
end

% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: listbox 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
end


% --- Executes on selection change in popupmenu_func_select.
function popupmenu_func_select_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu_func_select (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc
% Hints: contents = get(hObject,'String') returns popupmenu_func_select contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu_func_select
type=get(handles.popupmenu_func_select,'value');
if type==1 %选择“图像简单处理”
set(handles.uipanel_simple, 'visible', 'on');
set(handles.uipanel_huiduchange, 'visible','off');
set(handles.uipanel_histPanel, 'visible','off');
set(handles.uipanel_kjArea,'visible','off');
set(handles.uipanel_plArea, 'visible', 'off');
set(handles.uipanel_colorImaKJArea, 'visible','off');
elseif type==2 %选择“图像的灰度变换”
set(handles.uipanel_simple, 'visible', 'off');
set(handles.uipanel_huiduchange, 'visible','on');
set(handles.uipanel_histPanel, 'visible','off');
set(handles.uipanel_kjArea,'visible','off');
set(handles.uipanel_plArea, 'visible', 'off');
set(handles.uipanel_colorImaKJArea, 'visible','off');
elseif type==3 %选择“图像的直方图均衡化和比特平面分层”
set(handles.uipanel_simple, 'visible', 'off');
set(handles.uipanel_huiduchange, 'visible','off');
set(handles.uipanel_histPanel, 'visible','on');
set(handles.uipanel_kjArea,'visible','off');
set(handles.uipanel_plArea, 'visible', 'off');
set(handles.uipanel_colorImaKJArea, 'visible','off');
elseif type==4 %选择“空间域滤波”
set(handles.uipanel_simple,'visible','off');
set(handles.uipanel_huiduchange, 'visible','off');
set(handles.uipanel_histPanel, 'visible','off');
set(handles.uipanel_kjArea,'visible','on');
set(handles.uipanel_plArea, 'visible', 'off');
set(handles.uipanel_colorImaKJArea, 'visible','off');
elseif type==5 %选择“频率域滤波”
set(handles.uipanel_simple,'visible', 'off');
set(handles.uipanel_huiduchange, 'visible','off');
set(handles.uipanel_histPanel, 'visible','off');
set(handles.uipanel_kjArea, 'visible', 'off');
set(handles.uipanel_plArea, 'visible', 'on');
set(handles.uipanel_colorImaKJArea, 'visible','off');
elseif type==6 %选择“彩色图像的空间域滤波”
set(handles.uipanel_simple,'visible', 'off');
set(handles.uipanel_huiduchange, 'visible','off');
set(handles.uipanel_histPanel, 'visible','off');
set(handles.uipanel_kjArea, 'visible', 'off');
set(handles.uipanel_plArea, 'visible', 'off');
set(handles.uipanel_colorImaKJArea, 'visible','on');
end
end

% --- Executes during object creation, after setting all properties.
function popupmenu_func_select_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu_func_select (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% 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
end

%exp1_1
%输入图像和显示图像
function resIma = readShow()
global srcIma;
resIma=srcIma;
end

%exp1_3
%参数n表示剪切原图像的n
function resIma=cutIma(n)
global srcIma;
ima=srcIma;
ima=double(ima);
swh=size(ima);
sh=swh(:,1);
sw=swh(:,2);
dh=round(sh*n);
dw=round(sw*n);
resIma=ima(1:dh,1:dw);
resIma=uint8(resIma);
end

%exp2_1
%图像的旋转
function resIma=myRotation(angle)
global srcIma;
ima=srcIma;
ima=double(ima);
[r,c,h]=size(ima);

%将角度单位化为弧度单位
R=-angle*pi/180;
%这样的宽高会出现BUG,原因不明
%h2 = ceil(r*cos(R)+c*sin(R));
%w2 = ceil(r*sin(R)+c*cos(R));

%将对角线作为旋转后图像的宽高
h2=ceil(sqrt(r*r+c*c));
w2=h2;

resIma=zeros(h2,w2,h);

%旋转时x y的偏移量
dx=-0.5*w2*cos(R)-0.5*h2*sin(R)+0.5*c;
dy=0.5*w2*sin(R)-0.5*h2*cos(R)+0.5*r;

%采用反向映射
for i=1:h
for x=1:h2
for y=1:w2
%由结果图像的坐标 得出 原来图像的坐标
x0=ceil(x*cos(R)+y*sin(R)+dx);
y0=ceil(-x*sin(R)+y*cos(R)+dy);

if x0>0 && x0<=r && y0>0 && y0<=c
resIma(x,y,i)=ima(x0,y0,i);
end
end
end
end
resIma=uint8(resIma);
end

%exp2_2
%图像的反色
function resIma=myFanse()
global srcIma;
ima=srcIma;
ima = double(ima);
[r c m] = size(ima);
resIma = zeros(r,c,m);
for i=1:m
for j=1:r
for k=1:c
resIma(j,k,i)=255-ima(j,k,i); %由最高灰度级减去原坐标位置的灰度级进行反色
end
end
end
resIma = uint8(resIma);
end

%exp2_3
%图像的对比度拉伸
function resIma=myContrConver(mi, ma)
global srcIma;
ima=srcIma;
ima = double(ima);
[r c m] = size(ima);
resIma = zeros(r,c,m);
%for i=1:m
% for j=1:r
% for k=1:c
% im2(j,k,i)=min+(im1(j,k,i)-minR)/(maxR-minR)*(max-min);
% end
% end
%end

%先对原图像的 灰度级 进行归一化,再由归一化的区间缩放至所需区间
tempIma1=ima(:,:,1);
tempIma2=ima(:,:,2);
tempIma3=ima(:,:,3);
resIma(:,:,1)=mi+(ima(:,:,1)-min(tempIma1(:)))/(max(tempIma1(:))-min(tempIma1(:)))*(ma-mi);
resIma(:,:,2)=mi+(ima(:,:,2)-min(tempIma2(:)))/(max(tempIma2(:))-min(tempIma2(:)))*(ma-mi);
resIma(:,:,3)=mi+(ima(:,:,3)-min(tempIma3(:)))/(max(tempIma3(:))-min(tempIma3(:)))*(ma-mi);

resIma=uint8(resIma);
end

%exp3_2
% im2表示均衡化的图像,T表示均衡化的变换函数
function [resIma,T]=JunHengHua()
global srcIma;
ima=srcIma;
[r c]=size(ima);
ima=double(ima(:)); %将其转化为行向量,易于下面对灰度级的查找
resIma=zeros(r*c,1); %对应转化后的im1
L=256;
im1_H=zeros(1,L); %原图像的直方图
%im2_H=im1_H;
H_sum=0; %累计原图像的直方图
T=im1_H;
for k=1:L
tempK=k-1;
posK=find(ima==tempK); %寻找对应灰度值在原图像中的位置
im1_H(k)=length(posK)/(r*c); %原图像的直方图,并进行归一化
H_sum=H_sum+im1_H(k); %以求和代替积分,求累计原图像的直方图
T(k)=(L-1)*H_sum; %求得原图像均衡化过程的变化函数
if (T(k)-floor(T(k))) > 0.5 %进行四舍五入操作
resIma(posK)=ceil(T(k)); %得到变换后的灰度,赋值给原位置
else
resIma(posK)=floor(T(k));
end
end
resIma=uint8(reshape(resIma,r,c)); %将im2转为r行c列 无符号8位的矩阵
end

%exp3_3
%图像的比特平面分层
function resIma = get8BitPM2(pos)
global srcIma;
ima=srcIma;
[r c] = size(ima);
im1 = dec2bin(ima(:),8); %将原图像的矩阵转化为列向量,并且将向量的各个元素转化为8位二进制数

tempA=bin2dec(im1(:,pos))*255; %其中 im1(:,pos)表示取 im1中 所有行的元素的第 pos 位,再将其转化为 十进制数。此时该数为0或1,将灰度进行拉伸,变换到0或255
%即得第 pos 比特平面
tempA=reshape(tempA,r,c); %将A转化为r行 c列的矩阵

resIma=tempA;
end

%exp4_1
%pho 表示输入图像的名称
%filter_type 所选择的滤波器类型。有:高斯均值滤波器、中值滤波器、最大值滤波器、最小值滤波器
%filter_size 定义滤波器大小,如 filter_size=[3 3];
function resIma = mySpatialFilter(filter_type, filter_size)
global srcIma;
ima=srcIma;
ima=double(ima); %转化为双精度类型
[r c]=size(ima); %读取原图像的行数和列数

if strcmp(filter_type, '高斯均值滤波器')
filter_size=[3 3];
end
m=filter_size(1); %读取设置的滤波器模板的大小
n=filter_size(2);

rs=r+m-1; %计算边界填充后的图像大小(填充模板大小)
cs=c+n-1;
tIma=zeros(rs,cs); %创建一个边界填充后的矩阵
tIma((m-1)/2+1:rs-(m-1)/2, (n-1)/2+1:cs-(n-1)/2)=ima; %矩阵中间填充原图像灰度级

%以最邻近的边界作为边界填充的灰度值
%先填充上边界
for i=1:(m-1)/2
tIma(i,(n-1)/2+1:cs-(n-1)/2)=ima(1);
end
%填充下边界
for i=rs-(m-1)/2+1:rs
tIma(i,(n-1)/2+1:cs-(n-1)/2)=ima(r);
end
%填充左边界
for i=1:(n-1)/2
tIma(:,i)=tIma(:,(n-1)/2+1);
end
%填充右边界
for i=cs-(n-1)/2+1:cs
tIma(:,i)=tIma(:,cs-(n-1)/2);
end

rIma=zeros(rs,cs);
%遍历该矩阵,套用滤波模板
for i=1:rs-m+1
for j=1:cs-n+1
%读取模板覆盖处的图像
tempIma=tIma(i:i+m-1, j:j+n-1);
%计算该模板覆盖的中心位置
centerR=i+(m-1)/2;
centerC=j+(m-1)/2;
switch filter_type
case '高斯均值滤波器'
%定义3 x 3的高斯均值滤波器模板
module=[1 2 1; 2 4 2; 1 2 1];
module=module(:)';
%为邻域以该模板为系数进行加权求平均,得出的值赋值得邻域中心
rIma(centerR,centerC)=module*tempIma(:)/sum(module);
case '中值滤波器'
%为邻域内的值排序,求得中值,将该值赋值给邻域中心
tempIma=sort(tempIma(:));
rIma(centerR,centerC)=tempIma((length(tempIma)+1)/2,1);
case '最大值滤波器'
%求邻域中的最大灰度值,将该值赋值给邻域中心
rIma(centerR,centerC)=max(tempIma(:));
case '最小值滤波器'
%求邻域中的最小灰度值,将该值赋值给邻域中心
rIma(centerR,centerC)=min(tempIma(:));
otherwise
error('不存在该滤波器');
end
end
end
%去除原先填充的边界,得出最终结果
resIma=rIma((m-1)/2+1:rs-(m-1)/2, (n-1)/2+1:cs-(n-1)/2);
resIma=uint8(resIma);
end

%exp4_2
%mySobelGradCal函数使用Sobel梯度算子作为微分滤波器模板,对输入图像进行锐化操作
%参数 pho 表示输入图像的路径,如 EXP4-3.tif
function grad2=mySobelGradCal()
global srcIma;
ima=srcIma;
ima=double(ima); %转化为双精度类型对图像进行操作
[r c]=size(ima); %获取输入的图像的行数和列数
m=3; %Sobel梯度算子的梯度模板大小(行)
n=3; %Sobel梯度算子的梯度模板大小(列)
%边界填充后的大小
rs=r+m-1;
cs=c+n-1;
%创建边界填充的矩阵
tIma=zeros(rs,cs);
%将输入图像填充在该矩阵的中心
tIma((m-1)/2+1:rs-(m-1)/2,(n-1)/2+1:cs-(n-1)/2)=ima;

for i=1:(m-1)/2
%为矩阵填充下边界,根据输入图像的下边界进行填充
tIma(i,(n-1)/2+1:cs-(n-1)/2)=ima(1,:);
%为矩阵填充上边界,根据输入图像的上边界进行填充
tIma(rs-i-1,(n-1)/2+1:cs-(n-1)/2)=ima(r,:);
end

for i=1:(n-1)/2
%为矩阵填充左边界,根据矩阵已填充的左边一列进行填充
tIma(:,i)=tIma(:,(n-1)/2+1);
%为矩阵填充右边界,根据矩阵已填充的右边一列进行填充
tIma(:,cs-i-1)=tIma(:,cs-(n-1)/2);
end

grad2=zeros(rs,cs);
%以可以让Sobel梯度模板覆盖tIma对tIma进行遍历
for i=1:rs-m+1
for j=1:cs-n+1
%取出当前被梯度模板覆盖的区域(灰度值)——邻域
tempIma=tIma(i:i+m-1,j:j+n-1);
%求邻域的中心
centerR=i+(m-1)/2;
centerC=j+(n-1)/2;
%使用Sobel梯度算子进行计算
x=abs(sum([tempIma(3,:) tempIma(3,2)])-sum([tempIma(1,:) tempIma(1,2)]));
y=abs(sum([tempIma(:,3); tempIma(2,3)])-sum([tempIma(:,1); tempIma(2,1)]));
grad2(centerR,centerC)=x+y;
end
end
%截取出grad2中中心部分即为输出结果
grad2=grad2((m-1)/2+1:rs-(m-1)/2,(n-1)/2+1:cs-(n-1)/2);
grad2=uint8(grad2);
end

%exp5_1
%pho参数为输入图像的路径
%D0参数为截止频率
function im2 = myGLPF(D0)
global srcIma;
ima=srcIma;

%得到高斯低通滤波器
[r c] = size(ima); %获取输入图像的行和列
D = zeros(r,c); %D(u,v)是距频率矩形中心的距离
for i=1:r
for j=1:c
D(i,j)=sqrt((i-r/2)^2+(j-c/2)^2);
end
end
H=exp(-(D.^2)/(2*D0*D0)); %计算滤波器,得到高斯低通滤波器

F=fft2(ima,size(H,1),size(H,2)); %对原图像进行傅里叶变换
F=fftshift(F); %对傅里叶变换后的F进行中心移位
F1=ifft2(ifftshift(H.*F)); %对中心移位后的F使用高斯低通滤波器后进行反FFT移动(记得一定要进行一次反FFT移动,否则输入结果图片背景会变暗),并进行反变换
im2=real(F1); %从结果中获取幅度(或称 频率谱)
im2=uint8(im2);
end

%exp6_1
% pho 表示输入图像路径(包括图像名+后缀)
function hsi = RGBtoHSI()
global srcIma;
ima=srcIma;
rgb=double(ima); %将图像转为双精度类型
rgb=rgb/255; %归一化
R=rgb(:,:,1); %提取第1个通道
G=rgb(:,:,2); %提取第2个通道
B=rgb(:,:,3); %提取第3个通道

angle=(acos(1/2*((R-G)+(R-B))./sqrt((R-G).^2+(R-B).*(G-B))))*180/pi;
H=angle; %计算H(色调)通道
H(B>G)=360-H(B>G); %将 B>G 对应位置上的值赋值为 2*pi - angle
S=1-3./(R+G+B+eps).*min(min(R,G),B); %计算S(色饱和度)通道
I=1/3*(R+G+B+eps); %计算I(亮度)通道

hsi=cat(3,H/360,S,I); %合成3个通道,为HSI图像
end

%exp6_2
% hsi 表示HSI空间图像
function rgb = HSItoRGB()
global srcIma;
hsi=srcIma;
%ima=imread(pho);
hsi=double(hsi); %转化为双精度类型
[r c m]=size(hsi); %计算hsi的行列和维度
H=hsi(:,:,1); %提取H分量
S=hsi(:,:,2); %提取S分量
I=hsi(:,:,3); %提取I分量
H=H*360; %将色调值变为原来的范围[0°,360°]
R=zeros(r,c); %R分量
G=zeros(r,c); %G分量
B=zeros(r,c); %B分量

inv=0<=H & H<120; %RG扇区(inv是H中值在RG扇区的位置)
B(inv)=I(inv).*(1-S(inv)); %根据在RG扇区时的计算公式计算B分量
R(inv)=I(inv).*(1+S(inv).*cosd(H(inv))./cosd(60-H(inv))); %根据在RG扇区时的计算公式计算R分量
G(inv)=3*I(inv)-(R(inv)+B(inv)); %根据在RG扇区时的计算公式计算G分量

inv=120<=H & H<240; %GB扇区(inv是H中值在GB扇区的位置)
H(inv)=H(inv)-120;
R(inv)=I(inv).*(1-S(inv)); %根据在GB扇区时的计算公式计算R分量
G(inv)=I(inv).*(1+S(inv).*cosd(H(inv))./cosd(60-H(inv))); %根据在GB扇区时的计算公式计算G分量
B(inv)=3*I(inv)-(R(inv)+G(inv)); %根据在GB扇区时的计算公式计算B分量

inv=240<=H & H<360; %BR扇区(inv是H中值在BR扇区的位置)
H(inv)=H(inv)-240;
G(inv)=I(inv).*(1-S(inv)); %根据在BR扇区时的计算公式计算G分量
B(inv)=I(inv).*(1+S(inv).*cosd(H(inv))./cosd(60-H(inv))); %根据在BR扇区时的计算公式计算B分量
R(inv)=3*I(inv)-(G(inv)+B(inv)); %根据在BR扇区时的计算公式计算R分量

%将各个分量的灰度级拉伸到256个灰度级上
R=R*255;
G=G*255;
B=B*255;
rgb=cat(3,R,G,B); %合成彩色图像
rgb=uint8(rgb); %转为8位无符号整型
end

%exp6_3
%filter_type 所选择的滤波器类型。有:3x3高斯均值滤波器、5x5高斯均值滤波器、中值滤波器、最大值滤波器、最小值滤波器
%filter_size 定义滤波器大小,如 filter_size=[3 3];
function ima2 = mySpatialFilter2(ima, filter_type, filter_size)
%ima=imread(pho); %根据路径读取原图像
ima=double(ima); %转化为双精度类型
[r c]=size(ima); %读取原图像的行数和列数

%设置高斯均值滤波器只有3x3或5x5大小的
if strcmp(filter_type, '3x3高斯均值滤波器')
filter_size=[3 3];
end

if strcmp(filter_type, '5x5高斯均值滤波器')
filter_size=[5 5];
end
m=filter_size(1); %读取设置的滤波器模板的大小
n=filter_size(2);

rs=r+m-1; %计算边界填充后的图像大小(填充模板大小)
cs=c+n-1;
tIma=zeros(rs,cs); %创建一个边界填充后的矩阵
tIma((m-1)/2+1:rs-(m-1)/2, (n-1)/2+1:cs-(n-1)/2)=ima; %矩阵中间填充原图像灰度级

%以最邻近的边界作为边界填充的灰度值
%先填充上边界
for i=1:(m-1)/2
tIma(i,(n-1)/2+1:cs-(n-1)/2)=ima(1);
end
%填充下边界
for i=rs-(m-1)/2+1:rs
tIma(i,(n-1)/2+1:cs-(n-1)/2)=ima(r);
end
%填充左边界
for i=1:(n-1)/2
tIma(:,i)=tIma(:,(n-1)/2+1);
end
%填充右边界
for i=cs-(n-1)/2+1:cs
tIma(:,i)=tIma(:,cs-(n-1)/2);
end

rIma=zeros(rs,cs);
%遍历该矩阵,套用滤波模板
for i=1:rs-m+1
for j=1:cs-n+1
%读取模板覆盖处的图像
tempIma=tIma(i:i+m-1, j:j+n-1);
%计算该模板覆盖的中心位置
centerR=i+(m-1)/2;
centerC=j+(m-1)/2;
switch filter_type
case '3x3高斯均值滤波器'
%定义3 x 3的高斯均值滤波器模板
module=[1 2 1; 2 4 2; 1 2 1];
module=module(:)';
%为邻域以该模板为系数进行加权求平均,得出的值赋值得邻域中心
rIma(centerR,centerC)=module*tempIma(:)/sum(module);
case '5x5高斯均值滤波器'
%定义5 x 5的高斯均值滤波器模板
module=[1 2 3 2 1; 2 5 6 5 2; 3 6 8 6 3; 2 5 6 5 2; 1 2 3 2 1];
module=module(:)';
%为邻域以该模板为系数进行加权求平均,得出的值赋值得邻域中心
rIma(centerR,centerC)=module*tempIma(:)/sum(module);
case '中值滤波器'
%为邻域内的值排序,求得中值,将该值赋值给邻域中心
tempIma=sort(tempIma(:));
rIma(centerR,centerC)=tempIma((length(tempIma)+1)/2,1);
case '最大值滤波器'
%求邻域中的最大灰度值,将该值赋值给邻域中心
rIma(centerR,centerC)=max(tempIma(:));
case '最小值滤波器'
%求邻域中的最小灰度值,将该值赋值给邻域中心
rIma(centerR,centerC)=min(tempIma(:));
otherwise
error('不存在该滤波器');
end
end
end
%去除原先填充的边界,得出最终结果
ima2=rIma((m-1)/2+1:rs-(m-1)/2, (n-1)/2+1:cs-(n-1)/2);
ima2=uint8(ima2);
end

% hsi 表示HSI空间图像
function rgb = HSItoRGB2(hsi)
%ima=imread(pho);
hsi=double(hsi); %转化为双精度类型
[r c m]=size(hsi); %计算hsi的行列和维度
H=hsi(:,:,1); %提取H分量
S=hsi(:,:,2); %提取S分量
I=hsi(:,:,3); %提取I分量
H=H*360; %将色调值变为原来的范围[0°,360°]
R=zeros(r,c); %R分量
G=zeros(r,c); %G分量
B=zeros(r,c); %B分量

inv=0<=H & H<120; %RG扇区(inv是H中值在RG扇区的位置)
B(inv)=I(inv).*(1-S(inv)); %根据在RG扇区时的计算公式计算B分量
R(inv)=I(inv).*(1+S(inv).*cosd(H(inv))./cosd(60-H(inv))); %根据在RG扇区时的计算公式计算R分量
G(inv)=3*I(inv)-(R(inv)+B(inv)); %根据在RG扇区时的计算公式计算G分量

inv=120<=H & H<240; %GB扇区(inv是H中值在GB扇区的位置)
H(inv)=H(inv)-120;
R(inv)=I(inv).*(1-S(inv)); %根据在GB扇区时的计算公式计算R分量
G(inv)=I(inv).*(1+S(inv).*cosd(H(inv))./cosd(60-H(inv))); %根据在GB扇区时的计算公式计算G分量
B(inv)=3*I(inv)-(R(inv)+G(inv)); %根据在GB扇区时的计算公式计算B分量

inv=240<=H & H<360; %BR扇区(inv是H中值在BR扇区的位置)
H(inv)=H(inv)-240;
G(inv)=I(inv).*(1-S(inv)); %根据在BR扇区时的计算公式计算G分量
B(inv)=I(inv).*(1+S(inv).*cosd(H(inv))./cosd(60-H(inv))); %根据在BR扇区时的计算公式计算B分量
R(inv)=3*I(inv)-(G(inv)+B(inv)); %根据在BR扇区时的计算公式计算R分量

%将各个分量的灰度级拉伸到256个灰度级上
R=R*255;
G=G*255;
B=B*255;
rgb=cat(3,R,G,B); %合成彩色图像
rgb=uint8(rgb); %转为8位无符号整型
end

% pho 表示输入图像路径(包括图像名+后缀)
% 该函数输入原图像,将原图像转化为HSI彩色图像,在HSI空间使用5x5高斯均值滤波器,再将结果转为RGB输出
function rgb=useHSIGS()
global srcIma;
ima=srcIma;

hsi=RGBtoHSI(); %将输入图像由RGB空间向HSI空间转化
%在使用5x5高斯均值滤波器前,需要将I分量拉伸到[0 255]灰度区间
%为什么只对I分量进行操作呢?I分量与图像的彩色信息无关。
%注意差异性:
%RGB空间中的平均是不同彩色的平均
%HIS空间中仅仅是强度的平均,色调H和饱和度S均保持不变
hsi(:,:,3)=mySpatialFilter2(hsi(:,:,3)*255,'5x5高斯均值滤波器',[5 5]);
hsi(:,:,3)=hsi(:,:,3)/255; %将I分量再转回[0 1]区间
rgb=HSItoRGB2(hsi); %由HSI空间再转为RGB空间
end


function edit_cut_Callback(hObject, eventdata, handles)
% hObject handle to edit_cut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_cut as text
% str2double(get(hObject,'String')) returns contents of edit_cut as a double
end

% --- Executes during object creation, after setting all properties.
function edit_cut_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_cut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit 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
end




function edit_rotateAngle_Callback(hObject, eventdata, handles)
% hObject handle to edit_rotateAngle (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_rotateAngle as text
% str2double(get(hObject,'String')) returns contents of edit_rotateAngle as a double
end

% --- Executes during object creation, after setting all properties.
function edit_rotateAngle_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_rotateAngle (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit 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
end


function edit_contr_mi_Callback(hObject, eventdata, handles)
% hObject handle to edit_contr_mi (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_contr_mi as text
% str2double(get(hObject,'String')) returns contents of edit_contr_mi as a double
end

% --- Executes during object creation, after setting all properties.
function edit_contr_mi_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_contr_mi (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit 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
end


function edit_contr_ma_Callback(hObject, eventdata, handles)
% hObject handle to edit_contr_ma (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_contr_ma as text
% str2double(get(hObject,'String')) returns contents of edit_contr_ma as a double
end

% --- Executes during object creation, after setting all properties.
function edit_contr_ma_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_contr_ma (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit 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
end


function edit5_Callback(hObject, eventdata, handles)
% hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit5 as text
% str2double(get(hObject,'String')) returns contents of edit5 as a double
end

% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit 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
end



function edit_filterSize1_Callback(hObject, eventdata, handles)
% hObject handle to edit_filterSize1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_filterSize1 as text
% str2double(get(hObject,'String')) returns contents of edit_filterSize1 as a double
end

% --- Executes during object creation, after setting all properties.
function edit_filterSize1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_filterSize1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit 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
end



function edit_D0_Callback(hObject, eventdata, handles)
% hObject handle to edit_D0 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_D0 as text
% str2double(get(hObject,'String')) returns contents of edit_D0 as a double
end

% --- Executes during object creation, after setting all properties.
function edit_D0_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit_D0 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit 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
end


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im;
set(handles.pushbutton3,'enable','off');
pause(.05);

str=datestr(now,'mmmmdd_yyyyHH_MM_SS_FFFAM');
imwrite(im,num2str([str,'.tif']));
set(handles.pushbutton3,'enable','on');
end


标签:set,visible,GUI,uipanel,hObject,简易,handles,图像处理,inv
From: https://blog.51cto.com/u_15894233/5893355

相关文章