Can I use PCA effectively on a greyscale image?

7 views (last 30 days)
Hello!
I found this code online:
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,[]);
provided from another commentary form I was reading and I was wondering if there was any code that performed PCA that did not give the pc as a color channel. I don't want to do PCA on the colors of the image composite I want something else (not sure what else is but something not color), so applying PCA to a gray scale image.
Thanks for you any and all suggestions.
  4 Comments
Image Analyst
Image Analyst on 21 Dec 2015
If you think of a PCA coordinate system as a 3-D rotation of an original system, then I think that might help you. Imagine you have a cigar shape gamut (scatterplot of points in RGB color space). If the cigar didn't align with any of the R, G, or B axes but was at some crazy angle, then you could do PCA on it to get a new rotated system where one PC axis does through the main axis of the cigar, and the two others are perpendicular to that and lie in the small cross-sectional plane of the cigar. But with only 1 axis, how can you think that you can rotate it?
What about my last question where I said "better yet, just show your real image and tell us what you want to measure in it and we'll tell you if PCA is the best approach or if there is a better approach." Can you do that, or are you just trying to get a theoretical intuitive understanding of what PCA is and can do?
Neo
Neo on 21 Dec 2015
Edited: Neo on 21 Dec 2015
Oh I see, but just because an image is not RGB, it doesn't necessarily mean that it has only one axis, it might mean that the image has axes that are not related like in a RGB image. And let me get a picture for you to give an idea of what I am talking about actually, great suggestion. I read your response to quickly. Here ya go:

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Dec 2015
Sure. For example,
I = double(imread('cameraman.tif'));
X = reshape(I,[],4);
coeff = pca(X);
This would correlate vertical quarters of the image.
  36 Comments
Image Analyst
Image Analyst on 29 Dec 2015
By the way, here is one of the "average faces" of females constructed from eigenfaces. It's often asserted that the average face is the most attractive. However high fashion models that walk the catwalks seldom have faces that look like "average"
Neo
Neo on 29 Dec 2015
Haha, thanks Analyst. But I am more concerned with how I can feed multiple images into the PCA code so that I can get more than one PC from the image. Do you have an average face answer to that? Cheers, Neo Cornel

Sign in to comment.

More Answers (4)

mugahid albadawy
mugahid albadawy on 9 Feb 2017
i ve already used the same function for dicom image but it's not working properly

Stefan Karlsson
Stefan Karlsson on 27 Dec 2015
I think the answer by Walter, while technically correct, confounds a VERY simple topic. Being technically correct here amounts to what exactly?
  9 Comments
Stefan Karlsson
Stefan Karlsson on 28 Dec 2015
well.... I guess I will have to apologize for my inacuracies. You will get something useful from PCA over a single gray scale image:
you will get the global variance of the image, but that is the only thing. I do not disagree with anything that Walter says, it is technically correct.
Question: What is a Jumbo Jet made up of? I answer: mechanical parts, engines, fuel. While Walter answers by reference to the elements of the periodic table. What is the "right" answer depends only on the context the original question was posed...
(scale selection!) With that I leave this short but fun exchange. Maybe we should quickly note that PCA is actually not the best tool for what the question was about. I would guess Neo should use non-negative matrix factorization, or some other similar flavor. For photographic images, negative values are not allowed in neither basis nor coefficients....
sorry if i was harsh in my original reply.
Image Analyst
Image Analyst on 28 Dec 2015
Nah, you were fine. But is a little frustrating waiting for Neo to verbalize what features he wants to characterize in his image. It's almost like he heard about PCA and thought that it sounded cool and wanted to apply it to his image without considering if it was appropriate or not, or whether there might be better methods. I still don't know what kind of result he would want.

Sign in to comment.


Stefan Karlsson
Stefan Karlsson on 28 Dec 2015
... I guess one can also give another piece of advice to anyone who stumbles onto this looking for info on PCA. Read the posts by Image Analyst. They are as high in quality as they usually are.
  1 Comment
Neo
Neo on 28 Dec 2015
I'd say Walter Roberson was also very high quality as well.

Sign in to comment.


Aya Ahmed
Aya Ahmed on 5 Apr 2020
Neo can you tell me please ..
i want to make feature extraction using PCA ,using matlab code on galaxy grayscale image ,
I was wondering if you could help me with a few steps or even code to make feature extraction from images .
I would like to extract the features of galaxy images and then classify them in the classification learner app.
The data I have is a set of galaxy imagse.
The aim is to extract the features and then compare them in the classification app with each other.
Any help is appreciated!
i want to know how pca work ? does it work in gray images only ??
Thanks ..

Categories

Find more on Dimensionality Reduction and Feature Extraction in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!