Can I use PCA effectively on a greyscale image?
Mostrar comentarios más antiguos
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 comentarios
Image Analyst
el 21 de Dic. de 2015
Well with an RGB image, there are 3 independent features - the R, G, and B values. Do you have multiple independent components in your grayscale image? If so, what are they? You must have some reason for thinking you can do this, so . . . what is it? Or 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. As it is, I don't see what PCA can get you.
Neo
el 21 de Dic. de 2015
Image Analyst
el 21 de Dic. de 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?
Respuesta aceptada
Más respuestas (4)
mugahid albadawy
el 9 de Feb. de 2017
1 voto
i ve already used the same function for dicom image but it's not working properly
Stefan Karlsson
el 27 de Dic. de 2015
0 votos
I think the answer by Walter, while technically correct, confounds a VERY simple topic. Being technically correct here amounts to what exactly?
9 comentarios
Neo
el 27 de Dic. de 2015
Image Analyst
el 28 de Dic. de 2015
But we're still trying to figure out what you think the PCs should represent.
Neo
el 28 de Dic. de 2015
Stefan Karlsson
el 28 de Dic. de 2015
1. A single RGB image, of W x H size. Is there a way to perform PCA in a meaningful way?
answer: yes. the first PC-basis is usually the Brightness component in natural images. Of course, you can create images where this is not true, by forcing all pixel values to have the same brightness you will make the brightness component be the last PC instead of the first.
2. A single gray scale image, of W x H size. Is there any way to do PCA in a meaningful way?
Answer: no.
3. Performing multiple feature extractions, over local regions of a gray-scale image I can gather a "feature image". Can i do PCA on this image in a meaningful wau?
answer: yes. The easist example is if you gather 3 features over each local region. The resulting feature image can then even be visualized as an RGB image. On this image you can do PCA. As example, you can collect the TRIPLET: (mean, median, variance). While these feature are independent, you would expect a high correlation between mean and median.
Do not confuse this with the kind of PCA that is performed on databases of images. In that case, each image is an observable (what Image Analyst called a "feature" above). If you have 100 gray scale images, you can do PCA on the collection of those images as a whole. In this case, the dimensionality is equal to the number of pixels in the images, and the images must be normalized to the same number of pixels(by resizing usually).
Walter Roberson
el 28 de Dic. de 2015
I have to disagree on the "Answer: no" of #2. As you explored in #3, feature extraction can be done over the single image and the resulting features can be PCA'd: that is a "meaningful" way of doing PCA on a single grayscale image. The features can be extracted in multiple ways, including some ways that come down to geometric subsets. It would be uncommon that vertical quarters would have useful information but finer grained localities certainly might.
For example, if an image happens to be a multiple of 8 pixels horizontally and vertically, then break down the image into 8 x 8 sub-blocks and reshape() that into N x 64 and pca() that. The information so obtained about how the pixels in the block tend to relate to each other can be used for image compression.
More generally, breaking into P x Q blocks, permute and reshape to N x (P*Q), PCA, and the information you get out can be interpreted as texture information; you can probably vary P and Q over a range to get an idea of what the texture size is. (Though perhaps an FFT approach might be easier for that, I am not sure.)
pca() of an entire grayscale image without reshaping or extraction could potentially be meaningful for detecting texture.
Image Analyst
el 28 de Dic. de 2015
But Walter, that would be creating multiple features from an image, like measurements, or chunks of the image. And we agree with that - if you derive the right information from a single image, it can mean something. But if you don't, it's meaningless.
But I think Stefan was referring to doing PCA on just a single gray level image where the only feature is the gray levels - in that case you'd have a single PC and that is the gray level itself. If you divide the image up into meaningful regions like strips of 8 pixels wide, and that corresponds to some pattern in the image, then it might mean something. But if you're not smart about it and just arbitrarily divide, say, the cameraman demo image up into vertical strips 16 pixels wide, or the moon demo image up into quadrants, or the cell microscope image up into a checkerboard, then the PC of those things would probably not be meaningful -- you'd get something but it might not be interesting or useful to you. To be meaningful you have to create meaningful features from your input image in the first place. I know you'd agree with that.
Walter Roberson
el 28 de Dic. de 2015
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.
Stefan Karlsson
el 28 de Dic. de 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
el 28 de Dic. de 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.
Stefan Karlsson
el 28 de Dic. de 2015
0 votos
... 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 comentario
Neo
el 28 de Dic. de 2015
Aya Ahmed
el 5 de Abr. de 2020
0 votos
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 ..
Categorías
Más información sobre Dimensionality Reduction and Feature Extraction en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


