I want to compress an image using eigenvalues and eigenvector, since I already done it using singular value decomposition, but I do something wrong while trying to compress it using eigenvalues and eigenvectors.
Here is some code to see what I've already tried but something goes wrong while trying to calculate the Z
matrix.
image_matrix = double(imread(image));
[m n] = size(image_matrix);
miu = zeros(m, 1);
A = [];
for i = 1 : m
miu(i) = mean(image_matrix(i, :));
A(i, :) = image_matrix(i, :) - miu(i);
endfor
Z = A*A'/(n - 1);
[V S] = eig(Z);
W = V(:, 1 : k);
Y = W'*A;
A_k = W*Y + miu;
W*Y+miu
should've give me the approximation of matrix A from the above but it just gives a blank image. The above code receives: a path to an image and a number k
such that k
is some kind of order of approximation... I call it using ('path/to/image', 2)
and don't get the expected result. Also I am using this only for black and white images.
What you are trying to do is find the significant modes of the correlation matrix of your image, that is the modes that have the largest eigenvalues. In general eig returns the eigenvalues/vectors in random order, @CrisLuengo is telling you that before throwing eigenvalues/modes away, you need to first order the results that eig returns as I have done in this script;
clear;clf
tx = ty = linspace (-8, 8, 41)';
[xx, yy] = meshgrid (tx, ty);
r = sqrt (xx .^ 2 + yy .^ 2) + eps;
tz = sin (r) ./ r;
%mesh (tx, ty, tz);
Z=tz*transpose(tz);
[vec,L]=eig(Z);
lambda=diag(L);
[lambda,order]=sort(lambda,'descend');
vec=vec(:,order);
%%plot(lambda);hold on;plot(diag(L))
%% reconstruct with 5 largest modes:
neof=5;
shortz=tz*vec(:,1:5)*transpose(vec(:,1:5));
mesh (tx, ty, shortz);
The image is sin(r)/r, on a 41 by 41 grid. If you plot diag(L), the (not-ordered) eigenvalues you will note the fitst 30 or so are near zero, meaning that they correspond to very, very little info. All the action is in the last few modes. After re-ordering, these become the first few modes.