How to count all flower in this image by using Mathematica?

enter image description here

It is possible to detect red objects and then remove background?

I guess this blog may be useful. and this question, "Count Elements in Image"

Thank you in advance to any one who may be able to give me some ideas.

  • 1
    $\begingroup$ I get 251 with ComponentMeasurements[ MorphologicalComponents[ChanVeseBinarize[img, Red]], "Count"] // Length $\endgroup$ Jun 5, 2016 at 20:36
  • $\begingroup$ @bobthechemist there are more flowers than 251 :) ComponentMeasurements[ MorphologicalComponents[ ChanVeseBinarize[img, Red, MaxIterations -> 1]], "Count"] // Length. I get 317. $\endgroup$
    – vito
    Jun 6, 2016 at 9:44

1 Answer 1


Here is what I get.

img = Import["http://i.stack.imgur.com/o3S3Q.jpg"];

red = Fold[ImageSubtract, #[[1]], Rest[#]] &@ColorSeparate[img]

enter image description here

b = Binarize[red]

enter image description here

pos = ComponentMeasurements[b, {"Centroid", "EquivalentDiskRadius"}];


Fine Tuning

Since the flowers in the top left corner are comparatively dull in colour, they are not properly identified. In such case a segment wise fine tuning may be helpful.

Consider a $300 \times 300$ block at top left corner. The previous method give 13 flower in this region.

im2 = ImageTake[img, {0, 300}, {0, 300}];

red = Fold[ImageSubtract, #[[1]], Rest[#]] &@ColorSeparate[im2]
b = Binarize[%];
pos = ComponentMeasurements[b, {"Centroid", "EquivalentDiskRadius"}];


Now we do a manual fine tuning by eliminating other colors.

im3 = ColorReplace[im2, Green, 0.3];
MorphologicalComponents[im3] // Colorize;
ColorReplace[%, DominantColors[%]] // Binarize;
fls = ComponentMeasurements[%, {"Centroid", "EquivalentDiskRadius"}];


Show[im2, Graphics[{Red, Circle[#, 10] & /@ fls[[All, 2, 1]],
                    Black, Circle[#, 10] & /@ pos[[All, 2, 1]]}]]

enter image description here

So our count increases by (92-13) = 79.

You might want to use this method for the whole image. In that case it will consider the lady as a lot of flowers and that would be 'mathematically' incorrect.

  • $\begingroup$ +1. but there are more flowers, Show[img, Graphics[{Black, Thick, Circle[#[[1]], #[[2]]] & /@ ComponentMeasurements[ ImageMultiply[img, b], {"Centroid", "EquivalentDiskRadius"}][[ All, 2]]}]] see here $\endgroup$
    – vito
    Jun 6, 2016 at 10:10
  • $\begingroup$ I agree. The pale red flowers are really tricky to pick. You may have to do some manual scanning in some parts. $\endgroup$
    – Sumit
    Jun 6, 2016 at 12:33

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