Applications of SVD: image compression

  • Images as matrices
  • Low-rank approximation

Images as matrices

We can represent images as matrices, as follows. Consider an image having n \times m pixels. For gray-scale images, we need one number per pixel, which can be represented as a n \times m matrix. For color images, we need three numbers per pixel, for each color: red, green, and blue (RGB). Each color can be represented as a n \times m matrix, and we can represent the full-color image as a n \times 3m matrix, where we stack each color’s matrix column-wise alongside each other, as A = [A_{red}, A_{green}, A_{blue}].

Image of a baboon The image on the left is a grayscale image, which can be represented as a 298 \times 298 matrix containing the gray-scale values stored as integers.

The image can be visualized in Matlab as well. We must first transform the matrix from integer to double. In JPEG format, the image will be loaded into Matlab as a three-dimensional array, with one matrix for each color. For gray-scale images, we only need the first matrix in the array.

Matlab syntax
>> A = imread(baboon-grayscale.jpg); % loads image in integer format as a 3d-array
>> A = double(A); % transform to real values
>> A = A(:,:,1); % for gray-scale images, we consider only the first matrix in the array

Low-rank approximation

Using the low-rank approximation via SVD method, we can form the best rank-k approximations for the matrix. The matlab code for this is as follows.

Matlab syntax
>> [U,S,V] = svd(A);
>> Ak = U(:,1:k)*S(1:k,1:k)*V(:,1:k)';
alt text True and approximated images, with varying ranks. We observe that with k=50, the approximation is almost the same as the original picture, whose rank is r=m=n=298.

Recall that the explained variance of the rank-k approximation is the between the squared norm of the error matrix, to the

alt text The explained variance for the various rank-k approximations to the original image. For k=50, the approximation contains more than 99\% of the total variance in the picture.

License

Hyper-Textbook: Optimization Models and Applications Copyright © by L. El Ghaoui. All Rights Reserved.

Share This Book