Principal Component Analysis (PCA) on images in MATLAB, A Graphical User Interface (GUI)

Abhilash Singh
Analytics Vidhya
Published in
4 min readNov 27, 2019
Graphical User Interface for PCA in MATLAB

In this article, we will first discuss the basics of PCA and how we can use PCA in MATLAB. After that, we will try to answer a fundamental question in PCA. You can download the MATLAB code from my MATLAB repository.

Update (16/04/2022):

I have uploaded a video on PCA. Please go through it if you want to learn PCA from scratch. Don’t forget to subscribe to my channel.

Question. What is PCA?

PCA is a mathematical procedure that transforms a no. of possibly correlated variables into smaller no. of uncorrelated variables called principal components (PC’s).
The first PC accounts for the highest variability in the data and the succeeding components have less variability than the preceding one.

Question. How we can use PCA in MATLAB?

[coeff, score, latent, ~, explained] = pca(X)

X: Input data of n x p dimension. The rows (n) should be the observations and the column (p) should be the variables. X should be zero-centered,

i.e X= X — mean(X) {This is done to bring the all data on the same scale}

coeff: It is a p x p matrix where each column is a principal component. The first column explains the most variance.
score: Data ‘X’ transformed into PC spaces.
i.e X = score * coeff will be the reconstructed data.
latent: Variance explained by each PC
explained: Percentage of the total variance explained by each PC.

explained = (latent/total(latent))* 100

It is used to decide how many PC’s to keep.

Step 1. Upload an image

Click on the “Upload an image button” to upload any image. The acceptable image formats are png, jpg, jpeg, img and tif.

Upload an image

Step 2. Plotting of the Grayscale image

Then click on the “Plot the grayscale image” button

Plotting of the Grayscale image

Step 3. Enter the number of PC’s

After that enter the no. of PC’s up to which you want to retrieve the images (both colour and grayscale). For example, enter 1

Reconstructed image using the first PC. The original image has 685 PCs.

An error message/box will pop-up when you enter a number greater than the no. of PCs for that particular image.

Also, an error will message will pop-up when the entered input is not a number.

Increase the no. of PC’s. Let’s say 25

Reconstructed image using the first 25 PC’s

Finally, let us answer a fundamental question regarding PCA.

Question. Can PCA work like image size reducer?

Image (left) is the original image and Image (right) is the reconstructed using the first 200 Principal Components (PCs). The original image has 685 PCs.
The size of the reconstructed colour image is 449 KB. (See the c_reconstructed.png file)

The answer to this question is yes, it works as an image reducer.

Applications of PCA

  1. You can use PCA for features selection and optimization.
  2. In the case of unsupervised classification, if we apply PCA before applying the unsupervised algorithm then the classification accuracy increases significantly. You may check our publication in which we have applied PCA beforehand to clearly differentiate between the barren land and vegetative land.

You can write to me (abhilash.singh@ieee.org) if you have any suggestions (or you want to add something to the application part) or any issue with this GUI or visit my web page for more updates

--

--

Abhilash Singh
Analytics Vidhya

Researcher at Indian Institute of Science Education and Research Bhopal. Subscribe my YouTube channel: https://www.youtube.com/channel/UC3YYrAOSNRXvG8Tud3XepYA