I have the following image: img1

enter image description here

I'm trying to count al these colored cells and draw a circle around them. I really don't now how I can use SelectComponents for this image. And EdgeDetect has the following result: img2

enter image description here

Maybe some tips or examples.


  • $\begingroup$ If you just want information about the components (centroid, caliper length), ComponentMeasurements should be more useful than SelectComponents $\endgroup$ Commented Dec 7, 2012 at 13:59
  • $\begingroup$ No, I want to know how you can use these functions to count the cell (examples) $\endgroup$
    – Glenn
    Commented Dec 7, 2012 at 14:04
  • 3
    $\begingroup$ Have you looked at MorphologicalComponents ? $\endgroup$ Commented Dec 7, 2012 at 14:11
  • $\begingroup$ I have an impression that this is not the original image. What is the very original image you start from? $\endgroup$ Commented Dec 7, 2012 at 17:13
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the FAQs! 3) When you see good Q&A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign` $\endgroup$ Commented Dec 9, 2012 at 9:03

1 Answer 1


I will follow algorithm described in the following blog, but will have to modify some parameters for your case. You can also drop FillingTransform from the blog code because you don't have holes in your regions.

rbc = ImageCrop[   Import["https://i.sstatic.net/wOJps.png"], {300, 340}];

b = DeleteSmallComponents[ColorNegate[Binarize[rbc, .81]], 50];
distT = DistanceTransform[b, Padding -> 0];
marker = MaxDetect[ImageAdjust[distT], 0.02];
w = WatershedComponents[GradientFilter[b, 3], marker, 
   Method -> "Rainfall"];

cells = SelectComponents[w, "Count", 500 < # < 3000 &];
measures = 
   cells, {"Centroid", "EquivalentDiskRadius", "Label"}];
 Graphics[{Blue, Circle @@ # & /@ (measures[[All, 2, 1 ;; 2]]), 
   MapThread[Text, {measures[[All, 2, 3]], measures[[All, 2, 1]]}]}]]

enter image description here

Then bare number of circled cells can be computed with

measures // Length


  • $\begingroup$ I have used the blog you referred to. Thank you for your help! $\endgroup$
    – Glenn
    Commented Dec 9, 2012 at 12:08
  • $\begingroup$ @Glenn please register your account. Then you will be able to use the 41 "reputation" points you already have to post comments. $\endgroup$
    – Mr.Wizard
    Commented Dec 9, 2012 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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