Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grayscale image processing using k-mean

I am trying to convert the rgb image into a grayscale and then cluster it using kmean function of matlab .

here is my code

he = imread('tumor2.jpg');

%convert into a grayscale image
ab=rgb2gray(he);
nrows = size(ab,1);
ncols = size(ab,2);

%convert the image into a column vector
ab = reshape(ab,nrows*ncols,1);

%nColors=no of clusters
nColors = 3;
%cluster_idx is a n x 1 vector where cluster_idx(i) is the index of cluster assigned to ith pixel 
[cluster_idx, cluster_center ,cluster_sum] = kmeans(ab,nColors,'distance','sqEuclidean','Replicates',1,'EmptyAction','drop' );

figure;
%converting vector into a matrix of dimensions equal to that of original
%image dimensions (nrows x ncols)
pixel_labels = reshape(cluster_idx,nrows,ncols);
pixel_labels
imshow(pixel_labels,[]), title('image labeled by cluster index');

problems

1) output image is always a plain white image. i tried the solution given in the link below but output of the image is a plain gray image in this case.

find the solution tried here

2) when i execute my code second time ,execution does not proceed beyond k-mean function (it is likes an infinite loop there). hence no output in this case.

like image 205
dhama Avatar asked Jul 23 '26 17:07

dhama


1 Answers

Actually, it looks like when you are colour segmenting kmeans is known to fall in local minima. This means that often, it wont find the amount of clusters you want as the minimization is not the best (that's why lots of people use other type of segmentation, such as level sets or simple region growing).

An option is to increase the amount of Replicates (amount of times kmeans will try to find the answer). At the moment you are setting it to 1, but you could try 3 or 4, and it may reach the solution that way.

In this question the accepted answer recommends to use a kmeans version of the algorithm specifically created for image segmentation. I havent tried myself but I think its worth a shot.

Link to FEX

like image 186
Ander Biguri Avatar answered Jul 26 '26 09:07

Ander Biguri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!