离散型哈尔小波变换 Matlab简单实现
二阶哈尔变换矩阵如下
[ 1 2 1 2 1 2 − 1 2 ] \begin{bmatrix} \frac{1}{\sqrt 2}& \frac{1}{\sqrt 2} \\ \frac{1}{\sqrt 2} & -\frac{1}{\sqrt 2} \\ \end{bmatrix} [212121−21]
经过小波变换后,可以把数据划分为不同的成分:高频成分,低频成分。
多阶小波变换,对于低频成分可以进行划分。
一维离散型哈尔小波变换
实现代码
function [ A,D ] = haarLinearTranslation(data)
ha=[0.7071,0.7071; 0.7071,-0.7071];
[~,n]= size(data);
data = reshape(data,2,n/2);
data = ha*data;
A = data(1,:);
D = data(2,:);
end
调用
load noisdopp
[A,D]=haarLinearTranslation(noisdopp);
subplot(3,1,1)
plot(noisdopp) %原图
subplot(3,1,2)
plot(A) %近似和
subplot(3,1,3)
plot(D) %细节和
二维离散型哈尔小波变换
实现代码
function [ output ] = haarTranslation(img,k)
%对img图像做k阶哈尔小波变换
ha=[0.7071 0.7071; 0.7071 -0.7071];
size_img = 2^int32(log2(min(size(img))));
img = double(imresize(img,[size_img,size_img]));
output = translation(img,k,size_img,ha);
end
function out = translation(img,k,size,ha)
for i=1:2:size;
for j=1:2:size;
img(i:i+1,j:j+1)= ha*img(i:i+1,j:j+1)*ha';
end
end
size_half = size/2;
data = reshape(img,2,size_half,size);
u = reshape(data(1,:,:),size_half,size);
v = reshape(data(2,:,:),size_half,size);
u = reshape(u',2,size_half,size_half);
v = reshape(v',2,size_half,size_half);
out =[reshape(u(1,:,:),size_half,size_half),reshape(u(2,:,:),size_half,size_half);reshape(v(1,:,:),size_half,size_half),reshape(v(2,:,:),size_half,size_half);];
if k>1
out(1:size_half,1:size_half) = translation(out(1:size_half,1:size_half),k-1,size_half,ha);
end
end
调用
img = rgb2gray(imread('img.png'));
subplot(1,3,1)
imshow(img); %原图
subplot(1,3,2)
imshow(uint8(haarTranslation(img,1))); %一阶哈尔小波变换
subplot(1,3,3)
imshow(uint8(haarTranslation(img,2))); %二阶哈尔小波变换
上面看不清,再补一张图