Create binary image using label matrix for corresponding cluster

6 views (last 30 days)
I am doing a project to segment a cell image into a few region using k-means. I have found a useful example using adaptive k-means without the need to specify the cluster number and the link is as below.
However i face some problem to display the clustered image as the segmented image from original image. I was advice to use label matrix as a mask to create a binary image for corresponding label(clustered image) and then multiply the mask to original image in order to obtain the segmented image.
Any suggestion or advice how to do this? Thanks

Answers (2)

Walter Roberson
Walter Roberson on 30 Jan 2016
Presuming that the results of calling that function are in the variable LB
num_blob = max(LB(:));
for K = 1 : numblob
ax = subplot(1, num_blob, K);
mask = repmat(LB == K, 1, 1, 3);
masked_image = YourRGBImage .* mask;
image(ax, masked_image);
axis(ax, 'image');
title(ax, sprintf('blob %#d', K));
end

Image Analyst
Image Analyst on 30 Jan 2016
Edited: Image Analyst on 30 Jan 2016
Assuming you have a labeled image where every color that you care about has a label of an integer 1 or more, and everything that you don't care about has a value of 0, you can mask the image like this:
mask = labeledImage > 0; % Get stuff we care about.
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));

Community Treasure Hunt

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

Start Hunting!