MATLAB Answers

Neo
0

Can I use PCA effectively on a greyscale image?

Asked by Neo
on 18 Dec 2015
Latest activity Answered by mugahid albadawy on 9 Feb 2017
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

Show 1 older comment
Sorry I just saw this post Analyst. And you're absolutely right but what qualifies as being an independent component in a grayscale image? Then I can tell you more clearly. I think its easier to distinguish the components in a RGB since by definition, RGB is what those components are. :P
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?
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.

4 Answers

Answer by Walter Roberson
on 18 Dec 2015
 Accepted Answer

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

Thank you.
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"
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.


Answer by 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

If you take an arbitrary grayscale image and apply pca to the whole thing then you probably are not going to get much of interest out. But there are classes of images where the results might be interesting and useful. (Sometimes the useful bit of information would be that "this image is not a member of that class so move on to a different algorithm.")
The key is to have a reason to do the PCA you do, and to know what you are looking for in the results of the PCA.
The situation is not all that different from applying fft2 to arbitrary images: you are probably going to have a lot of difficulty understanding the results unless you know what you are looking for.
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.
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.


Answer by 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

I'd say Walter Roberson was also very high quality as well.

Sign in to comment.


Answer by mugahid albadawy on 9 Feb 2017

i ve already used the same function for dicom image but it's not working properly

  0 Comments

Sign in to comment.