2019.4.7 注:本次的代码和设计GUI代码都是参考 刘衍琦和詹福宇所著的《MATLAB图形和视频处理实用案例详解》
1、在理解了小波图像融合的代码后我开始按照网上的GUI设计程序和书上给的代码进行小波变换的GUI设计,下面我先展示设计的结果:
这是最后的结果展示:
2、具体的主要代码如下:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc;
axes(handles.axes1); cla reset;box on;set(gca,'XTickLabel','','YTickLabel','')
axes(handles.axes2); cla reset;box on;set(gca,'XTickLabel','','YTickLabel','')
axes(handles.axes3); cla reset;box on;set(gca,'XTickLabel','','YTickLabel','')
handles.file1 = [];
handles.file2 = [];
handles.result = [];
[filename,pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files'},'选择图像1',...
fullfile(pwd,'D:\matlab_files\wavelet_learning\c01_1.tif'));
if filename == 0
return;
end
handles.file1 = fullfile(pathname,filename);
Imag1 = imread(fullfile(pathname,filename));
axes(handles.axes1);cla reset;box on;set(gca,'XTickLabel','','YTickLabel','');
imshow(Imag1,[]);
guidata(hObject,handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename,pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files'},'选择图像2',...
fullfile(pwd,'D:\matlab_files\wavelet_learning\c01_1.tif'));
if filename == 0
return;
end
handles.file2 = fullfile(pathname,filename);
Imag2 = imread(fullfile(pathname,filename));
axes(handles.axes2);cla reset;box on;set(gca,'XTickLabel','','YTickLabel','');
imshow(Imag2,[]);
guidata(hObject,handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%小波分解函数定义
function[c,s] = Wave_Decompose(M,zt,wtype)
%小波分解处理
%输入参数:
%M - 图像矩阵
%zt - 尺度信息
%wtype - 使用的小波类型
%输出参数:
%c,s -- 分解结果
%参数处理
if nargin < 3
wtype = 'haar';
end
if nargin < 2
zt = 2;
end
%小波分解
[c,s] = wavedec2(M,zt,wtype);
% 小波融合函数定义
function Coef_Fusion = Fuse_Process(c0,c1,s0,s1) %
%小波融合处理
%输入参数:
%c0,c1,s0,s1 --两幅图像小波分解结果
%输出参数:
%Coef_Fusion-- 融合结果
KK = size(c1);
Coef_Fusion = zeros(1,KK(2));
%低频系数的处理
Coef_Fusion(1:s1(1,1)*s1(1,2)) = (c0(1:s1(1,1)*s1(1,2)) +c1(1:s1(1,1)*s1(1,2)))/2;
%处理高频系数\
MM1 = c0(s1(1,1)*s1(1,2)+1:KK(2));
MM2 = c1(s1(1,1)*s1(1,2)+1:KK(2));
%融合处理
mm = (abs(MM1)) > (abs(MM2));
Y = (mm.*MM1) + ((~mm).*MM2);
Coef_Fusion(s1(1,1)*s1(1,2)+1:KK(2)) = Y;
% 小波重构部分
function Y = Wave_Reconstruct(Coef_Fusion,s,wtype)
%小波重构
%输入参数:
%Coef_Fusion-- 融合系数
%s--小波系数
%wtype -- 小波类型
%输出参数:
%Y -- 小波重构结果
if nargin < 3
wtype = 'haar';
end
%重构
Y = waverec2(Coef_Fusion,s,wtype);
% --- 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)
if isempty(handles.file1)
msgbox('请载入图像1!','提示信息','modal');
return;
end
if isempty(handles.file2)
msgbox('请载入图像2!','提示信息','modal');
return;
end
[imA,map1] = imread(handles.file1);
[imB,map2] = imread(handles.file2);
M1 = double(imA)/256;
M2 = double(imB)/256;
zt = 2;
wtype = 'haar';
%多尺度二维小波分解
[c0,s0] = Wave_Decompose(M1,zt,wtype);
[c1,s1] = Wave_Decompose(M2,zt,wtype);
%小波融合
Coef_Fusion = Fuse_Process(c0,c1,s0,s1);
%重构
Y = Wave_Reconstruct(Coef_Fusion,s0,wtype);
handles.result = im2uint8(mat2gray(Y));
guidata(hObject,handles);
msgbox('小波融合处理完毕','提示信息','modal');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes3);cla reset;box on;set(gca,'XTickLabel','','YTickLabel','');
imshow(handles.result);
guidata(hObject,handles);
3、感想:
首先要懂得原理,其次实践是检验你是否能将原理运用的关键!!
多模仿别人的代码,多思考,多实践;这才是学习的一种方法!