haar小波对图像的一次分解实验,同样要求不使用matlab自带的小波函数和卷积函数。
分解过程:
matlab代码如下:
clc
clear
x=imread('cameraman.tif'); %读取matlab里自带的一副图片
%imshow(img);该图像的行列为366*364
%haar小波对图像进行分解
h1=x;
h3=x;
for i=1:256
for j=1:255
%对行,进行低通滤波 (1/2,1/2)
h1(i,1)=0+1/2*x(i,1);
h1(i,j)=1/2*x(i,j)+1/2*x(i,j+1);
%对行,进行高通滤波()
h3(i,1)=-1/2*x(i,1);
h3(i,j)=1/2*x(i,j)-1/2*x(i,j+1);
end
h1(i,256)=1/2*x(i,256);
end
for i=1:256
for j=1:128
%对行压缩
k=2*j;
h1(i,j)=h1(i,k);
h3(i,j)=h3(i,k);
end
end
h2=h1;
h4=h3;
for j=1:128
for i=1:255
%对列进行低通滤波
h1(1,j)=1/2*h1(1,j);
h1(i,j)=1/2*h1(i,j)+1/2*h1(i+1,j);
%对h2的列进行高通滤波
h2(1,j)=-1/2*h2(1,j);
h2(i,j)=1/2*h2(i,j)-1/2*h2(i+1,j);
%对h3的列低通滤波
h3(1,j)=1/2*h3(1,j);
h3(i,j)=1/2*h3(i,j)+1/2*h3(i+1,j);
%对h4的列高通滤波
h4(1,j)=-1/2*h4(1,j);
h4(i,j)=1/2*h4(i,j)-1/2*h4(i+1,j);
end
end
%对列压缩
for j=1:128
for i=1:128
k=2*i;
h1(i,j)=h1(k,j);
h2(i,j)=h2(k,j);
h3(i,j)=h3(k,j);
h4(i,j)=h4(k,j);
end
end
h1=h1(1:128,1:128);
h2=h2(1:128,1:128);
h3=h3(1:128,1:128);
h4=h4(1:128,1:128);
%haar小波的一次分解的边界太不明显,处理一下像素,使边缘更明显一些
n=9;
for i=1:128
for j=1:128
if(h2(i,j)*n<256)
h2(i,j)=n*h2(i,j);
end
if(h3(i,j)*n<256)
h3(i,j)=n*h3(i,j);
end
if(h3(i,j)*n<256)
h3(i,j)=n*h3(i,j);
end
if(h4(i,j)*n<256)
h4(i,j)=n*h4(i,j);
end
end
end
subplot(2,2,1),imshow(h1);
subplot(2,2,2),imshow(h2);
subplot(2,2,3),imshow(h3);
subplot(2,2,4),imshow(h4);