Here's code I got from Spandan, one of the developers of the Image Processing Toolbox at the Mathworks:
Here some quick code for getting principal components of a color image. This code uses the pca() function from the Statistics Toolbox which makes the code simpler.
I = double(imread('peppers.png'));
X = reshape(I,size(I,1)*size(I,2),3);
coeff = pca(X);
Itransformed = X*coeff;
Ipc1 = reshape(Itransformed(:,1),size(I,1),size(I,2));
Ipc2 = reshape(Itransformed(:,2),size(I,1),size(I,2));
Ipc3 = reshape(Itransformed(:,3),size(I,1),size(I,2));
In case you don’t want to use pca(), the same computation can be done without the use of pca() with a few more steps using base MATLAB functions.
Hope this helps.
Also attached are some full demos.