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$
  • $\begingroup$ You mean, how do you draw lines between the circles? $\endgroup$ – Feyre Aug 1 '16 at 21:10
  • $\begingroup$ Exactly what I need. $\endgroup$ – LCarvalho Aug 1 '16 at 21:25
  • 1
    $\begingroup$ Have you looked at ComponentMeasurements[]? $\endgroup$ – J. M.'s discontentment Aug 1 '16 at 21:45
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

| improve this answer | |
$\endgroup$
  • $\begingroup$ Yes. This is exactly what I need. $\endgroup$ – LCarvalho Aug 2 '16 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$ – Alexey Popkov Aug 2 '16 at 4:01
  • $\begingroup$ @AlexeyPopkov Thanks. I thought that might be the better option. $\endgroup$ – Young Aug 2 '16 at 4:17
8
$\begingroup$

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

lines on the image

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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