clustering image, grayscale image

Hello, I have for example image
I want convert to grayscale image, and then use Kmeans method.
Does anyone help ?
Thanks.

Answers (2)

Image Analyst
Image Analyst on 15 Mar 2014
Use rgb2gray() from the Image Processing Toolbox. Then kmeans() from the Statistical Toolbox. Here's a demo: http://www.mathworks.com/products/demos/image/color_seg_k/ipexhistology.html

16 Comments

Tomas
Tomas on 15 Mar 2014
Edited: Tomas on 15 Mar 2014
k = imread('hestain.png');
k = rgb2gray(k);
figure(1),imshow(k);
[idx centroids]=kmeans(double(k(:)),4);
imseg = zeros(size(k,1),size(k,2));
for i=1:max(idx)
imseg(idx==i)=i;
end
imsec(imseg)
??? Error using ==> kmeans>batchUpdate at 436 Empty cluster created at iteration 1.
Error in ==> kmeans at 337 converged = batchUpdate();
Error in ==> Untitled2 at 6 [idx centroids]=kmeans(double(k(:)),4);
I don't understand step 2 - Convert image from RGB color space to L*a*b* color space
cform = makecform('srgb2lab');
lab_he = applycform(he,cform);
How do I use when I have a grayscale image ?
step 3 - Classify the colors in 'a*b*' space using K-means clustering
ab = double(lab_he(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
How do I use when I have a grayscale image ?
Thank for you help
First tell me what you think your two clusters represent. What are the two axes - what are their units?
Tomas
Tomas on 15 Mar 2014
Edited: Tomas on 15 Mar 2014
I don't understand to image processing toolbox, I have to clustering image, according to similar color,
my result
Image Analyst
Image Analyst on 15 Mar 2014
Edited: Image Analyst on 15 Mar 2014
Then why are you converting to grayscale???? It doesn't make sense. Can you explain?
Why don't you try this File Exchange submission that looks like what you want to achieve: http://www.mathworks.com/matlabcentral/fileexchange/37197-dem-diffused-expectation-maximisation-for-image-segmentation
I can not use foreign functions.
I am doing a cluster analysis in the image.
I have to clustering pixel in image.
I do not know how to obtain a position of pixels from the image.
I do clustering in bin,gray and rgb image.
Again, that does not make sense. It's an image. Each pixel has a position specified by what row and columns it's in. What do you mean by "obtain the position of pixels"? For example, look at this small image of only 4 pixels:
123 234
73 37
Now if I were to ask you "give me the position of pixels in that image", what would you answer?
I don't know :), i have to clustering conjoint features in image as color... I so understand
We can't help you unless you can accurately say what you want. The best I could do was to show you a color classification method that seemed to produce something similar to what you showed that you want.
give examples about what I need
in binary images
I,map]=imread('test3','bmp');
I = ~I;
imshow(I,map);
[m n]=size(I)
P = [];
for i=1:m
for j=1:n
if I(i,j)==1
P = [P ; i j];
end
end
end
size(P)
MON=P;
[IDX,ctrs] = kmeans(MON,3)
clusteredImage = zeros(size(I));
clusteredImage(sub2ind(size(I) , P(:,1) , P(:,2)))=IDX;
imshow(label2rgb(clusteredImage))
my output:
I need this also with grayscale and RGB image.
But do we really need to carry out this same conversation in two different threads? Respond to each of them and let me know which one you will continue with and which you will abandon, because I don't want to monitor two threads on the same thing and answer the same thing twice.
we can continue conversation in this threeds. grayscale images seen as a 3-Dimensional matrix, , which consists of two dimensions of the original image x and y axes of the third dimension consists of the luminance values of f (x, y).
my input is at graycale image. when i use kmeans methods my ouput is second on this image
when I will have rgb image my ouput is
after using the kmeans method my ouput is
Thanks for you help
I don't know what your question is, if any. And I'm having trouble matching your mindset. A gray level is a 2D image. Even if you plot it as a 2.5D surface where the height above the plane is the gray level, it's still a 2D image because there are two independent variables (row & column) and one dependent variable (the gray level). In 1D, if I had a vector [3,5,8,16,22] everyone says that's a 1D vector, not a 2D signal just because you could plot that values along the y axis and the index along the x axis. Or consider a sine wave - it's a 1D signal, not a 2D signal just because you plot it in the x,y plane.
I don't how you used kmeans. You simply have a colormap applied. You just say that if the gray level is less than about 64, make it red, and if it's between 64 and 145 it's green, and if it's greater than 145 it's blue. You don't need kmeans for that, you can use ind2rgb(). And I don't know what you did for the color image, for example did you use rgb2gray(), rgb2hsv() and use the v channel, or did you just take one of the color channels, or something else. Anyway, I'm not sure if you have a question or if we're done. I don't have the stats toolbox so I can't actually try any code with kmeans() in it.
this is my code for kmeans image obrazok.zip
if you have time,Try it run script,then you can understand ,what i want do.
main function is metodakkkk.
My problem is, i don't now show my output. I have to show as a image.
Thaks for help.
Sorry I don't understand the language. I don't know what to click, and it started spewing out a bunch of stuff that I didn't know what it was into the command window. It went on for several minutes and I couldn't stop it, not even with control-C. I had to go into task manager to finally kill off MATLAB.
I suggest you try normal debugging operations to figure out what's going on.
Tomas
Tomas on 16 Mar 2014
Edited: Tomas on 16 Mar 2014
Never mind, my program works correctly only when clustering image It takes a long time.
I need, show my output.
I have output in cell.
for example Z= {1438x1} {5800x1} {425x1} {45x1}
in Z{1} is [48] [ 55] [46] ......
in Z{2} is [78] [ 89] [94].....
in Z{3} is [ 120] [ 134] [ 158]...
in Z{4} is [ 210] [ 248] [255].....
how to convert cell, so I could see it as a image.

Sign in to comment.

Boni_Pl
Boni_Pl on 12 Nov 2016
Please anyone upload a matlab code for kmeans clustering of an ultrasound image.

Asked:

on 15 Mar 2014

Answered:

on 12 Nov 2016

Community Treasure Hunt

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

Start Hunting!