K-means for a grayscale image

3 visualizaciones (últimos 30 días)
anusha reddy
anusha reddy el 29 de Mayo de 2016
Editada: Image Analyst el 27 de Jun. de 2021
I've tried the below code to cluster the grayscale image,
I = imread('sym_059.tif');
I = im2double(I);
c = kmeans(I, 3);
p = reshape(c, size(I));
executing this code, I am getting error as follows "Error using reshape-To RESHAPE the number of elements must not change." How can I debug this.? Help appreciated.
  8 comentarios
Walter Roberson
Walter Roberson el 24 de Oct. de 2018
imshow(uint8(p))
Image Analyst
Image Analyst el 24 de Oct. de 2018
Try my code (hidden in the comments above), NOT the code that anusha says there is a problem with.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 29 de Mayo de 2016
kmeans returns a vector of cluster indices, one index per row of input. You are trying to reshape that as if it had as many entries as the number of pixels in your image.
Possibly you want to try
c = kmeans(I(:), 3);
  21 comentarios
Salma Hassan
Salma Hassan el 27 de Jun. de 2021
Editada: Image Analyst el 27 de Jun. de 2021
What about several images? How can I cluster them into k clusters?
Walter Roberson
Walter Roberson el 27 de Jun. de 2021
Provided the images are the same number of pixels, and are all RGB or are all grayscale, then construct an array in which each row is reshape() of an image into a single row, and the rows correspond to different images. Then kmeans() .
This would attempt to cluster the images as a whole into clusters.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 16 de Abr. de 2021

Categorías

Más información sobre Images en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by