Image clustering by Gaussian mixture models
Show older comments
I want to use GMM(Gaussian mixture models for clustering a binary image and also want to plot the cluster centroids on the binary image itself.
This is my initial code
I=im2double(imread('sil10001.pbm'));
K = I(:);
mu=mean(K);
sigma=std(K);
P=normpdf(K, mu, sigma);
Z = norminv(P,mu,sigma);
X = mvnrnd(mu,sigma,200);
scatter(X(:,1),X(:,1),10,'ko');
options = statset('Display','final');
gm = fitgmdist(X,1,'Options',options);
idx = cluster(gm,X);
cluster1 = (idx == 1);
cluster2 = (idx == 2);
figure;
scatter(X(cluster1,1),X(cluster1,1),10,'r+');
hold on
scatter(X(cluster2,1),X(cluster2,1),10,'bo');
hold off
legend('Cluster 1','Cluster 2','Location','NW')
But the problem is that the output is coming like a straight line and not like the figures which are mentioned in the mat-lab reference .And also I am unable to plot the cluster centroids received from GMM in the primary binary image.How do i do this?

I want the output to be like this The contours represent the position of the cluster centroids
Answers (1)
missagh dayer
on 6 May 2017
Edited: missagh dayer
on 6 May 2017
Hi, Newman. I ran your code on my own computer and detected a couple of problems:
Problem #1: You used this line:
gm = fitgmdist(X,1,'Options',options);
From what I gather you want to perform clustering on an image, so why are you performing it on X? X is a randomly created vector... You need to perform the clustering on P! So your code would look like this:
gm = fitgmdist( P,1,'Options',options);
(P.S why are using only 1 cluster in you code? Unless you use 2 or more, this code will have no effect!)
Problem #2: Your input was a 2-D image, so you need to convert your clusters into 2-D images as well. To do that you might want to use something like this:
cluster1 = (idx == 1);
cluster1=reshape(cluster1,size(I));
imshow(cluster1);
Do this for all clusters and see what you get.
As for the centroids, I don't know what code you are using so I'm afraid I can't be of much help there.
Categories
Find more on Gaussian Mixture Models in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!