Mga Pahina

Huwebes, Setyembre 5, 2013

Principal Component Analysis as a tool for Image Compression

It's blogging time again! :) Now that we have discussed about the Image file format and Fourier Analysis, we are now ready to tackle a more advanced method that is almost the same with Fourier Transforms. We have learned from the past activities that Fourier transform is very essential not only in the world of signal processing, but also in image processing.

Now, let's tackle about the PCA or the Principal Component Analysis. Basically, PCA to me is like a sister of the Fourier transform. They are both orthogonal and express a signal into a linear superposition of subsignals or components. Suppose a signal is represented with xy-coordinates shown in Figure 1. The information is represented by a 2-dimensioanal coordinate and the signal follows a straight line. If I introduce another coordinate, say ji –coordinate where it is defined by the rotation of the signal with respect to the x-coordinate, I can compress the information into 1-dimensional since the signal has 0 values in the jth coordinate.
Figure 1. PCA: Decomposing the number of representation of a certain information

Now, extending our analysis in an N-dimensional space, information can be expressed in a lesser number of components or dimension through a number of rotations or change of coordinates. This is what PCA does: to find the principal components of a given signal.

I find it really helpful that the AP187 and AP186 class are in sync. This topic has already been discussed in AP187, so it was easier for me to understand the concepts when it comes to compressing images.
Essentially, we can think of an image as a signal, only that it is 2-dimensional. According to what Dr Soriano said, an image of a face needs a minimum number of 100 pixels to be able to comprehend or distinguish its features. Each set of block pixels (say 10x10) stores a large array of information that can represent the whole image. When using PCA, we can minimize this with a minimal loss in the resolution or quality of the image. This is how jpeg images are being compressed, which is why it is an example of a lossy compression. PCA uses the weighted basis eigenfunctions (or in this case, eigenimages) to reconstruct the original image.

Let’s now try to reconstruct an image using PCA. I just got back from Cagayan de Oro last weekend and I had a one –kind of an adventure with my high school barkada. The picture of us below is taken at a coffee shop while we were chilling out around the city proper.

Figure 2. Image to be reconstructed

The size of my image is 336x448 pixels. Now, the first step in reconstructing this image is dividing the image into a number of 10x10 pixel blocks, creating a total of 1452 sub-blocks. Each block is then translated into a single column to have 1452 sets of 100x1 values. We call this matrix x. We can now decompose x using pca() to get the principal components and the eigenvalues. These are stored in the second (facpr) and third (comprinc) matrix, respectively, that is returned by pca(). The comprinc stores the eigenvalues themselves so there is no more need to normalize it. We can just get the dot product of this and the eigenvectors given by the facpr. The result then gives us the reconstructed image information. After getting this, we are now ready to assign back the information back to its matrix form of 330x440 pixels.

Since the goal of PCA here is to compress the image by decreasing the information stored in a number of dimensions, we can just get the correct number of principal components needed to construct the image up to 99% accuracy instead of using all principal components. This can be obtained by getting the cummulative sum of the second row of the matrix lambda that is returned by the pca(). My sample picture needs the first M = 16 principal components to reconstruct the image with at least 99% information.

 Figure 3. Reconstructed grayscale images with varying number of components (M) used

Notice from the figure above that the reconstructed image even using only the first principal component appears successful. As you increase the M, you increase the number of principal components is increased. At M = 16, we were able to reconstruct the image without any sign of information loss.

Another method I used is using only the eigenvectors provided by the facpr matrix, I calculated for the eigenvalues by taking the dot product of the signals with the facpr, the same way I did when I took the dot product of the principal components to the desired signal to get the coefficients of the principal components  back in our Applied Physics 187 activity. I then used these coefficients and obtained the superposition of the principal components multiplied with their corresponding coefficients. The result is the information regarding the reconstructed image.

I also wondered how the pictures in Figure 3 would look like if I reconstruct them into their respective RGB equivalents. I tried reconstructing each channel (R,G and B) of the original image.




Figure 4. Reconstructed RGB image using comprinc as eigenvalues at different number of principal components

You can't almost see the difference between the different number of principal components used for M=16, 50 100. This is why PCA is a very effective way of compressing images. 


Figure 5. Comparison of reconstructed images using different methods

Figure 5 shows a comparison in the reconstructed images between using comprinc and facpr and that of using facpr only. There is no apparent difference between the two.


For this activity, I give myself a grade of 11/10 for being able to reconstruct the images in two ways.

Walang komento:

Mag-post ng isang Komento