kmeans with RGB space

1 view (last 30 days)
Cristina
Cristina on 29 Jul 2011
Dear colleague,
I have a problem with function kmeans. I have used it with RGB space but it don´t run. I did:
1) Y=imread('1.jpg.'); where Y is RGB image (1960x1960x3)
r=Y(:,:,1);
g=Y(:,:,2);
b=Y(:,:,3);
vector=[r(:) g(:) b(:)];
[Idx,c]=kmeans(vector,3); because I want three regions.
However it don´t run good. It give Idx as a vector with centroid in place of a label matrix.
Below I tried this :
A=zeros (3841600,3);Y=double(Y);
A(:,1)=Y(:,:,1);
A(:,2)=Y(:,:,2);
A(:,3)=Y(:,:,3)
but it isn´t correct.
Finally I tried to change it directly with:
A=reshape(Y,3841600,1,3)
but it didn´t run.
What is the matter? Could anyone help me, please?

Accepted Answer

Image Analyst
Image Analyst on 29 Jul 2011
Follow MATLAB's turorial here http://www.mathworks.com/products/demos/image/color_seg_k/ipexhistology.html The only difference is that their works in lab color space while you want to work in rgb color space, so just skip the rgb to lab conversion step in their demo.
  2 Comments
Cristina
Cristina on 1 Aug 2011
Dear colleague, Thanks for the reply. The solution is: Always that I use kmeans function I must convert the RGB space to lab color space. Why does not kmeans function run directly with the RGB space? I want to capture features of RGB space of image
Image Analyst
Image Analyst on 6 Nov 2011
Like I said, it will work on RGB color space also. I don't know why you say that you must convert to lab color space.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!