MATLAB: Kmeans with RGB space

kmeans

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?

Best Answer

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.