Hello Pooja, 
for performing PCA it is recommended to convert the images into greyscale to reduce the dimensionality. 
Next, the covariance matrix can be found by directly using the MATLAB in-built function “cov”. This can then be used to find the eigen values and hence the principal components from the images. 
You may refer to the following MATLAB code for more understanding:  
imageFiles = dir(fullfile(imageDir, '*.jpg')); 
numImages = length(imageFiles); 
sampleImage = imread(fullfile(imageDir, imageFiles(1).name)); 
[imageHeight, imageWidth] = size(rgb2gray(sampleImage)); 
X = zeros(imageHeight * imageWidth, numImages); 
    img = imread(fullfile(imageDir, imageFiles(i).name)); 
X_centered = X - mean_face; 
cov_matrix = cov(X_centered'); 
[eigenvectors, eigenvalues_matrix] = eig(cov_matrix); 
eigenvalues = diag(eigenvalues_matrix); 
[sorted_eigenvalues, index] = sort(eigenvalues, 'descend'); 
sorted_eigenvectors = eigenvectors(:, index); 
principal_components = sorted_eigenvectors(:, 1:k); 
The following links might be helpful for better understanding of “cov”, “eig”, and “diag” functions: 
You can also refer to the attached output for better understanding, the principal components variable is highlighted: