how to encircle the blue objects and separating them?

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

 Réponse acceptée

Image Analyst
Image Analyst le 16 Août 2015

0 votes

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.

Plus de réponses (1)

Image Analyst
Image Analyst le 16 Août 2015

0 votes

You should use color segmentation followed by bwboundaries(). See my File Exchange for color segmentation tutorials. http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 You should probably start with the HSV method. Post your adapted code when/if you run into problems.

5 commentaires

ahasan ratul
ahasan ratul le 16 Août 2015
Modifié(e) : ahasan ratul le 16 Août 2015
thanks for the replay. it does separate the blue colored object. but i want to select the mask automatically. can you tell me a way that how can i do it instead "DrawFreehandRegion" ?
I'm not sure I understand. You have light reddish background, rose colored objects, light purple objects, and dark purple objects. I don't see blue objects. What color objects did you threshold for? And you forgot to attach your code to make it easy for me to help you.
The delta E method asked you to freehand draw a sample in the image that you want to extract. If you don't want to do that, just identify thresholds in the HSV segmentation demo.
Have you tried the "Color Thresholder" app on the "Apps" tab?
i want to separate those two light purple and dark purple objects. cant i crop the mask for single time and i'll use it for the next times i use this process may be with different but same times of images? for your convenience i am attaching few figure i cropped while using your color segmentation for my image. this one is my desired result but i want to create mask by freehand cropping for single time and want to store it for further use. is there any way?
Where is the mask? Is it in the spatial domain of your original image? That won't work because the cells will be in different places in other images. Best is to just use fixed thresholds in the HSV color space. Here is your image looking down onto the HS plane:
If looks like you can get purple by thresholding at less than 0.1 or more than 0.7 and dark purple has an S of more than about 0.2 and light purple is between 0.05 and 0.2, or something around there.
hsvImage = rgb2hsv(rgbImage);
h = hsvImage(:,:,1);
s = hsvImage(:,:,2);
v = hsvImage(:,:,3);
h_mask = h < 0.1 | h > 0.7;
s_mask_darkPurple = s > 0.2; % or whatever works.
s_mask_lightPurple = s > 0.05 & s < 0.2; % or whatever works.
% Get overall mask
darkPurple = h_mask & s_mask_darkPurple;
lightPurple = h_mask & s_mask_lightPurple;
% Mask back in RGB color space
% Mask the image using bsxfun() function
maskedDarkPurple = bsxfun(@times, rgbImage, cast(darkPurple, class(rgbImage)));
maskedLightPurple = bsxfun(@times, rgbImage, cast(lightPurple, class(rgbImage)));
OK, try the attached code. It does a decent job of getting the purple.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by