Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a pre-processed image, from which I have to get coordinates of some components.

sample image

There are 7 green components (pigs actually) that I have already isolated from an angry bird screen shot. So what I want is to locate those six components on the image.

I tried

ImageValuePositions[u, RGBColor[1, 0, 0], .1]
{ Mean[#1], Mean[#2]}& @@@ Transpose /@ FindClusters[%];
LocatorPane[ %, sample image]

But it doesn't work at all. Could anyone help me out?


How should I figure this out?

share|improve this question
RGBColor[1, 0, 0] is red, not green... – Rahul Nov 11 '13 at 1:53
I am so stupid!!!!!! – user10495 Nov 11 '13 at 8:01
"There are 7 green components (pigs actually)" .... "what I want is to locate those six components" ... So ... six or seven? Do you want partial matches or not? – Dr. belisarius Nov 11 '13 at 14:24
sorry, that's a typo, I need 7. Thanks – user10495 Nov 11 '13 at 22:58
Show[i, Graphics@{PointSize[Large], 
                                        ChanVeseBinarize[i, TargetColor -> Green], 10]], 
                           "Centroid"][[All, 2]]}]

Mathematica graphics


You can specify a convex method for the morph. components and then you won't need deleting the small components:

Show[i, Graphics@{PointSize[Medium], 
                              ChanVeseBinarize[i, TargetColor -> Green], 
                           Method -> "Convex"],
                        "Centroid"][[All, 2]]}]

Mathematica graphics

share|improve this answer
Thank you, that's really fancy!! – user10495 Nov 11 '13 at 8:01
It seems like when I apply this method, it can only detect two black dots, maybe because they are not separated? HELP ME !!! Thank you so much – user10495 Nov 11 '13 at 8:19
I mean the new picture I posted above. – user10495 Nov 11 '13 at 8:36

Here's an approach quite similar to what you tried. First, allG is all the green points which are then clustered into the 7 groups called clus. The mean of each group provides an approximation to the central point of the clusters.

allG = ImageValuePositions[img, Green];
clus = FindClusters[allG, 7];
Mean[clus[[#]]] & /@ Range[7]
{{182.542, 179.264}, {288.831, 20.1529}, {299.942, 18.1395}, {315.611, 15.8951}, 
 {255.067, 13.1082}, {230.66, 5.70168}, {255.8, 4.24286}}

Consolidating this into one command yields:

Mean[FindClusters[ImageValuePositions[img, Green], 7][[#]]] & /@ Range[7]

which gives the same answer as above.

share|improve this answer
GOOD GOOD GOOD!!!!! – user10495 Nov 11 '13 at 8:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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