Target:将下面的图1进行3级的Haar变化,即意味着变化后的图有10个成分。
图1
Knowledge:二维的Harr是先在行方向进行一次一维的haar变化,而后再一次列方向的一维haar变换,这叫做完成一次的二维Harr变化。如果要继续做Haar变化的话,比如说二级Haar变化,那你的输入就不是用来的那个data了,而是LL这个部分了,可以借助图2进行理解。
图2
第二点,就是本代码采取的Soft threshold function,它表达式如下:
Little details:由于Haar有压缩的作用,所以你滤完波后,图像可能会变得模糊。
clc;
clear;
%read image
data=imread('../images/noisy.tif');
% imshow(data);
subplot(2,2,1);
imshow(data,[]);
title("original");
data_haar=(haar(data,3));
subplot(2,2,2);
imshow(data_haar,[]);
title("haar");
data_de=denoise(data_haar,30);
subplot(2,2,3);
imshow(data_de,[]);
title("denoise");
data_de=ihaar(data_de,3);
subplot(2,2,4);
imshow(data_de,[]);
title("inverse haar");
%harr transform function, data denote the image data, n denote the haar numbers that you need. there, n=3 means i need 3-level haar transform.
function a=haar(data,n)
data=double(data);
[h,w]=size(data);
haar=double(zeros(h,w));
for i=1:n
h0=h/(2^i);
w0=w/(2^i);
for j=1:2*h0
for k=1:w0
haar(j,k)=(data(j,2*k-1)+data(j,2*k))/((2));
haar(j,k+w0)=(data(j,2*k-1)-data(j,2*k))/((2));
end
end
eee=haar;
for j=1:2*w0
for k=1:h0
haar(k,j)=(eee(2*k-1,j)+eee(2*k,j))/((2));
haar(k+h0,j)=(eee(2*k-1,j)-eee(2*k,j))/((2));
end
end
data=haar;
end
a=haar;
end
%this function is to denoise, you can use or not. denoise way is soft threshold. T denote threshold.
function a=denoise(data,T)
[h,w]=size(data);
for i=1:h
for j=1:w
e=data(i,j);
if((abs(e)>=T)&&(e>0))
e=(abs(e)-T);
elseif((abs(e)>=T)&&(e<0))
e=(-1)*(abs(e)-T);
else
e=0;
end
data(i,j)=e;
end
end
a=data;
end
%This inverse haar transform, data and n are some as haar function.
function a=ihaar(data,n)
data=double(data);
u=data;
[h,w]=size(data);
haar=data;
for i=1:n
h0=h/(2^(n-i));
w0=w/(2^(n-i));
for j=1:w0
for k=1:h0/2
haar(2*k-1,j)=data(k,j)+data(k+h0/2,j);
haar(2*k,j)=data(k,j)-data(k+h0/2,j);
end
end
ti=haar;
for j=1:h0
for k=1:w0/2
haar(j,2*k-1)=ti(j,k)+ti(j,k+w0/2);
haar(j,2*k)=ti(j,k)-ti(j,k+w0/2);
end
end
data=haar;
end
% haar(1:h/(2^n),1:w/(w/(2^n)))=u(1:h/(2^n),1:w/(w/(2^n)));
a=haar;
end
Reference:haar小波变换学习笔记