Problems with SVD on imported jpg

1 view (last 30 days)
Nicholas
Nicholas on 10 Sep 2014
Edited: Nicholas on 10 Sep 2014
I am trying to use a low rank approximation of SVD to compress an image that I am importing. I first tested the code on a random 298x298 matrix. Here is my code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
B = rand(298,298); B(:,:,2) = rand(298,298); B(:,:,3) = rand(298,298);
B = double(B); % go to double precision
[U,S,V] = svd(B(:,:,1)); % do SVD
k = 100;
Bk = U(:,1:k)*S(1:k,1:k)*V(:,1:k)'; %truncate the SVD decomposition
imwrite(B(:,:,1),'newImage.jpg','jpg','Comment','My JPEG file') % write grayscale uncompressed image to file
imwrite(Bk,'newImage_compressed.jpg','jpg','Comment','My JPEG file') % write grayscale compressed image to file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Everything works great as expected. The compressed image is slightly washed out with this large of a k, but I more or less get what I expect. Now I turn to importing a 298x298 jpg file (as an array). Split off the grayscale portion (even though it is already black and white to simplify things for now) and compress it in the same way as the random matrix of the same size and write the compressed resulting image to a file and I get a basically blank image with maybe 10 black pixels clustered towards the center of the image. Here is my code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A = imread('baboon-grayscale.JPG'); % loads image in integer format as a 3d-array
A = double(A); % go to double precision
[U,S,V] = svd(A(:,:,1)); % do SVD
k = 100;
Ak = U(:,1:k)*S(1:k,1:k)*V(:,1:k)';%truncate the SVD decomposition
imwrite(A(:,:,1), 'baboon-grayscale_doubleprecision_uncompressed.jpg','jpg','Comment','My JPEG file'); % write grayscale uncompressed image to file
imwrite(Ak, 'baboon-grayscale_compressed.jpg','jpg','Comment','My JPEG file'); % write grayscale compressed image to file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I am stuck. The code for image compression works for a matrix, but not a matrix from an imported image. I looked at some random values of the compressed matrix and there are grayscale values between 0 and 256, but the image is basically blank with a few random black pixels towards the middle. I tried also, just importing the jpeg file and writing it to another file, to see if it could do so uncorrupted and it does. Like I said, I am stuck. Any suggestions would be greatly appreciated.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!