SVD and Image Compression(by Shiny! Yes!)

We have a brilliant Shiny application demonstrating Image Compression with Singular Value Decomposition. One could get a sense ofm SVD quickly from it: https://yihui.shinyapps.io/imgsvd/.

ImgSVD made by Nan Xiao, who is Data Analyst Intern at SupStat and Yihui Xie who is Software Engineer at RStudio, also advisory Data Scientist at SupStat Inc.

The interface looks like the following:

Shiny1

Singular Value Decomposition is a complex technique. It is a matrix factorization and is widely used in dimensionality reduction. Its result is an approximation of a matrix, and it is flexible because we can determine the degree of the approximation by the parameter k. Besides, image compression is also focusing on approximation. We can use this method for image compression if we regard an image as a matrix. Following is a demonstration of the result of our algorithm.

Original image is

Shiny2

When we set k=1

Shiny3

When we set k=5

Shiny4

When we set k=10

Shiny5

When we set k=20

Shiny6

When we set k=100

Shiny7
When k=1, we can only see the color of the original picture. When k=5, the scheme of the scene is vaguely recognizable. As k keeps increasing, the picture becomes clearer. When k=100, it is almost the same as the original one. SVD is a computation expensive algorithm, therefore the function svd() in R cannot fulfill our requirement. So instead, we use the function svds() from package rARPACK which will only calculate the first k terms. The code for computing is as follows:
https://gist.github.com/casunlight/9628504
References:Image Compression with the SVD in RFoundations of Data ScienceSVD on Wikipedia