How can I separate my a green channel of the image using k-means clustering?
Show older comments
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?
1 Comment
Walter Roberson
on 16 Aug 2015
A couple of sample images would help.
Answers (2)
Walter Roberson
on 16 Aug 2015

So experiment with
mean_cluster_value = mean(cluster_center * [1 0;0 -1],2);
Image Analyst
on 16 Aug 2015
0 votes
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
John Conson
on 16 Aug 2015
John Conson
on 16 Aug 2015
Walter Roberson
on 16 Aug 2015
Do you need to do something with the segmented image beyond setting the non-leaf parts to black?
John Conson
on 17 Aug 2015
Image Analyst
on 17 Aug 2015
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.
John Conson
on 29 Aug 2015
Walter Roberson
on 29 Aug 2015
How much longer until the sample images arrive?
Image Analyst
on 29 Aug 2015
There are lots of color segmentation algorithms. For example, here's one: http://www.mathworks.com/matlabcentral/fileexchange/37197-dem--diffused-expectation-maximisation-for-image-segmentation
Categories
Find more on Agriculture 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!