필터 지우기
필터 지우기

K-means on a gray scale image

조회 수: 3 (최근 30일)
Iremsu Savas
Iremsu Savas 2018년 10월 24일
댓글: Image Analyst 2020년 9월 2일
Hello everyone, I have a grayscale image 'lena.bmp' and I am trying to apply k-means color quantization. I found the following code but I have some points that are not clear in my mind: 1)Why we need to convert the image format to double? 2)While I am writing "pixel_labels = reshape(cluster_idx,nrows,ncols);" in the code what excatly it is doing? 3)Why did I need to write the square brackets inside this "imshow(pixel_labels,[])" ? The code is here:
Image=imread('lena.bmp');
Image = im2double(Image);
nrows = size(Image,1);
ncols = size(Image,2);
Image = reshape(Image,nrows*ncols,1);
Colors = 4;
[cluster_idx, cluster_center] = kmeans(Image,Colors,'distance','sqEuclidean');
pixel_labels = reshape(cluster_idx,nrows,ncols);
imshow(pixel_labels,[]);
Thank you so much.

채택된 답변

Adam
Adam 2018년 10월 24일
1. kmeans only supports double or single inputs. It works in floating point so integers wouldn't make sense.
2.
doc reshape
will answer this. It does pretty much exactly what it says - it reshapes your output back to the number of rows and columns of your image so that you have an image back rather than just a vector of class indices.
3.
doc imshow
describes the different scaling you can do with imshow. This syntax is the 3rd in the list.

추가 답변 (1개)

Image Analyst
Image Analyst 2018년 10월 24일
See my attached demos for kmeans in both color and grayscale. Write back if you still have questions.
By the way, you can't do "color quantization" on a grayscale image, unless you're considering the gray levels as "colors".
  댓글 수: 5
Mnahil Khursheed
Mnahil Khursheed 2020년 9월 1일
How to save the result of segmentation is a folder for feature extraction further. Any one please help me.
Image Analyst
Image Analyst 2020년 9월 2일
Call imwrite() or save().

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

Community Treasure Hunt

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

Start Hunting!

Translated by