\

Singular Value Decomposition (SVD) is an important matrix Decomposition in linear algebra. In many cases, a portion of the data carries most of the information in the data set, while the rest of the information is either noise or irrelevant. Implemented with SVD, the original dataset can be represented with a much smaller dataset. In doing so, you essentially remove noise and redundant data. Similarly, removing information is useful when we’re trying to save space. You can use this idea to compress images.

In Python, SVD decomposition is very simple, using the np.linalg.svd() function, such as u,sigma,v= Np.linalg.svd (A), then u and v return the left and right singular vectors of A, and sigma does not return the coefficient matrix. It’s going to be a vector with singular values from highest to lowest.

For an image, a normal color image is actually a superposition of matrices on three RGB layers, with each element being an integer between 0 and 255. In Python, the image can be read by using the plt.imread() function, which directly produces a matrix of AB3, and then processes the three layers separately.

The basic steps of image compression using SVD decomposition are as follows:

1 read the picture and decompose it into RGB three matrices. 2. SVD decomposition is performed on the three matrices to obtain corresponding singular values and singular vectors. 3 filter singular values according to certain criteria (a certain percentage of the total number, or a certain percentage of the sum of singular values) 4 Restore the matrix, and add up the RGB three matrices. 5 Save the image.

The implementation steps are as follows:

Since images need to be read in Python, the following libraries need to be installed on the command line:

pip install matplotlib pillow
Copy the code

The program starts by importing the following two libraries:

import numpy as np   
from matplotlib import pyplot as plt
Copy the code

Then define the following functions to read in the image and generate the data matrix:

def svdimage(filename,percent): Array (original[:,:,0]) G0=np.array(original[:,:,1]) # fetch matrix data V0 = Np.linalg. SVD (R0) B0= Np.linalg. SVD (G0) R1= Np.zeros (R0. Shape) G1= NP.Zeros (G0. Shape) B1=np.zeros(B0.shape) total0=sum(sigma0) total1=sum(sigma1) total2=sum(sigma2) sd=0 for i,sigma in enumerate(sigma0): Filter by the percentage of the sum of singular values. R1 + np = sigma *. Dot (u0 [, I]. Reshape (1, 1), where v0 [I:]. Reshape (1, 1)) sd + = sigma if sd > = percent * total0: break sd=0 for i,sigma in enumerate(sigma1): 0 0 0 0 G1+=sigma*np.dot(U1 [:, I].0 (1, 1) v1[I,:].0 (1,-1) SD +=sigma if SD >=percent* Total1:0 break sd=0 for i,sigma in enumerate(sigma2): 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 break final=np.stack((R1,G1,B1),2) final[final>255]=255 final[final<0]=0 final=np.rint(final).astype('uint8') return finalCopy the code

Finally, you can call this function to specify the desired percentage. As shown below:

For p in np.arange(.1,1,.1): after=svdimage(filename,p) plt.imsave(STR (p)+'_1.jpg',after)Copy the code

The original image is as follows:

The image below shows the compressed information with 50% singular value preserved.

The image below shows the compressed information that retains 90% of the singular value.

It can be seen that the image compression ratio is different with different singular value retention degrees.

\

This article is excerpted from Python 3 Data Analysis and Machine Learning In Action, published by Peking University Press. The book is now available on JD.com for a 100 off 50 discount in spring. Click to read the original article to learn more.

\

If you have made interesting applications in machine learning, we will send one of the above books to a random reader in the comments.