clc
clear all
close all

i=imread('load the image');
se1=strel('disk',40);
i1=imopen(i,se1);
i2=i-i1;
i3=adapthisteq(i2,"NumTiles",[32 32],"ClipLimit",0.002);
i4=imsharpen(i3,'Radius',2,'Amount',1);

t=220;
mask=i4>t;
i5=i4.*uint8(mask);
i6=imbinarize(i5,"adaptive","ForegroundPolarity","bright","Sensitivity",1);
minarea=20;
i7=bwareaopen(i6,minarea);

se2=strel('disk',5);
i8=imdilate(i7,se2);
gc1=~i8;
D1=bwdist(gc1);
L1=watershed(imhmin(-D1,1));
w1=L1==0;
g1=i8 & ~w1;



[label,m]=bwlabel(g1);
stats = regionprops("table",g1,"Centroid", ...
    "MajorAxisLength","MinorAxisLength","Area","EquivDiameter","BoundingBox");

figure;
imshow(i);
hold on;

for idx = 1:size(stats, 1)
    bbox = stats.BoundingBox(idx,:);
    rectangle('Position', bbox, 'EdgeColor', 'r', 'LineWidth', 1);
end

