我們有這麼一張灰度圖64*64
我們可以定義出4096個基,分別是某一位是0其他是1,在這種情況下,如果我們傳輸圖片,那麼就相當於傳輸原始數據
假設傳到一半,網絡壞了。
於是,我們得到
我們可以計算原圖像和這圖像的差距
error = I - I_approx;
distance = sqrt(sum(sum(error.*error)))
distance =
713.5559
假設我們使用Haar基
function h = haar(N)
% Assume N is power of 2
h = zeros(N,N);
h(1,1:N) = ones(1,N)/sqrt(N);
for k = 1:N-1
p = fix(log(k)/log(2));
q = k-(2^p);
k1 = 2^p;
t1 = N/k1;
k2 = 2^(p+1);
t2 = N/k2;
for i=1:t2
h(k+1,i+q*t1) = (2^(p/2))/sqrt(N);
h(k+1,i+q*t1+t2) = -(2^(p/2))/sqrt(N);
end
end
然後,我們看一下只接受一半的圖片可以恢復成什麼樣子,這個東西可以用到圖片壓縮(PCA降維)
clear all
close all
clc
% Load image
I = double(imread('camera.jpg'));
% Arrange image in column vector
I = I(:);
% Generate Haar basis vector (rows of H)
H = haar(4096);
% Project image on the new basis
I_haar = H*I;
% Remove the second half of the coefficient
I_haar(2049:4096) = 0;
% Recover the image by inverting change of basis
I_haar = H'*I_haar;
% Rearrange pixels of the image
I_haar = reshape(I_haar, 64, 64);
% Display image
figure
imshow(I_haar,[]);
效果還可以
error = I - I_haar;
distance = sqrt(sum(sum(error.*error)))
distance =
350.6765
Haar基是正交基
如何將普通基變成正交基呢
線性代數中的拉格慕-施密特正交化就行
function E=gs_orthonormalization(V)
% V is a matrix where each column contains the vectors spanning the space of which we want to compute the orthonormal base
% E is a matrix where each column contains an ortho-normal vector of the base of the space
[numberLines,numberColumns]=size(V);
% U is a matrix containing the orthogonal vectors (non normalized)
U=zeros(numberLines,numberColumns);
for indexColumn=1:numberColumns
U(:,indexColumn)=V(:,indexColumn);
for index=1:(indexColumn-1)
R(index,indexColumn) =U(:,index)'*V(:,indexColumn);
U(:,indexColumn)=U(:,indexColumn)-R(index,indexColumn)*U(:,index);
end
R(indexColumn,indexColumn)=norm(U(:,indexColumn));
U(:,indexColumn)=U(:,indexColumn)./R(indexColumn,indexColumn);
end
E=U;
return