# How to determine coordinates of a shape in an image?

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

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?

-
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? –  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],
Point@ComponentMeasurements[
MorphologicalComponents[DeleteSmallComponents[
ChanVeseBinarize[i, TargetColor -> Green], 10]],
"Centroid"][[All, 2]]}]


Edit

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],
Point@ComponentMeasurements[
MorphologicalComponents[
ChanVeseBinarize[i, TargetColor -> Green],
Method -> "Convex"],
"Centroid"][[All, 2]]}]


-
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.

img=Import["http://i.stack.imgur.com/uO8QJ.png"];
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.

-
GOOD GOOD GOOD!!!!! –  user10495 Nov 11 '13 at 8:04