为什么80%的码农都做不了架构师?>>>
小波变换与小波包变换
人脸图像f(x,y) 的一层小波变换如下图所示:
图中L 和H 分别表示低通滤波器和高通滤波器,l(n) 和h(n) 分别表示它们相应的脉冲响应,2↓1表示降2采样fLL和fHH分别表示双向低频和高频小波子空间图像,而fLH和fHL分别表示一个方向低频、另一个方向高频的小波子空间图像。
若继续对fLL 做小波分解,则称为二层小波变换;若对4个子图都继续做小波分解,则称为二层小波包变换。对只含有低频成分的子图做小波分解,则称为不完全小波包变换。
图像小波变换的Matlab代码
clc;
clear;
close all;Xa =[40,10,56,46]; % 56*2=112,46*2-92(图片分辨率为112*92)
Xh =[40,10,56,46];
Xv =[40,10,56,46];
Xd =[40,10,56,46];
for i = 1:40 % 40个人脸for j = 1:10 % 1人10个表情I = imread(strcat('D:\Faces\ORL\s',num2str(i),'\',num2str(j),'.bmp')); [c1,s1]=wavedec2(double(I),1,'db1'); % 分别做4个方向的小波分解ca = appcoef2(c1,s1,'db1',1);ch = detcoef2('h',c1,s1,1);cv = detcoef2('v',c1,s1,1);cd = detcoef2('d',c1,s1,1);% A = uint8([wcodemat(ca,255),wcodemat(ch,255);wcodemat(cv,255),wcodemat(cd,255)]);% subplot(2,5,j),imshow(A,[],'InitialMagnification',100);for k = 1:56for l = 1:46Xa(i,j,k,l) = ca(k,l);Xh(i,j,k,l) = ch(k,l);Xv(i,j,k,l) = cv(k,l);Xd(i,j,k,l) = cd(k,l);endendend
endfor i = 1:1 % 查看第1个人的10个表情图的小波分解效果for j = 1:10xa = [56,46];xh = [56,46];xv = [56,46];xd = [56,46];for k = 1:56for l = 1:46xa(k,l) = Xa(i,j,k,l);xh(k,l) = Xh(i,j,k,l);xv(k,l) = Xv(i,j,k,l);xd(k,l) = Xd(i,j,k,l);endendA = uint8([wcodemat(xa,255),wcodemat(xh,255);wcodemat(xv,255),wcodemat(xd,255)]);subplot(2,5,j),imshow(A,[],'InitialMagnification',100);end
end
效果图
后记
本例仅给出图像1层小波变换,可以根据实际项目需求做n层图像小波分解,用于提取人脸图像特征。例如:
[c,s] = wavedec2(double(ca),1,'db1');
a = appcoef2(c,s,'db1',1);
% 构造基本分类器D1
for m = 1:28for n = 1:23D1(i,j,m,n) = a(m,n);end
end
h = detcoef2('h',c,s,1);
v = detcoef2('v',c,s,1);
d = detcoef2('d',c,s,1);
具体可以参考论文《基于模糊积分的不完全小波包子空间集成人脸识别 翟俊海 王熙照 张素芳》。
Reference
Matlab小波变换的三级分解程序用于图像处理
帖中代码:
clc;clear;close all
I = imread('lena.bmp');
%subplot(221);
subplot(221),imshow(I,[],'InitialMagnification',100);
title('原始载体图像')%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%图像的一级小波分解
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[c1,s1]=wavedec2(double(I),1,'db1');
ca88 = appcoef2(c1,s1,'db1',1);
ch88 = detcoef2('h',c1,s1,1);
cv88 = detcoef2('v',c1,s1,1);
cd88 = detcoef2('d',c1,s1,1);
A88 = uint8([wcodemat(ca88,255),wcodemat(ch88,255);wcodemat(cv88,255),wcodemat(cd88,255)]);
%subplot(222);
subplot(222),imshow(A88,[],'InitialMagnification',100);
title('图像的一级小波分解');%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%图像的二级小波分解
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%[c,s]=wavedec2(double(I),2,'db1');
ca2 = appcoef2(c,s,'db1',2);
ch2 = detcoef2('h',c,s,2);
cv2 = detcoef2('v',c,s,2);
cd2 = detcoef2('d',c,s,2);
%ca1 = appcoef2(c,s,'db2',1);
ch1 = detcoef2('h',c,s,1);
cv1 = detcoef2('v',c,s,1);
cd1 = detcoef2('d',c,s,1);A1 = [ca2*4,ch2*4;cv2*4,cd2*4];
k=s(2,1)*2-s(3,1);
ch1 = padarray(ch1,[k k],1,'post');
cv1 = padarray(cv1,[k k],1,'post');
cd1 = padarray(cd1,[k k],1,'post');
%A = [A1,ch1*4; cv1*4,cd1*4];
A = uint8([wcodemat(A1,255),wcodemat(ch1*10,255); wcodemat(cv1*10,255),wcodemat(cd1*10,255)]);
% min = min(A(:));
% max = max(A(:));
%subplot(223);
subplot(223),imshow(A,[],'InitialMagnification',100);
title('图像的二级小波分解');%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%图像的三级小波分解
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%[c,s]=wavedec2(double(I),3,'db1');
ca3 = appcoef2(c,s,'db1',3);
ch3 = detcoef2('h',c,s,3);
cv3 = detcoef2('v',c,s,3);
cd3 = detcoef2('d',c,s,3);
ch2 = detcoef2('h',c,s,2);
cv2 = detcoef2('v',c,s,2);
cd2 = detcoef2('d',c,s,2);
ch1 = detcoef2('h',c,s,1);
cv1 = detcoef2('v',c,s,1);
cd1 = detcoef2('d',c,s,1);
A2 = [ca3*4,ch3*4;cv3*4,cd3*4];
k=s(2,1)*2-s(3,1);
ch2 = padarray(ch2,[k k],1,'post');
cv2 = padarray(cv2,[k k],1,'post');
cd2 = padarray(cd2,[k k],1,'post');
A1= [A2,ch2*4; cv2*4,cd2*4];
k=s(2,1)*4-s(4,1);
ch1 = padarray(ch1,[k k],1,'post');
cv1 = padarray(cv1,[k k],1,'post');
cd1 = padarray(cd1,[k k],1,'post');
AA = uint8([wcodemat(A1,255),wcodemat(ch1*4,255); wcodemat(cv1*4,255),wcodemat(cd1*4,255)]);
% min = min(A(:));
% max = max(A(:));
%subplot(224);
subplot(224),imshow(AA,[],'InitialMagnification',100);
title('图像的三级小波分解');
效果图: