0
votes

I want to get the first principal component for an image using the built-in function pca. How can I do that?

I have tried the following code:

[COEFF, SCORE] = pca(image);
SCORE(1:size(SCORE,1),:)=0;
reconstructed_image = SCORE / COEFF + repmat(mean(image,1),size(image,1), 1);
I=reshape(reconstructed_image,[256,256]);
figure
imshow(I,[0 255])

I only get the fist row of the image. Any idea how can I do that correctly?

1

1 Answers

1
votes

You can't "PCA one image". What this did is not give you the first row, it used all rows as observations and your columns as parameters, like you'd usually set up your measurements. So it calculated the variance through all parameters, giving you a vector with the length being equal to your number of columns. You'd probably want more images to do this instead.

Please read the following answer of mine though before continuing, since I explain the main pitfalls of PCA in MATLAB there.

PCA in matlab selecting top n components