ant = imread("IMG_4335.GREEN.JPG");
antLAB = rgb2lab(ant);
antL = antLAB(:,:,1);
antA = antLAB(:,:,2);
antB = antLAB(:,:,3);
imshow(ant)
roi = drawpolygon
BWant = createMask(roi);
aROI = antA(BWant);
bROI = antB(BWant);
meanROI = [mean(aROI) mean(bROI)];
aMean = meanROI(1);
bMean = meanROI(2);
[finalantimage centroids] = createantmask(ant, antA, aMean, bMean);
ant = imread("Y2.JPG");
[finalantimage centroids] = createantmask(ant, antA, aMean, bMean);
imshow(finalantimage)
hold on
plot(centroids(:,1),centroids(:,2),"b*");
hold off
fig = figure();
ax(1) = axes('Units','normalized','Position', [ .1 .1 .4 .8]);
ax(2) = axes('Units','normalized','Position', [ .5 .1 .4 .8]);
imshow(finalantimage, 'Parent', ax(1))
imshow(ant, 'Parent', ax(2))
linkaxes(ax)
function [finalantimage centroids] = createantmask(ant, antA, aMean, bMean)
ant = imgaussfilt(ant,2);
antLAB = rgb2lab(ant);
antL = antLAB(:,:,1);
antA = antLAB(:,:,2);
antB = antLAB(:,:,3);
distLab = sqrt((antA - aMean).^2 + (antB - bMean).^2);
mask = distLab < 10;
se = strel("disk",7);
dilatedant = imdilate(mask,se);
maskfilledholes = imfill(dilatedant,"holes");
maskcleaned = bwareaopen(maskfilledholes,10);
antmeasurements = regionprops(finalantimage, "all");
centroids = cat(1,antmeasurements.Centroid);
dlmwrite("antcoordinateS.txt",centroids);
imwrite(maskcleaned,"outputantimage.jpg");
end
Best Answer