~ 1 ~
ECES 682 – IMAGE PROCESSING
PROJECT 2 - REPORT
PROJECT TITLE
Principal Component Analysis
TEAM MEMBERS
Chetan Rao Namrata Baranwal
GOAL OF THE PROJECT
To implement and test the Principal Component transforms.
~ 2 ~
TABLE OF CONTENTS
S NO. CONTENTS PAGE NO.
1 Definition 4
2 Algorithm and Its Visualization 4
3 Results 7
4 Applications 11
5 Conclusion 11
6 Appendix 12
7 References 15
~ 3 ~
TABLE OF FIGURES
FIG NO CONTENTS PAGE NO
1 RGB components of an image 4
2 3-D Column Vector Xi 4
3 Vector X 5
4 Mean Vector = mR, mG, mB 5
5 Covariance Matrix with its Eigen Values 5
and Eigen Vectors
6 Transformation Matrix A 6
7 Principal Components 6
8, 10(a), 11(a) Multispectral Image 7, 8, 9
9 Principal Components of Figure 8 7
10(b) Reconstructed Images using the first 8
2 Principal Components
11(b) Reconstructed Images using only the 9
1st Principal Component
12 Galaxy 1: RGB Components and 3 PCs 10
13 Galaxy 2: RGB Components and 3 PCs 10
14 Galaxy 3: RGB Components and 3 PCs 10
15 Sun: RGB Components and 3 PCs 11
~ 4 ~
PRINCIPAL COMPONENT ANALYSIS
DEFINITION:
Principal Component Analysis is a feature extraction technique .It is also called as
Karhunen–Loève transform. The aim of principal component transform is to transform a
correlated set to an orthogonal set of variables called principal components. It reveals the
internal structure of the data in a way which best explains the variance in the data.
ALGORITHM AND ITS VISUALIZATION:
3D Column Vector X:
o An RGB image having 3 components can be treated as a unit by expressing
each group of three corresponding pixels as a vector Xi, representing a pixel i.
o The 3-D column vector Xi for a pixel = [r g b]’, where r, g and b are the
respective pixel values of the RGB components of the image.
o Similarly, Extend it to the spectral band components where instead of RGB, we
have b1, b2 …., bn; where b1, b2,…….bn are the respective pixel values of the
spectral band image components.
o For PCA, the input vector X is a vector of vector Xi.
o For an m x n image, the vector X will be of length mn x number of bands.
Figure 1: RGB components of an Image
Figure 2: 3D Column Vector Xi
~ 5 ~
Mean Vector m:
o Find the mean of vector over the bands.
Covariance Matrix Cx:
o Calculate the covariance matrix of vector X which is denoted by Cx.
Eigen values and Eigen vectors :
o Calculate the Eigen values and Eigen vectors of the covariance matrix Cx.
o Arrange the Eigen vectors in the decreasing order of the respective Eigen
values.
Eigen Values
Covariance Matrix
Eigen Vector
Figure 5: Covariance Matrix with its Eigen Value and Eigen Vectors
1, 1, :
1, 2, :
... 1, N, : ……
N, 1, :
N, 2, :
… N, N, :
Figure 3: Vector X
MEAN
Figure 4: Mean Vector = mR, mG, mB
~ 6 ~
Transformation matrix A:
o Generate a transformation matrix A using the Eigen vectors as the rows.
Figure 6: Transformation Matrix A
Transformed matrix Y :
o The transformed matrix Y = A*(X-m).
o Rearrange Y to obtain the principal components of the image (Matrix PCIM).
The first component of matrix PCIM will correspond to the maximum variance.
The second component of matrix PCIM will correspond to the second highest
variance and so on.
Figure 7: Principal Components
~ 7 ~
RESULTS:
We took the multispectral satellite images from Gonzales and Woods and performed
Principal Component Analysis.
Figure 8: Multispectral Satellite image.
Figure 8 contains the multispectral images of a satellite image given in the book Gonzales
and Woods. It corresponds to six spectral bands: Visible Blue (450 – 520 nm), visible green
(520 – 600 nm), visible red (630 – 690 nm), near infrared (760 – 900 nm), middle infrared
(1550 – 1750 nm) and thermal infrared (10400 – 12500 nm).
Figure 9: Principal Components of Figure 8
Figure 9 shows the six principal components of Figure 8 using the algorithm described above.
B1 Component B2 Component B3 Component
B4 Component B5 Component B6 Component
Principle Component 1 Principle component 2 Principle Component 3
Principle Component 4 Principle component 5 Principle Component 6
~ 8 ~
RECONSTRUCTING THE ORIGINAL IMAGES USING PCs:
Amongst the principal components, the first few carry the most information. The first two
principal components in Figure 9 account for approximately 89% of the variance. Using
these components, it is possible to reconstruct the original images with almost zero error.
Figure 10(a): Original Multispectral image
Figure 10(b): Reconstructed Image using Principal component 1 and 2
This reconstruction using the first two principal components (Refer Figure 10(b)) can be
achieved by considering the Eigen vectors corresponding to the first two principal
components in the transformation matrix and applying the following equation:
B1 Component B2 Component B3 Component
B4 Component B5 Component B6 Component
Regenerated B1 Regenerated B2 Regenerated B3
Regenerated B4 Regenerated B5 Regenerated B6
~ 9 ~
X’ = A’Y’+M
X’ = Reconstructed Vector
A’ = New Transformation matrix
Y’ = Matrix with only the first two principal components
M = Mean matrix The first five images closely resemble the first five bands of the multispectral image. The
sixth reconstructed image is sharper than the original image due to the loss of blurry details.
We went ahead and used the first principal component to reconstruct the image. It is
observed that not all the reconstructed images are identical to the original images (Refer
Figure 11(b)).The first three are almost identical to the original image components while the
last three have major variability.
Figure 11(a): Original Multispectral image
Figure 11(b): Reconstructed Images using only the first Principal Component
B1 Component B2 Component B3 Component
B4 Component B5 Component B6 Component
Regenerated B1 Regenerated B2 Regenerated B3
Regenerated B4 Regenerated B5 Regenerated B6
~ 10 ~
Figure 12 Row 1: RGB Components and Row 2: 3 Principal Components
Figure 13: Row 1: RGB Components and Row 2: 3 Principal Components
Figure 14: Row 1: RGB Components and Row 2: 3 Principal components
Red Component Green Component Blue Component
Principle Component 1 Principle component 2 Principle Component 3
Red Component Green Component Blue Component
Principle Component 1 Principle component 2 Principle Component 3
Red Component Green Component Blue Component
Principle Component 1 Principle component 2 Principle Component 3
~ 11 ~
Applications:
Data Compression and Restoration:
The losses in the data due to compression by PCA are quite less because PCA retains all the
important information and most of the information can be reconstructed from the principal
components.
Facial Recognition:
PCA identifies the important features in a face and hence can be used for facial recognition
since it can match 2 images with the same principal components even if they are taken from
different angles. A face reconstructed using PCA will be almost similar to the original
image.
Conclusion:
From the results obtained, we find that even if we keep the first three principal components,
we get a good reconstruction of the images. Hence, PCA is a good approach for extracting
features. Also, PCA’s dimensionality reduction feature can be used for compression.
~ 12 ~
Appendix: Code:
%% PROJECT 2: PRINCIPAL COMPONENT ANALYSIS % By Chetan Rao, Namrata Baranwal % Image Processing ECES 682
clear all; close all; %% Read Band Images %image1= imread('PIA15260.jpg'); % image1= imread('Sunimsal.jpg'); % image1= imread('gravitational lensing.jpg');
image1(:,:,1)= imread('1.tif'); image1(:,:,2)= imread('2.tif'); image1(:,:,3)= imread('3.tif'); image1(:,:,4)= imread('4.tif'); image1(:,:,5)= imread('5.tif'); image1(:,:,6)= imread('6.tif');
%% Plot the band components subplot(2,3,1); imshow(image1(:,:,1)) title('B1 Component') subplot(2,3,2); imshow(image1(:,:,2)) title('B2 Component') subplot(2,3,3); imshow(image1(:,:,3)) title('B3 Component') if(size(image1,3)==6) subplot(2,3,4); imshow(image1(:,:,4)) title('B4 Component') subplot(2,3,5); imshow(image1(:,:,5)) title('B5 Component') subplot(2,3,6); imshow(image1(:,:,6)) title('B6 Component') end
%% 3D Column Vector x image(:,1) = image1(1,:,1); image(:,2) = image1(1,:,2); image(:,3) = image1(1,:,3); if(size(image1,3)==6) image(:,4) = image1(1,:,4); image(:,5) = image1(1,:,5); image(:,6) = image1(1,:,6); end for i=2:size(image1,1) im = image1(i,:,:); imtemp(:,:) = im(1,:,:); im = imtemp; image=[image; im]; end
x= image;
~ 13 ~
clear imtemp image;
%% Mean m=mean(x); M =repmat(m',1,length(x));
%% Covariance Matrix c=cov(double(x));
%% Eigen Vectors and Eigen Values [evc eic]=eig(c); [trash,I] = sort(sum(eic)); evc = evc(:,I);
%% Transformation Matrix A= evc';
%% Trasformed PC y=A*(double(x)'-M);
%% Image Principal Components j=1; k=1; for i=1:length(y) ytemp =y(:,i); pcim(k,j,:) = fliplr(ytemp'); j=j+1; if(mod(i,size(image1,2))==0) j=1; k=k+1; end end
clear ytemp; %% Plot Principal Components figure subplot(2,3,1); imshow(pcim(:,:,1),[-128,128]) title('Principal Component 1 ') subplot(2,3,2); imshow(pcim(:,:,2),[-128,128]) title('Principal component 2') subplot(2,3,3); imshow(pcim(:,:,3),[-128,128]) title('Principal Component 3') if(size(image1,3)==6) subplot(2,3,4); imshow(pcim(:,:,4),[-128,128]) title('Principal Component 4 ') subplot(2,3,5); imshow(pcim(:,:,5),[-128,128]) title('Principal component 5') subplot(2,3,6); imshow(pcim(:,:,6),[-128,128]) title('Principal Component 6') end
~ 14 ~
%% Regenerate using only few Principal Components
newA=A; newY=y;
% Except for 1st and 2nd PC all others are made zero. newY(1,:) = zeros(1,length(y)); newA(1,:) =zeros(1,length(A)); newY(2,:) = zeros(1,length(y)); newA(2,:) =zeros(1,length(A));
if(size(image1,3)==6) newY(3,:) = zeros(1,length(y)); newA(3,:) =zeros(1,length(A)); newY(4,:) = zeros(1,length(y)); newA(4,:) =zeros(1,length(A)); end
py = newA'*newY + M;
j=1; k=1; for i=1:length(py) ytemp =py(:,i); pcoim(k,j,:) = ytemp; j=j+1; if(mod(i,size(image1,2))==0) j=1; k=k+1; end end
%% Plot Reconstructed images using Principal Components figure subplot(2,3,1); imshow(uint8(pcoim(:,:,1))) title('Reconstructed B1') subplot(2,3,2); imshow(uint8(pcoim(:,:,2))) title('Reconstructed B2') subplot(2,3,3); imshow(uint8(pcoim(:,:,3))) title('Reconstructed B3') if(size(image1,3)==6) subplot(2,3,4); imshow(uint8(pcoim(:,:,4))) title('Reconstructed B4') subplot(2,3,5); imshow(uint8(pcoim(:,:,5))) title('Reconstructed B5') subplot(2,3,6); imshow(uint8(pcoim(:,:,6))) title('Reconstructed B6') end
~ 15 ~
References:
[1] Digital Image processing – Gonzales, Woods. 2nd edition – 2002 – ISBN: 0201180758
[2] Reconstruction of Walking People Images by Principal Component Analysis –Zhipeng Zhao
[3] Space Images - http://photojournal.jpl.nasa.gov/gallery/universe
[4] Image Database - http://imageprocessingplace.com/root_files_V3/image_databases.htm