MATLAB Answers

How can I improve segmentation of these images?

34 views (last 30 days)
BC
BC on 25 Feb 2021
Commented: BC on 27 Feb 2021
I'm trying to segment ants in several thousand images (I've attached two images here). I'm then applying a colour detection function, with the ultimate aim of detecting paint marks on the abdomen of ants.
I've had some success with the below segmentation, but it also sometimes picks up the brood (the pale oval-shaped things in the image), as well as some of the border. I can't just use something like imclearborder, because often there are ants connected to these border pixels, right on the edge of the image.
I have also tried
  • K means clustering - but often the colour blobs are treated seperately to the ant body
  • imfindcircles (to try and isolate just the abdomen of ants) - not much success so far
I'm just wondering if anyone has any input or other methods to improve the segmentation.
ant = imread("ant.jpg") % read original image, call it "ant"
burnedAnt = createAntSegmentation(ant) % apply local function to ant image
%% display images side by side, check segmentation not too strict
fig = figure();
ax(1) = axes('Units','normalized','Position', [ .1 .1 .4 .8]);
ax(2) = axes('Units','normalized','Position', [ .5 .1 .4 .8]);
imshow(ant,'Parent', ax(1)) % display original image
imshow(burnedAnt, 'Parent', ax(2)) % display new burned image
linkaxes(ax)
%% create function to segment ants, and burn the mask onto the original
% image
function burnedAnt = createAntSegmentation(ant)
greyant = rgb2gray(ant); % convert image to grayscale
% threshold image. Higher = more lenient filering
adaptedAnt = adaptthresh(greyant, 0.38,"ForegroundPolarity","dark");
BW = imbinarize(greyant,adaptedAnt); % binarize ant image, using the above thresholding
BWopen = bwareaopen(~BW,800); % exclude pixels smaller than X
se = strel("disk",32); % create shape used for dilating image
% dilate image, expand the white pixels to ensure not cutting off relevant areas
BWdilate = imdilate(BWopen,se);
BWfilled = imfill(BWdilate,"holes"); % fill in any holes
%produce final image, mask burned onto original image, with black (k) fill.
burnedAnt = imoverlay(ant,~BWfilled,"k");
end

Accepted Answer

Image Analyst
Image Analyst on 27 Feb 2021
Edited: Image Analyst on 27 Feb 2021
Did you try the Color Thresholder on the Apps tab of the tool ribbon? Set it up for hsv color space and tell it to find green blobs and then export the code. Do you need the whole ant, or just need to know how many green blobs there are? It could get tricky if some of the ants touch each other, connecting the blobs.
  5 Comments
BC
BC on 27 Feb 2021
Yeah there's lots of little issues that'll crop up for sure! It'll never be 100% accurate but I'm trying to get it as good as I can. Thanks again for the help :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!