3
$\begingroup$

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

enter image description here

$\endgroup$
3
  • $\begingroup$ You mean, how do you draw lines between the circles? $\endgroup$
    – Feyre
    Commented Aug 1, 2016 at 21:10
  • $\begingroup$ Exactly what I need. $\endgroup$
    – LCarvalho
    Commented Aug 1, 2016 at 21:25
  • 1
    $\begingroup$ Have you looked at ComponentMeasurements[]? $\endgroup$ Commented Aug 1, 2016 at 21:45

2 Answers 2

7
$\begingroup$

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

enter image description here

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

enter image description here

$\endgroup$
3
  • $\begingroup$ Yes. This is exactly what I need. $\endgroup$
    – LCarvalho
    Commented Aug 2, 2016 at 2:13
  • 2
    $\begingroup$ Strictly speaking instead of PixelValuePositions one should use ImageValuePositions here because the latter "assumes the standard image coordinate system." $\endgroup$ Commented Aug 2, 2016 at 4:01
  • $\begingroup$ @AlexeyPopkov Thanks. I thought that might be the better option. $\endgroup$
    – Young
    Commented Aug 2, 2016 at 4:17
8
$\begingroup$

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

img = Import["https://i.sstatic.net/3W859.png"];
pts = ComponentMeasurements[SelectComponents[ColorNegate[Binarize[img]],
                                             Small], "Centroid"];

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

lines on the image

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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