How do i convert RGB to Gray using the PCA

2 ビュー (過去 30 日間)
Jorge
Jorge 2013 年 5 月 16 日
コメント済み: Image Analyst 2016 年 7 月 2 日
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).
Thanks

回答 (3 件)

Image Analyst
Image Analyst 2013 年 5 月 21 日
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.

Walter Roberson
Walter Roberson 2013 年 5 月 16 日
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));
  2 件のコメント
Jorge
Jorge 2013 年 5 月 20 日
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?
Walter Roberson
Walter Roberson 2013 年 5 月 20 日
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.

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


hossein
hossein 2016 年 7 月 2 日
編集済み: hossein 2016 年 7 月 2 日
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(:)));
  2 件のコメント
hossein
hossein 2016 年 7 月 2 日
by using code:
Image Analyst
Image Analyst 2016 年 7 月 2 日
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.

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

カテゴリ

Help Center および 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