How can I create lines using the center of the circles as a reference?

How can I create lines using the center of the circles as a reference?

I created a code, but it generates multiple lines around the circle.

coord=PixelValuePositions[imagem,Red];
coord=Sort@coord;
Graphics[{Line[{{coord[[#,1]],coord[[#,2]]}&/@Range[First[Dimensions[coord]]],{coord[[#,1]],coord[[#,2]]}&/@Range[2,First[Dimensions[coord]]]}]}]


The image used is at the link below:

Image

• You mean, how do you draw lines between the circles? – Feyre Aug 1 '16 at 21:10
• Exactly what I need. – LCarvalho Aug 1 '16 at 21:25
• Have you looked at ComponentMeasurements[]? – J. M.'s discontentment Aug 1 '16 at 21:45

Using FindClusters to group the pixels

coord = FindClusters[ImageValuePositions[imagem, Red], 5];
coord = Table[Mean[coord[[i]]], {i, 1, 5}]
ListPlot[coord]


{{36., 204.}, {347., 184.}, {150., 149.}, {246., 76.}, {71., 39.}}

Show[
imagem,
Graphics@Line@Subsets[#, {2}] &@coord]


• Yes. This is exactly what I need. – LCarvalho Aug 2 '16 at 2:13
• Strictly speaking instead of PixelValuePositions one should use ImageValuePositions here because the latter "assumes the standard image coordinate system." – Alexey Popkov Aug 2 '16 at 4:01
• @AlexeyPopkov Thanks. I thought that might be the better option. – Young Aug 2 '16 at 4:17

As I mentioned, you can use ComponentMeasurements[], at least for this example:

img = Import["http://i.stack.imgur.com/3W859.png"];
pts = ComponentMeasurements[SelectComponents[ColorNegate[Binarize[img]],
Small], "Centroid"];

HighlightImage[img, GraphicsComplex[Range[5] /. pts, {Blue, Line[Subsets[Range[5], {2}]]}]]


• What version and OS are you on? – J. M.'s discontentment Aug 4 '16 at 21:34
• It shouldn't have problems, then. Try the code in a fresh session. – J. M.'s discontentment Aug 4 '16 at 21:46
• Try applying Normal[] to the GraphicsComplex[] object before passing it to HighlightImage[]. – J. M.'s discontentment Aug 5 '16 at 11:23
• Wait... can you give the precise version that you have? ComponentMeasurements[] was introduced in 10.3, you see. – J. M.'s discontentment Aug 5 '16 at 12:19
• After running the first two lines in a fresh session, what does the output of pts look like? – J. M.'s discontentment Aug 5 '16 at 12:30