K-means on a gray scale image
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (1)
Image Analyst
il 24 Ott 2018
3 voti
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 Commenti
Iremsu Savas
il 25 Ott 2018
Image Analyst
il 25 Ott 2018
Not exactly sure what that means. But neither you or I mentioned anything "wrong".
pooja s
il 11 Feb 2020
THANK U SO MUCH SIR . THIS HELPED ME A LOT
Mnahil Khursheed
il 1 Set 2020
How to save the result of segmentation is a folder for feature extraction further. Any one please help me.
Image Analyst
il 2 Set 2020
Call imwrite() or save().
Categorie
Scopri di più su k-Means and k-Medoids Clustering in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!