MATLAB: How to encircle the blue objects and separating them

bio-medical image procesingcolor segmentationimage analysisImage Processing Toolboximage segmentation

i am trying to separate the blue/violet object in the "testimage" and to count them. but the way i tried encircling the objects does not encircle the blue objects (may be its not exactly circular, that's why) see ("result"). if some one could help me to separate the connected object if possible like the desired(e) image i've attached.
testimage
if true
A=imread('testimage.jpg);
E=rgb2gray(A);
level = graythresh(E)
B= im2bw(E,level);
H=~B;
G= imfill(H,'holes');
C=double(B);
for i=1:size(C,1)-2
for j=1:size(C,2)-2
%Sobel mask for x-direction:
Gx=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-(2*C(i,j+1)+C(i,j)+C(i,j+2)));
%Sobel mask for y-direction:
Gy=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-(2*C(i+1,j)+C(i,j)+C(i+2,j)));
%The gradient of the image
%B(i,j)=abs(Gx)+abs(Gy);
B(i,j)=sqrt(Gx.^2+Gy.^2);
end
end
D= imfill(B,'holes');
K=G | D
BB=imclearborder(K);
CC=im2bw(BB);
figure
imshow(CC)
bw3 = imopen(CC, ones(20,20));figure
imshow(bw3)
[centers,radii] = imfindcircles(bw3, [20 300], 'Sensitivity', .8);
viscircles(centers, radii, 'DrawBackgroundCircle', false)
end

Best Answer

Here is your frequency weighted color gamut.
You can see that there is very good separation between the dark purple and the other colors, and reasonably good separation between the light purple and the other. Between the rose colored cells and the background, there is not a clear separation line so getting those might require a little bit of cleanup before or after segmentation. For example you might try to flatten the background beforehand using adapthisteq(), then segment and use things like bwareafilt() or regionprops to get rid of non-round blobs or blobs of clearly the wrong size.