Image Compression via SVD

We borrow a Wolfram demonstration to give a visual impression of how well a partial SVD approximates a given (dense) matrix. This is an image from the Wolfram test database.

Manipulate[Module[{lr},
lr = LowRank[n];
Grid[{{ArrayPlot[tank/255.,ImageSize->256,PlotRangePadding->None,Frame->False,PlotLabel->Style["exact","Label"]],ArrayPlot[lr,ImageSize->256,PlotRangePadding->None,Frame->False,PlotLabel->Style["compressed","Label"]]},
{ArrayPlot[Abs[lena - lr],ImageSize->256,PlotRangePadding->None,Frame->False,PlotLabel->Style["difference between exact and compressed","Label"]],SpanFromLeft}}]],{{n,61,"included singular values"},1,61,1,Appearance->"Labeled"},Initialization:>{tank =Transpose@Take[Transpose[Take[N@Map[255-#&,ExampleData[{"TestImage","Tank2"},"Data"],{2}],{170,300}]],{220,350}];
{u,sigma,v}= SingularValueDecomposition[tank];
sigmalist= Tr[sigma,List];
AppendTo[sigmalist,0.000];LowRank[n_]:=Total[Table[sigmalist[[i]]*Outer[Times,u[[All,i]],v[[All,i]]],{i,n}]]/; 0< n ≤ 128;},SynchronousInitialization->False]

CAPTION

This demonstrates how an image can be compressed via the singular value decomposition (SVD). The original image is first represented as a matrix A with the intensity of each pixel assigned a numeric value. Then the singular value decomposition is performed and a low rank approximation of A is formed via A ≃(∑_(k=i)^n)(Subscript[σ, i])(Subscript[u, i]) ((Subscript[v, i])^T), where (Subscript[σ, i])  is the (i^th) singular value and (Subscript[u, i]) and (Subscript[v, i]) are the (i^th) left and right singular vectors, respectively.

THIS NOTEBOOK IS THE SOURCE CODE FROM

"Image Compression via the Singular Value Decomposition" from the Wolfram Demonstrations Project
 http://demonstrations.wolfram.com/ImageCompressionViaTheSingularValueDecomposition/

Contributed by: Chris Maes

tank2 =Transpose@Take[Transpose[Take[N@Map[255-#&,ExampleData[{"TestImage","Tank2"},"Data"],{2}],{200,300}]],{250,350}];

tank2