/Reconstruction-and-Compression-of-Color-Images

Reconstruction and Compression of Color Images Using Principal Component Analysis (PCA) Algorithm

Primary LanguagePython

Reconstruction and Compression of Color Image Using Principal Component Analysis PCA

While reviewing linear algebra, I decided to work on some funny projects that will help me to understand the concepts more deeply. In this repo, I have reconstructed a [600,600,3] RGB image using PCA. To undestand this project, one may need to have solid knowledge of eigendecomposition and covariance matrix. I have another repository where i have included these concepts with python codes, basically using Numpy. Feel free to have a look there!

The input image:

dhoni

with 10 principal components:

screenshot from 2017-06-03 22-09-09

20 Principal Components:

screenshot from 2017-06-03 22-09-53

50 Principal Components:

screenshot from 2017-06-03 22-10-33

100 Principal Components:

screenshot from 2017-06-03 22-11-27

Conclusion:

Using first 100 PCs, a quite good image has been recontructed. Remember our input image had 600 columns, so we had 600 PCs in our eigendecomposition step. So, clearly a great dimension reduction is possible using PCA analysis on any image. Added, in the mean normalising step, you can tune. For example you can use: 0.5 * np.mean(2D_image, axis = 1). It will reduce noise to some extent. Chect it.

I have added two .py files here. One is main fuction to do it and another is my practice file which I did for myself from scratch - but that's too helpful if anyone wants to understand each steps more clearly.

To run the code on ubuntu, save input file "dhoni.jpg" and "image_reconstruction_using_PCA.py" in same directory and run the .py file. However, install all required packages before running the code. You can use this code on any image, with a bit of change, for similar purpose.

Update:

If you are working with rectangular image shapes, just change: axis = None from axis = 0/1. I could change the code and make more generalized but not doing that right now.