How can I classify the image pixels into two classes only using Kmeans algorithm?

I have gray-level image, I need to use Kmeans in MatLab to convert this image into binary image.

 Accepted Answer

I think the solution would be like this:
% Read gray-scale image
I = imread('cameraman.tif');
% Clustering by k-means
x = double(I(:));
[idx, center] = kmeans(x,2);
% Make binary image
[nrow,ncol] = size(I);
labels = reshape(idx,nrow,ncol);
BW = labels == 1;
% Show the result
imshowpair(I,BW,'montage')

10 Comments

Thanks for you. But why you defined BW by label equal 1 values, not 2 values??
Thanks for your comment. The definition of BW = 0/1 in my script is just an example. There is no reason to chose the label = 1.
I mean, how can I know the white pixels belong to any class derived by Kmeans? I need to explore the white pixels, which of the class (1 or 2)?
OK. To identify the class of the blight pixels, you have to compare some parameters, such as average value, for each group, like:
if mean(I(labels == 1)) > mean(I(labels == 2))
BW = labels == 1;
else
BW = labels == 2;
end
This is because the initial position of centroid of K-means are randomly determined.
Do you mean: else BW = labels == 0; %instead of 2
No. The value of each element of labels in the above script is 1 or 2.
I am so sorry for my questions, but now I need to assign the new image represent the two clusters; white cluster and black cluster. So I thought I should define in previous code " else BW = labels == 0 "
labels will never equal zero. So depending on what you want to be foreground (white, true, 1) you can either set it to the pixels that are label 1
BW = labels == 1;
or to the pixels that have label 2:
BW = labels == 2;
Note that there is no guarantee with kmeans which label (1 or 2) will be the dark pixels and which label will be the bright pixels. That's due to the nature of kmeans.
why do we need to convert the image to the double version?
When computing distances between points, you don't want the result to be integer because the precision won't be that accurate, and if the inputs were integers the output would be also.
I guess they want you to do the conversion before calling rather than the function doing it itself internally.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!