# Count flowers in an image [duplicate]

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

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.

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

Here is what I get.

img = Import["https://i.sstatic.net/o3S3Q.jpg"];

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


b = Binarize[red]


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


633

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"}];
Length[pos]


13

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"}];
Length[fls]


92

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


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.

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