How can I separate my a green channel of the image using k-means clustering?

I have been working on a project that separates leaf color(green) from the remaining background and plots it. The only problem is that whenever I do it each cluster comes out random (one cluster would equal another and vice versa) here is my code
he = imread('6.jpg');
%imshow(he), title('LEAF');
text(size(he,2),size(he,1)+15,...
'Image courtesy of *******, ', ...
'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 = 3;
% repeat the clustering 3 times to avoid local minima
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols);
%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
C1 = segmented_images{1};
C2 = segmented_images{2};
C3 = segmented_images{3};
%imshow(C1), title('objects in cluster 1');
%imshow(C2), title('objects in cluster 2');
%imshow(C3), title('objects in cluster 3');
%start example mean_cluster_value = mean(cluster_center,2);
[tmp, idx] = sort(mean_cluster_value);
blue_cluster_num = idx(1);
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;
%end example imshow(blue_nuclei), title('blue nuclei');
From start example to end example, it is supposed to only include the blue part of the cluster. How can I change it to only include the green part?

Answers (2)

So experiment with
mean_cluster_value = mean(cluster_center * [1 0;0 -1],2);
Well, that's not going to work. Not sure why you thought it would. Have you actually looked at the projection onto the a-b plane? Using kmeans is not good - you'd be better off using rgb2ind() (which uses a more sensible color quantization routine) or using fixed thresholds. Even better would be to use fixed thresholds in HSV color space. And why did you think there would be three color clusters in your gamut? Depending on what's in the background, there could be 2 or 4 or 10 or 20.
It's late tonight. Perhaps tomorrow I can explain further and show you the color gamut. In the mean time, try the color segmentation app if you have the last version or two of MATLAB.

8 Comments

For the most part, Kmeans did an OK job at segmenting the layers (and separating them accurately.) Color segmentation would always get something else into the shot and I am not entirely sure how to utilize it (I am a beginner). Plus, with the amount of different shots I will be processing (150-500), the contrast and overall color of the leaf/ photo may change. Color gamut does sound useful
How would I exactly segment the image once it is indexed?
Do you need to do something with the segmented image beyond setting the non-leaf parts to black?
I have to set a histiogram for each layer of intensity (red, blue, green, etc.) after I set the non-leave part to black
Try this:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
[redCounts, redLocations] = imhist(redChannel(mask));
[greenCounts, greenLocations] = imhist(greenChannel(mask));
[blueCounts, blueLocations] = imhist(blueChannel(mask));
Also make sure you check out my lengthy discussion of gamuts and color segmentation in your duplicate question.
Thanks again for the suggestion. Do you know any other available segmentation options (such as median cut?)
How much longer until the sample images arrive?

Sign in to comment.

Categories

Find more on Agriculture in Help Center and File Exchange

Asked:

on 15 Aug 2015

Commented:

on 29 Aug 2015

Community Treasure Hunt

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

Start Hunting!