I am brand new to MATLAB but am trying to do some image compression code for grayscale images.
Questions
How can I use SVD to trim off low-v
Just to start, I assume you're aware that the SVD is really not the best tool to decorrelate the pixels in a single image. But it is good practice.
OK, so we know that B = U*S*V'
. And we know S is diagonal, and sorted by magnitude. So by using only the top few values of S, you'll get an approximation of your image. Let's say C=U*S2*V'
, where S2 is your modified S. The sizes of U and V haven't changed, so the easiest thing to do for now is to zero the elements of S that you don't want to use, and run the reconstruction. (Easiest way to do this: S2=S; S2(N+1:end, :) = 0; S2(:, N+1:end) = 0;
).
Now for the compression part. U
is full, and so is V
, so no matter what happens to S2
, your data volume doesn't change. But look at what happens to U*S2
. (Plot the image). If you kept N singular values in S2
, then only the first N rows of S2
are nonzero. Compression! Except you still have to deal with V
. You can't use the same trick after you've already done (U*S2)
, since more of U*S2
is nonzero than S2
was by itself. How can we use S2 on both sides? Well, it's diagonal, so use D=sqrt(S2)
, and now C=U*D*D*V'
. So now U*D
has only N nonzero rows, and D*V'
has only N nonzero columns. Transmit only those quantities, and you can reconstruct C, which is approximately like B.