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

Dimensionality reduction

3 件のコメント

Ameerah Omar
Ameerah Omar 2015 年 11 月 9 日
i run your code but it is not work with me this error in the picture in the file plz see it and tell me what is the wrong
SHEETAL AGRAWAL
SHEETAL AGRAWAL 2021 年 9 月 14 日
Can I use PCA for grey scale images
Image Analyst
Image Analyst 2021 年 9 月 14 日
@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?

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2014 年 12 月 24 日
編集済み: Image Analyst 2020 年 4 月 14 日
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));
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 件のコメント

G Prasanth Reddy
G Prasanth Reddy 2014 年 12 月 24 日
the image is transformed but we didn't understand what happened. will u please transform the image which I enclosed and expalin it if possible...
Devan Marçal
Devan Marçal 2015 年 10 月 5 日
Hi, the variables Ipc1, Ipc2 and Ipc3 are the first, the second and the third principal components of the Image I or are they the first component of each color channel?
Devan
Image Analyst
Image Analyst 2015 年 10 月 5 日
They are the principal components of the color image. It's like you rotated the R, G, and B axes so that it goes down through the major axis of the 3D color gamut.
pranav
pranav 2016 年 4 月 2 日
How can i create a new file with selected bands if my dimensions are 200x150x50 and i have selected few bands 1,3,5,11,24 and now want to change it to 200x150x5??
Use cat(3, ....)
newImage = cat(3, data(:,:,1), data(:,:,3), data(:,:,5), data(:,:,11), data(:,:,24));
Error using reshape
To RESHAPE the number of elements must not change.
Error in pca1 (line 2)
X = reshape(I,size(I,1)*size(I,2),3);
>>
Munshida P
Munshida P 2020 年 1 月 3 日
Why the above error occured?
Walter Roberson
Walter Roberson 2020 年 1 月 3 日
Whatever you stored in I is not an RGB image. It is either a grayscale image or a CMYK image or an RGBA image, or one of the very very uncommon gray+alpha images.
Ben Grassi
Ben Grassi 2020 年 2 月 13 日
I use a longhand form of this code to generate a range of PCA images, but when I use imwrite() to save the images, they are different to the images displayed by imshow(). Is there a way to ensure the images saved are the same as those displayed?
Image Analyst
Image Analyst 2020 年 2 月 13 日
You can save them with save() instead of imwrite() to save the image variable exactly. Or you can use getframe() to get a bitmap/screenshot of the axes to save a 24 bit version of the image, which may be different than the actual variable. For example a double image in the range of 0-1 and displayed with imshow() will save exactly as a double with save() but if you get its displayed version with getframe() it will be a uint8 in the range of 0-255 since that's what the display adapter uses.
Ben Grassi
Ben Grassi 2020 年 2 月 13 日
Thanks so much for the help, getframe() gave me exactly what I needed.

サインインしてコメントする。

その他の回答 (7 件)

Devan Marçal
Devan Marçal 2015 年 8 月 13 日

0 投票

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 件のコメント

Image Analyst
Image Analyst 2015 年 8 月 14 日
No, it should work fine.
Newman
Newman 2015 年 9 月 27 日
what if the image is binary??IN that case what changes will be on the code?
Image Analyst
Image Analyst 2015 年 9 月 27 日
I don't understand why you'd want to use PCA on a binary image or a single gray level image. Why do you? What are you trying to do? What are the two features? With gray scale and binary images there's only one feature - the gray level - unless you add more images or color channels, so I don't see how PCA can be applied.
Anim Hossain
Anim Hossain 2018 年 4 月 6 日
hello. 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?
Image Analyst
Image Analyst 2018 年 4 月 6 日
Yes it is. See attached demo.
Etworld
Etworld 2019 年 4 月 3 日
Hello, it is weird but, in line 114 it gives inner dimension error.
transformedImagePixelList = listOfRGBValues * coeff;
For 'coloredChips' image example, size(listOfRGBValues)=202538x3
size(coeff)=202538x2
Why do you think is happening ?
Darshan Jain
Darshan Jain 2019 年 7 月 25 日
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)
Etworld, I just ran the colored chips image and it ran fine. Did you change my code at all?
00_Screenshot.png
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];

サインインしてコメントする。

Shaveta Arora
Shaveta Arora 2016 年 1 月 30 日

0 投票

Can I have the pca code used in this color image example

6 件のコメント

Image Analyst
Image Analyst 2016 年 1 月 30 日
See my attached m-file.
Shaveta Arora
Shaveta Arora 2016 年 1 月 31 日
thanks
Shaveta Arora
Shaveta Arora 2016 年 1 月 31 日
Dear Analyst In the attachment pca_image.m, pls share the function pca(X); as I ma having error : Undefined function or method 'pca' for input arguments of type 'double'.
Image Analyst
Image Analyst 2016 年 1 月 31 日
You must not have the Statistics and Machine Learning Toolbox.
Shaveta Arora
Shaveta Arora 2016 年 1 月 31 日
Might possible. Pls share this pca function to save in my folder.
Image Analyst
Image Analyst 2016 年 1 月 31 日
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.

サインインしてコメントする。

Anitha Anbazhagan
Anitha Anbazhagan 2016 年 9 月 17 日

0 投票

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 件のコメント

Image Analyst
Image Analyst 2016 年 9 月 17 日
It depends on if you want PCA components on each image individually, or the PCA components of the group as a whole.

サインインしてコメントする。

Mina Kh
Mina Kh 2016 年 12 月 11 日

0 投票

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?
Arathy Das
Arathy Das 2016 年 12 月 20 日

0 投票

How can i extract three texture features among the 22 using PCA?

1 件のコメント

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.

サインインしてコメントする。

joynjo
joynjo 2018 年 3 月 24 日

0 投票

How to visualize the result of PCA image in pseudocolor?

1 件のコメント

imshow(PC1); % Display the first principal component image.
colormap(jet(256));

サインインしてコメントする。

F M Anim Hossain
F M Anim Hossain 2018 年 4 月 6 日

0 投票

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?

カテゴリ

ヘルプ センター および File ExchangeDimensionality Reduction and Feature Extraction についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by