![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178060/image.jpeg)
how to encircle the blue objects and separating them?
4 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/148120/image.jpeg)
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
0 个评论
采纳的回答
Image Analyst
2015-8-16
Here is your frequency weighted color gamut.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178060/image.jpeg)
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.
0 个评论
更多回答(1 个)
Image Analyst
2015-8-16
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 个评论
Image Analyst
2015-8-16
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/180462/image.jpeg)
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)));
Image Analyst
2015-8-17
OK, try the attached code. It does a decent job of getting the purple.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!