MATLAB Answers


How do i convert RGB to Gray using the PCA

Asked by Jorge
on 16 May 2013
Latest activity Commented on by Image Analyst
on 2 Jul 2016

I'm trying to convert a image from rgb scale to gray with the pca. How can i do that? (I do not want to use rgb2gray).



Log in to comment.

3 Answers

Answer by Image Analyst
on 21 May 2013

I've "done" it in one project where the 3D color gamut was like an ellipsoid that was not aligned with any RGB or HSV axis, and not going along the R=G=B line either. But I was working with a statistician and he said he could do it so I just wrote out all the colors to a N by 3 array and he gave me a formula. Since it was an ellipse, I had PC1 that went along the major axis of the long skinny ellipsoid, and PC2 and PC2 that were perpendicular to that and went out along the short circular cross section of the ellipsoid. PC2 and PC3 didn't have any meaningful interpretation so I just used PC1 to get the gray level. It was basically the amount of the way I was between some sort of blue color, and white. I was wanting to quantify the amount of blue crystals mixed in with a bunch of white crystals. You'd do the same thing - to go to gray you'd want to use the PC1 (the first principal component) only. If you want my opinion, you can post your color image and I could examine the 3D color gamut to see if PCA even really makes sense for your image. I could post the 3D image of your gamut. For example, look at the 3D color gamut of the standard MATLAB demo image "pears":

By looking at the shape of this gamut, you can immediately see how a PCA approach might give a more meaningful grayscale image than the standard book formula like rgb2gray() uses, at least as far as quantification via image analysis goes, though maybe not as far as matching up with the human visual system.


Log in to comment.

Answer by Walter Roberson
on 16 May 2013

I figure,

Idub = double(YourImage);
rdub = Idub(:,:,1);
gdub = Idub(:,:,2);
bdub = Idub(:,:,3);
r2 = sum(rdub(:).^2);
g2 = sum(gdub(:).^2);
b2 = sum(bdub(:).^2);
rgbtot = sqrt(r2 + g2 + b2);
rfac = sqrt(r2) ./ rgbtot;
gfac = sqrt(g2) ./ rgbtot;
bfac = sqrt(b2) ./ rgbtot;
YourGrey = cast(rdub .* rfac + gdub .* gfac + bdub .* bfac, class(YourImage));


I would like to know more about that algortihm, cause it showed some interesting results. Can you give some theorical references about this expressions? That is the algorithm for the PCA?

Oh gosh, I must have been awake when I programmed that ;-)

I remember it has something to do with scaling by the norm(), but at the moment I can't quite remember why I calculated the norm() that way.

I do not know if it is the same as PCA would use in this case.

Log in to comment.

Answer by hossein
on 2 Jul 2016
Edited by hossein
on 2 Jul 2016

lab = rgb2lab(im);
f = 0;
wlab = reshape(bsxfun(@times,cat(3,1-f,f/2,f/2),lab),[],3);
[C,S] = pca(wlab);
S = reshape(S,size(lab));
S = S(:,:,1);
gray = (S-min(S(:)))./(max(S(:))-min(S(:)));


by using code:

Don't use gray - it's a built in function. And that last 2 lines could be done simpler as

grayImage = mat2gray(S(:,:,1));

mat2gray() is a built-in function that scales max to min as 0 to 1, like your formula.

Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!