# how PCA can be applied to an image to reduce its dimensionality with example?

161 visualizzazioni (ultimi 30 giorni)
G Prasanth Reddy il 24 Dic 2014
Commentato: Image Analyst il 14 Set 2021
Questo/a domanda è stato/a segnalato/a da Walter Roberson
Dimensionality reduction
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
SHEETAL AGRAWAL il 14 Set 2021
Can I use PCA for grey scale images
Image Analyst il 14 Set 2021
@SHEETAL AGRAWAL, perhaps. You obviously need at least two features. What would be your two features? Maybe gray level is one, but what is the other? Or do you just have two different features, like blob area and blob texture or brightness?

Accedi per commentare.

### Risposta accettata

Image Analyst il 24 Dic 2014
Modificato: Image Analyst il 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.
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));
figure, imshow(Ipc1,[]);
figure, imshow(Ipc2,[]);
figure, imshow(Ipc3,[]);
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.
-Spandan
Also attached are some full demos.
##### 12 CommentiMostra 10 commenti meno recentiNascondi 10 commenti meno recenti
Ben Grassi il 13 Feb 2020
Thanks so much for the help, getframe() gave me exactly what I needed.
Aya Ahmed il 4 Mar 2020

Accedi per commentare.

### Più risposte (7)

Devan Marçal il 13 Ago 2015
Hi,
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.
Devan
##### 8 CommentiMostra 6 commenti meno recentiNascondi 6 commenti meno recenti
Darshan Jain il 25 Lug 2019
Hey @ImageAnalyst,
I checked out your script, I had a small question, How could I plot the colored image back in three plots (showing approximation by pca1, then pca1 and pca2 and then followed by pca1, pca2 and pca3).
I tried doing using the imfuse comand "imfuse(pca1,pca2)", the clarity improved well, but i'm not able to reproduce the same colors. (see the attached image)
I think this is because I need to normalize the data, and then un-normalize it back before plotting. (I'm not sure though)
Image Analyst il 25 Lug 2019
Etworld, I just ran the colored chips image and it ran fine. Did you change my code at all?
Darshan: where did your colors come from? I don't understand what your "approximations" are supposed to be. But anyway, you can stitch images side by side if they are all RGB images to begin with:
wideImage = [rgbImage1, rgbImage2, rgbImage3];

Accedi per commentare.

Shaveta Arora il 30 Gen 2016
Can I have the pca code used in this color image example
##### 6 CommentiMostra 4 commenti meno recentiNascondi 4 commenti meno recenti
Shaveta Arora il 31 Gen 2016
Might possible. Pls share this pca function to save in my folder.
Image Analyst il 31 Gen 2016
I can't. It would not be legal. You either have to buy the toolbox from the Mathworks, or implement it yourself from low level code.

Accedi per commentare.

Anitha Anbazhagan il 17 Set 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?
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Image Analyst il 17 Set 2016
It depends on if you want PCA components on each image individually, or the PCA components of the group as a whole.

Accedi per commentare.

Mina Kh il 11 Dic 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?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Arathy Das il 20 Dic 2016
How can i extract three texture features among the 22 using PCA?
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Image Analyst il 20 Dic 2016
I think you should start your own discussion with your own data or images. If you have 22 PCA columns, then just extract the 3 you want as usual.
pca3 = pca22(:, 1:3); % or whatever.

Accedi per commentare.

joynjo il 24 Mar 2018
How to visualize the result of PCA image in pseudocolor?
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Image Analyst il 24 Mar 2018
imshow(PC1); % Display the first principal component image.
colormap(jet(256));

Accedi per commentare.

F M Anim Hossain il 6 Apr 2018
I'm new to the concept of PCA. I'm trying to develop something that can recognize color features from different images. Is it possible to do it with the help of PCA?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Dimensionality Reduction and Feature Extraction in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by