K-means for a grayscale image

조회 수: 4 (최근 30일)
anusha reddy
anusha reddy 2016년 5월 29일
편집: Image Analyst 2021년 6월 27일
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
Walter Roberson
Walter Roberson 2018년 10월 24일
imshow(uint8(p))
Image Analyst
Image Analyst 2018년 10월 24일
Try my code (hidden in the comments above), NOT the code that anusha says there is a problem with.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2016년 5월 29일
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
Salma Hassan
Salma Hassan 2021년 6월 27일
편집: Image Analyst 2021년 6월 27일
What about several images? How can I cluster them into k clusters?
Walter Roberson
Walter Roberson 2021년 6월 27일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 4월 16일

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by