My goal is to find an insect from a fruit image using k means segmentaion. while doing iit using 4 clusters, each time the index values change. that is sometimes insect comes in 1st cluster, sometimes in second, and so on. how to fix it. my code is:

1 view (last 30 days)
debasmita bhoumik
debasmita bhoumik on 11 Mar 2016
Commented: Suleman Afzal on 12 Jul 2017
close all;
he = imread('deseaseleaf(3).jpg');
figure;
imshow(he), title('Original Leaf Image');
text(size(he,2),size(he,1)+15,...
'Image courtesy ____________ , Analysed by Debasmita Bhoumik, University of Calcutta', ...
'FontSize',7,'HorizontalAlignment','right');
cform = makecform('srgb2lab');
lab_he = applycform(he,cform);
ab = double(lab_he(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 4;
% repeat the clustering 4 times to avoid local minima
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean','Replicates',4);
pixel_labels = reshape(cluster_idx,nrows,ncols);
figure;
imshow(pixel_labels,[]), title('image labeled by cluster index');
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:nColors
color = he;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
figure;
imshow(segmented_images{1}), title('objects in cluster 1');
figure;
imshow(segmented_images{2}), title('objects in cluster 2');
figure;
imshow(segmented_images{3}), title('objects in cluster 3');
figure;
imshow(segmented_images{4}), title('objects in cluster 4');
mean_cluster_value = mean(cluster_center,2);
[tmp, idx] = sort(mean_cluster_value);
blue_cluster_num = idx(2);
L = lab_he(:,:,1);
blue_idx = find(pixel_labels == blue_cluster_num);
L_blue = L(blue_idx);
is_light_blue = im2bw(L_blue,graythresh(L_blue));
nuclei_labels = repmat(uint8(0),[nrows ncols]);
nuclei_labels(blue_idx(is_light_blue==false)) = 1;
nuclei_labels = repmat(nuclei_labels,[1 1 3]);
blue_nuclei = he;
blue_nuclei(nuclei_labels ~= 1) = 0;
figure
imshow(blue_nuclei), title('1st nuclei');
  1 Comment
Suleman Afzal
Suleman Afzal on 12 Jul 2017
u should have to show your cluster images to user and allow them to select specific one in which only insect is shown

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 11 Mar 2016
kmeans has no idea of what the data means so there is no way it is going to know that one variety of data should go into the first cluster specifically.
kmeans() also uses random initialization as part of its strategies, and you have specifically asked it to use 4 different random starting points. Because of that, even if in each of the 4 times the data broke up into exactly the same subsets, the "first" cluster could have been seeded around any of the locations, so you cannot assume that the insect will be in the first cluster even on two different runs of the same image.
You will need to use some other information to decide what the data in the cluster means. You might find regionprops() useful for examining features of each of the areas. regionprops can accept labeled images directly, by the way.

Image Analyst
Image Analyst on 11 Mar 2016
kmeans will be no good for determining if a picture has fruit flies in it or not. Do you know why? Think about it. Think about what kmeans does and how it operates and then you should realize.
  4 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!