how PCA can be applied to an image to reduce its dimensionality with example?
171 views (last 30 days)
Image Analyst on 24 Dec 2014
Edited: Image Analyst on 14 Apr 2020
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.
More Answers (7)
Devan Marçal on 13 Aug 2015
in your example you used PCA in just one image. I have an image bank a total of ~ 800 images. If I make a loop (if, while, etc ..) using the PCA function for each image individually, will be using this command wrong or inefficiently?
Thanks a lot.
Anitha Anbazhagan on 17 Sep 2016
I have 200 ROIs from each of the 50 images. For each ROI, I have 96 feature vectors for four different frequency bands. It seems very high dimensional. How to apply PCA for this? PCA should be applied to data matrix. Do I have to apply for each image or each ROI?
Mina Kh on 11 Dec 2016
Hi. I have multispectral( multi channel) data and I want to apply PCA to reduce the number of channel. Can u give me some hint?Which code i have to use?