K-means clustering

I am doing color based segmentation using kmeans clustering.I am using inbuilt function of matlab(kmeans).My input image has object and background where I need to segment the object. For that I am using cluster value as 2 and repeating the clustering 3 times.The problem I am facing is that for some images, the output of k-means is very bad the first time, but when I try doing the segmentation for the 2nd time it gives me good results. Why is this happening?Is it because of the light variations in the image? Posting the original image, image with wrong segmentation and image with right segmentation

Risposte (3)

Image Analyst
Image Analyst il 22 Gen 2017

1 voto

See my attached demo for doing kmeans clustering on RGB images:

7 Commenti

Srinivas Reddy
Srinivas Reddy il 8 Feb 2018
Modificato: Image Analyst il 8 Feb 2018
It's showing an error!!!
Error using reshape
To RESHAPE the number of elements must not change.
Error in kmeans_color_segmentation (line 128)
class1 = reshape(indexes == 1, rows, columns);
Image Analyst
Image Analyst il 8 Feb 2018
It worked for me. Which demo image did you use?
Srinivas Reddy
Srinivas Reddy il 8 Feb 2018
hands1.jpg,hands2.jpg,car1.jpg,car2.jpg
Image Analyst
Image Analyst il 8 Feb 2018
It works fine with all 4 of those images. I get this:
What are the sizes of indexes, rows, and columns that you get? Are you sure you didn't modify my demo somehow?
Srinivas Reddy
Srinivas Reddy il 8 Feb 2018
Modificato: Srinivas Reddy il 8 Feb 2018
I haven't changed the code
size of indexes= 1x5 matrix
size of rows= 1x1 matrix
size of columns = 1x1 matrix
Srinivas Reddy
Srinivas Reddy il 8 Feb 2018
It's working...Thanks a lot
Image Analyst
Image Analyst il 8 Feb 2018
For hands1 you should have indexes = 76800 x 1, rows = 240, and columns = 320. You must have changed something, but I'm glad you restored it and got it working again.

Accedi per commentare.

Image Analyst
Image Analyst il 21 Gen 2017

0 voti

Well obviously there are not 2 clusters. There are 3 dominant colors: green, brown, and black. Use k=3 in your code and it should improve.
Better yet, if you know you are going after certain colors like green, do thresholding in HSV color space. Try the color thresholder on the Apps tab of the tool ribbon.

4 Commenti

Radhika Bhagwat
Radhika Bhagwat il 21 Gen 2017
Thank you for the reply. Yes there are 3 colors in the image. But then when I execute it again how is it that I get proper segmentation?
Image Analyst
Image Analyst il 21 Gen 2017
Why would you not? Let's see your code. read this
Radhika Bhagwat
Radhika Bhagwat il 22 Gen 2017
Here is the code:
I= rgb image;The first image posted earlier.
cform = makecform('srgb2lab');
lab_he = applycform(I,cform);
ab = double(lab_he(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 2;
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ... 'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols);
figure();imshow(pixel_labels,[]), title('image labeled by cluster index');
After executing this code, the output I get for the first time is the second image that I have posted earlier. But if I try executing this entire code again I get a perfect segmented image (the 3rd image posted earlier). My question is why is this happening?(ie some times it gives bad segmentation while some times a good one). I understand that k-means selects the seeds randomly(So I could get 1 for object and 0 for background or vice-versa and this could change on every run).But I think ,the segmented part should remain same for every run(ie object and background). No matter if object/background is 1 or 0.But in the second image the algorithm has given the leaf and the shadow as object.While on second run,I get perfect result.
Image Analyst
Image Analyst il 22 Gen 2017
Did you run my kmeans demo I made up for you? It's in my second answer on this page. It plots the 3-D color gamut. Here is what a scatterplot of your a,b data looks like looking down the L axis:
and here is what it looks like from the side:
Do you see 2 well defined, well separated clusters there? No, you do not. The colors go continuously from one color to the next. There are going to be some colors that are "in between" colors and some maybe classified as one thing and some as the other thing, perhaps in disagreement with what you thought they should be.
That is why after doing color classification, by whatever method, often/usually you need additional steps to clean things up.
For what it's worth, I'm attaching another statistical method demo given to me by the Mathworks. It uses principal components analysis.
Additionally there are thresolding-based methods of color segmentation in my File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862

Accedi per commentare.

steny ynets
steny ynets il 28 Ago 2017

0 voti

what is the code to differentiate diseased and healthy part of the leaf

Richiesto:

il 21 Gen 2017

Commentato:

il 8 Feb 2018

Community Treasure Hunt

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

Start Hunting!

Translated by