3
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ I get 251 with ComponentMeasurements[ MorphologicalComponents[ChanVeseBinarize[img, Red]], "Count"] // Length $\endgroup$ – bobthechemist Jun 5 '16 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 '16 at 9:44
14
$\begingroup$

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

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.

| improve this answer | |
$\endgroup$
  • $\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 '16 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 '16 at 12:33

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