{"id":178,"date":"2017-09-11T20:17:47","date_gmt":"2017-09-11T19:17:47","guid":{"rendered":"http:\/\/www.science-now.org\/giampaolo\/?p=178"},"modified":"2018-08-21T14:16:38","modified_gmt":"2018-08-21T13:16:38","slug":"image-compression-with-svd","status":"publish","type":"post","link":"https:\/\/www.science-now.org\/giampaolo\/2017\/09\/11\/image-compression-with-svd\/","title":{"rendered":"Image compression with SVD"},"content":{"rendered":"<p>Techniques adopted for model reduction are used for image compression as well. In this case, they are useful to explain how model reduction works. In particular, we&#8217;re going to use Singular Value Decomposition (SVD) to compress an image!<\/p>\n<p><!--more--><\/p>\n<p>First of all, the ingredients we are going to use for this recipe are: a grayscale image, some linear algebra math and a little bit of time. Regarding the software, we can use any kind of scientific package and in this case we&#8217;re going to use Mathematica from Wolfram since it allows for\u00a0a quick and effective image manipulation as well as advanced math manipulation.<\/p>\n<p><strong>Step 1.<\/strong> The first step involves choosing the image. For the sake of simplicity, we&#8217;re going to use a grayscale image. Such images can be seen as matrices whose elements are number ranging from 0 to 1 and representing the level of grey corresponding to each pixel. The image we&#8217;ll use is this:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-179\" src=\"http:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/src.jpg\" alt=\"\" width=\"80%\" srcset=\"https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/src.jpg 770w, https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/src-300x186.jpg 300w, https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/src-768x477.jpg 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>It is a nice picture of New York from the Staten Island ferry. It was converted to grayscale by discarding the colour information and it has a resolution of 642&#215;398 pixels.<\/p>\n<p><strong>Step 2<\/strong>. We have to load this image into a Mathematica notebook and transform the data into a matrix. This is achieved with this command:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">S = Image[ImportData[''SampleNY.jpg'']]<\/pre>\n<p>The matrix S has dimension m x n where m and n are the number of pixels in the horizontal and vertical direction, 642 and 398 respectively.<\/p>\n<p><strong>Step 3.<\/strong>\u00a0The compression is performed calculating the\u00a0singular value decomposition of the matrix S. Given\u00a0S, its SVD decomposition is written as:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">S = U w\u00a0V*<\/pre>\n<p>and U w and V are obtained with the code<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">[U, w, V] = SingularValueDecomposition[S]<\/pre>\n<p>This results in a factorisation of the rectangular matrix S that resembles the eigenvalue decomposition of a square matrix. \u00a0The columns of\u00a0U and V are called left and right singular vectors, respectively, while the entries of the diagonal matrix w are the singular values. The entries of w are associated with the amount of\u00a0information contained by each pair of singular vectors. We can sort the singular values since they are a measure of the energy contained in the corresponding singular pair.\u00a0The compression is performed by assuming the image can be represented by the few most energetic\u00a0singular vectors.<\/p>\n<p>We can ask Mathematica to calculate the SVD decomposition and return a subset of singular vectors corresponding to the largest singular values. In particular, we can ask for the\u00a010 singular pairs with the largest\u00a0singular values:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">[U, w, V] = SingularValueDecomposition[S,10]<\/pre>\n<p>Now, the matrices U and V contain\u00a0only 10 singular pairs.<\/p>\n<p><strong>Step 4.<\/strong> The image is reconstructed using only these 10 singular pairs. The new data is calculated and\u00a0stored in a new variable. In Mathematica, that becomes:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">recS\u00a0= U . w . Conjugate[Transpose[V]]<\/pre>\n<p>where the dot stands for the matrix product. The matrix recS\u00a0has the same size of the initial S matrix which represents the full-quality image. However, in terms of memory the combined size of U, V and w \u00a0is much smaller\u00a0than the corresponding memory needed by S or recS.<\/p>\n<p><strong>Step 5.<\/strong> Reconstructing the image with few singular pairs means also discarding a large part of the information. In fact, we used the 10 most &#8220;energetic&#8221; pairs out of hundreds.\u00a0We can visualise the effect of retraining only few singular pairs by showing the image reconstructed with just 10 of them:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-180\" src=\"http:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img1.png\" alt=\"\" width=\"80%\" srcset=\"https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img1.png 770w, https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img1-300x186.png 300w, https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img1-768x477.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>As you can see, the main features of the picture are there but all the details are missing!<\/p>\n<p><strong>Step 6.<\/strong> Last but not least, we can play with the number of singular pairs\u00a0we use to reconstruct the image. As already\u00a0said, the largest singular value corresponds to the most important feature of the image. If we reduce the picture to just 1 singular value, we have:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-181\" src=\"http:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img2.png\" alt=\"\" width=\"80%\" srcset=\"https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img2.png 770w, https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img2-300x186.png 300w, https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img2-768x477.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>Increasing the number of singular values from 1 to 5, things improve&#8230;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-182\" src=\"http:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img3.png\" alt=\"\" width=\"80%\" srcset=\"https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img3.png 770w, https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img3-300x186.png 300w, https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img3-768x477.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>But we need at least 50 singular pairs to have a good result like this:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-183\" src=\"http:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img4.png\" alt=\"\" width=\"80%\" srcset=\"https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img4.png 770w, https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img4-300x186.png 300w, https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img4-768x477.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>and if we increase the number to 100, we have the good result shown below which uses only 40% of the original memory!<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-184\" src=\"http:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img5.png\" alt=\"\" width=\"80%\" srcset=\"https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img5.png 770w, https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img5-300x186.png 300w, https:\/\/www.science-now.org\/giampaolo\/wp-content\/uploads\/2018\/08\/img5-768x477.png 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p><strong>Step 7.<\/strong> To summarise, the compression is obtained here by calculating the SVD decomposition of the image,\u00a0discarding the majority of the singular pairs and retraining only a subset of them (the most important). In terms of storage\u00a0memory, this subset needs just a fraction of the memory which would be required by the whole image!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Techniques adopted for model reduction are used for image compression as well. In this case, they are useful to explain how model reduction works. In particular, we&#8217;re going to use Singular Value Decomposition (SVD) to compress an image!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[29,28,27],"class_list":["post-178","post","type-post","status-publish","format-standard","hentry","category-programming","tag-image-compression","tag-model-reduction","tag-singular-value-decomposition"],"_links":{"self":[{"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/posts\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/comments?post=178"}],"version-history":[{"count":3,"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":197,"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/posts\/178\/revisions\/197"}],"wp:attachment":[{"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/categories?post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.science-now.org\/giampaolo\/wp-json\/wp\/v2\/tags?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}