Question About Counting Spots on a Leaf in Mathematica

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.


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.

Dr.Henrik

Thank you for your response.

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

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!

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?

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 &]]