# Question About Counting Spots on a Leaf in Mathematica

**URL:** <https://community.wolfram.com/t/question-about-counting-spots-on-a-leaf-in-mathematica/25166>\
**Category:** Questions\
**Tags:** computer-science, data-science, image-processing, machine-learning\
**Created:** [February 21, 2025, 2:55pm UTC](https://community.wolfram.com/t/question-about-counting-spots-on-a-leaf-in-mathematica/25166 "2025-02-21T14:55:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![floravf80](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/floravf80/32/7144_2.png) [@floravf80](https://community.wolfram.com/u/floravf80)\
**Post date:** [February 21, 2025, 2:55pm UTC](https://community.wolfram.com/t/question-about-counting-spots-on-a-leaf-in-mathematica/25166/1 "2025-02-21T14:55:40Z")

</div>

Hi Giulio Alessandrini,

Thank you for your guidance on this code. I tested a few leaf samples, and one of them is encountering a significant error during loading. I would appreciate it if you could provide some guidance on these errors.

 ![Screen Shot 2025-02-19 at 9.42.35 PM.jpg](https://global.discourse-cdn.com/wolfram/original/3X/1/7/17d29e945d06baf4e38268545ad0ffc5e5821c37.jpeg)  
 ![Screen Shot 2025-02-21 at 6.21.07 PM.jpg](https://global.discourse-cdn.com/wolfram/original/3X/c/f/cf8ecb01e68baf1547a048c894a358a49f5802e8.jpeg)

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [February 22, 2025, 10:33pm UTC](https://community.wolfram.com/t/question-about-counting-spots-on-a-leaf-in-mathematica/25166/2 "2025-02-22T22:33:30Z")

</div>

Flora,

the function `Binarize[]` has a parameter! In this case the value “.1” is much too small - just play around using other values, or do not use it at all.

---

<div class="post-metadata">

**Author:** ![floravf80](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/floravf80/32/7144_2.png) [@floravf80](https://community.wolfram.com/u/floravf80)\
**Post date:** [March 4, 2025, 10:26am UTC](https://community.wolfram.com/t/question-about-counting-spots-on-a-leaf-in-mathematica/25166/3 "2025-03-04T10:26:07Z")

</div>

Dr.Henrik

Thank you for your response.

to prevernt showing points outside the image,what should i do?

 ![Screen Shot 2025-03-04 at 1.48.57 PM.jpg](https://global.discourse-cdn.com/wolfram/original/3X/6/c/6c505fc30ada096b17e05d4e1fd2453007811296.jpeg)

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [March 6, 2025, 9:28am UTC](https://community.wolfram.com/t/question-about-counting-spots-on-a-leaf-in-mathematica/25166/4 "2025-03-06T09:28:17Z")

</div>

> to prevernt showing points outside the image,what should i do?

A simple method could be to use a mask defining the leaf shape - and than working with the resulting image, i.e. like so:

```
img0 = Import["https://community.wolfram.com/c/message_boards/get_message_attachment?messageId=3396992&attachment=Screen+Shot+2025-02-19+at+6.40.56+PM.png"]
leafMask = FillingTransform@MorphologicalBinarize[img0, .3]
img1 = leafMask img0

```

As always: You have to experiment with WL-functions and their parameters - but its fun!

---

<div class="post-metadata">

**Author:** ![floravf80](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/floravf80/32/7144_2.png) [@floravf80](https://community.wolfram.com/u/floravf80)\
**Post date:** [March 13, 2025, 4:27pm UTC](https://community.wolfram.com/t/question-about-counting-spots-on-a-leaf-in-mathematica/25166/5 "2025-03-13T16:27:42Z")

</div>

Dear Dr. Henrik,

Thank you for your response. I tried the code you provided and increased the parameter value, but the error still persists and has even increased compared to before.

Could you please advise on how to proceed?

 ![Screen Shot 2025-03-13 at 7.52.51 PM.jpg](https://global.discourse-cdn.com/wolfram/original/3X/f/b/fbd174afdc8ffaa962d0f2ed4cab74c12d2c7f46.jpeg)

---

<div class="post-metadata">

**Author:** ![henrikschachner](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/henrikschachner/32/32119_2.png) [@henrikschachner](https://community.wolfram.com/u/henrikschachner)\
**Post date:** [March 15, 2025, 5:00pm UTC](https://community.wolfram.com/t/question-about-counting-spots-on-a-leaf-in-mathematica/25166/6 "2025-03-15T17:00:07Z")

</div>

Flora,

you are not posting the full code, but only an image of only part of the code. One problem seems to be that you are trying to detect the spots using `ColorsNear[Black]` - but having a black background.

Here comes another approach - basically as an one-liner:

```
img0 = Import["https://community.wolfram.com/c/message_boards/get_message_attachment?messageId=3396992&attachment=Screen+Shot+2025-02-19+at+6.40.56+PM.png"]
HighlightImage[img0, ComponentMeasurements[Binarize[ColorDetect[img0, ColorsNear[RGBColor[.2, .25, .25]]], .6], Centroid", 10 < #Count < 2000 &]]

```
