I have an image:

I tried to extract the outline of the bubbles, but I met with some problems. Firstly I tried the method used in Mathematica documentation for Analyze Segmented Cells In An Image, but it didn't work well, I got an irregular figure:

Then I tried the function EdgeDetect. Things got a little bit better, but it's still not what I want. Could you please help me with my problem?
Answer
image = Import["http://i.stack.imgur.com/UNXEV.jpg"];
n = TotalVariationFilter[image, 0.05]
b = LocalAdaptiveBinarize[n, 50]
s = DeleteSmallComponents[ColorNegate[DeleteSmallComponents[b, 20^2]], 20^2]
(d = DistanceTransform[s]) // ImageAdjust
m = MaxDetect[d, 10]
(w = WatershedComponents[ColorNegate[d], m]) // Colorize
c = ComponentMeasurements[w, {"Centroid", "EquivalentDiskRadius"}, #2 < 50 &];
Show[image, Graphics[{Thick, Red, Circle @@@ c[[All, 2]]}], ImageSize -> 1280]
(Click for bigger.)
Run the code to see the intermediate steps.
Thanks to nikie for the second half of this answer.
LocalAdaptiveBinarize is new in version 10, but all it does is "creates a binary image by replacing values above the mean of the range-$r$ neighborhood with $1$ and others with $0$." So you can replace it with something like, say,
b = Binarize[ImageSubtract[n, MeanFilter[n, 50]], 0]
which gives similar results, though not identical for some reason. Possibly there's a more elegant way to do it.

Comments
Post a Comment