![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178799/image.png)
How can I classify the image pixels into two classes only using Kmeans algorithm?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
abdelrahim hashem
am 22 Okt. 2017
Kommentiert: Image Analyst
am 24 Okt. 2018
I have gray-level image, I need to use Kmeans in MatLab to convert this image into binary image.
0 Kommentare
Akzeptierte Antwort
Akira Agata
am 23 Okt. 2017
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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178799/image.png)
10 Kommentare
Image Analyst
am 24 Okt. 2018
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.
Weitere Antworten (1)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!