5
$\begingroup$

I need some help on image processing in Mathematica (I have unsuccesfully tried with OpenCV).

I have an image:

enter image description here

and I need to count all objects in this image and mark them.

The perfect result would look like this:

enter image description here

Is this possible? How?

Thank you all for your help. Alternative question: Is there a solution how to get rid of all the connections between objects without changing the rest of the image: enter image description here

Thank you in advance! Lauris

$\endgroup$
  • 2
    $\begingroup$ OpenCV ? Are you sure this question is about Wolfram Mathematica ? $\endgroup$ – Sektor Nov 26 '14 at 20:12
  • $\begingroup$ I failed to solve this by using OpenCV - I do not know if this possible using Wolfram Mathematica! $\endgroup$ – Lauris Nov 26 '14 at 20:31
  • $\begingroup$ I have not done anything (produced code) yet using Wolfram Mathematica. It was said to me that our University have Wolfram Mathematica so I just wanted to know if this possible and then start to dig in code. $\endgroup$ – Lauris Nov 26 '14 at 20:50
  • $\begingroup$ Wow! Binarize[img, 0] for a nice surprise! It was there all along! $\endgroup$ – Aisamu Nov 26 '14 at 22:19
  • $\begingroup$ Yes, this definitely looks like the distance transform of a binary image. Why not provide the original binary image in the first place? $\endgroup$ – Rahul Nov 27 '14 at 8:54
5
$\begingroup$

You can play with the binarization value to tweak the results:

img = Import["http://i.stack.imgur.com/LWNaK.png"]; 
MorphologicalComponents[Binarize[img, 0.4]] // Colorize

enter image description here

You can then calculate the positions, means, sizes, etc of the components using ComponentMeasurements as in this answer.

As for your second question, about removing the links between objects, the straightforward approach here is Erosion. For instance

Erosion[img, 5]

nicely removes the bulk of the links. Again, you will want to play with the erosion parameter (in this case 5) for best effect.

enter image description here

$\endgroup$
  • $\begingroup$ Thank you for the answer! What would be your guess of percentage of objects which I will be able to count and mark by this approach? $\endgroup$ – Lauris Nov 26 '14 at 20:53
  • $\begingroup$ There are two kinds of errors that might happen: you might miss a component that does exist or you might find an extra one (that doesn't really exist). You can balance these two possible kinds of errors, but never eliminate them completely. $\endgroup$ – bill s Nov 26 '14 at 20:55
  • $\begingroup$ That is clear! I am planning to analyse medical data. Allowed error is plus/minus 3%. Currently I have error of 15% or more :( $\endgroup$ – Lauris Nov 27 '14 at 8:35
4
$\begingroup$

Here's a set of functions, drawn mostly from here and a portion of this screencast. It is not a perfect answer at this point; the major problem being the small tendrils that link features together. It is possible that some type of filter with a DiskMatrix mask will lead you to a better result, but for the moment, I suspect there's a lot to wade through here.

seimg = Import["http://i.stack.imgur.com/LWNaK.png"];
i = GaussianFilter[FillingTransform[seimg], 15];
b = Binarize[i];
d = DistanceTransform[b, Padding -> 0];
m = MaxDetect[ImageAdjust[d, 0.2]];
w = WatershedComponents[GradientFilter[b, 3], m, Method -> "Rainfall"];
s = SelectComponents[w, "Count", 20 < # < 10000 &];
ms = ComponentMeasurements[
   s, {"Centroid", "EquivalentDiskRadius", "Label"}];
Show[seimg, 
 Graphics[{Blue, Circle @@ # & /@ (ms[[All, 2, 1 ;; 2]]), 
   MapThread[Text, {ms[[All, 2, 3]], ms[[All, 2, 1]]}]}]]

enter image description here

$\endgroup$
  • $\begingroup$ Is there a solution how to get rid of all the connections between objects without changing the rest of the image? I suspect that by doing this and afterwards using DiskMatrix it could improve results! $\endgroup$ – Lauris Nov 27 '14 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.