4
$\begingroup$

I'm using a github repo about turning point data into an array to then create a mirror. The author used a point picker in mathematica, but I want to automate the process. How can I import my image and get vertex values from it?

this is the image

This is the image

$\endgroup$
1
  • $\begingroup$ It would be valuable for you to clarify what you mean by “vertex coordinates”. From your responses to the existing answers, it appears that you want the boundaries of this image, i.e., a list of coordinates that describe the lines that are the boundaries of this image. $\endgroup$ Commented Nov 13, 2021 at 16:45

2 Answers 2

4
$\begingroup$

Are you looking for a smaller number of coordinates on the boundary of the image? If so, you could convert to a boundary mesh region and extract the coordinates from that.

After importing the image:

mesh = ImageMesh[ColorNegate[img]];
points = MeshCoordinates[mesh];

This gives a list of points on the boundaries of the parts of the image. E.g., to plot those points:

 ListPlot[points, AspectRatio -> Automatic]

image mesh points

$\endgroup$
6
  • $\begingroup$ this... this is exactly what I want, but I also need a list of the vertex coordinates. How do I do that? $\endgroup$ Commented Nov 13, 2021 at 5:08
  • $\begingroup$ The variable 'points' is the list of coordinates. Unless I'm not understanding what you mean by "vertex coordinates". $\endgroup$
    – tad
    Commented Nov 13, 2021 at 5:10
  • $\begingroup$ I mean; how can I print a list of the points for copy/pasting? $\endgroup$ Commented Nov 13, 2021 at 5:12
  • $\begingroup$ Just evaluate "points". It will output a list like {{72.08219949365268, 236.49319696191608}, {74.13043478260869, 245.52173913043475}, ....} which you can then copy. $\endgroup$
    – tad
    Commented Nov 13, 2021 at 5:15
  • $\begingroup$ Thank you so much. one more thing; this is how my graph comes out as imgur.com/a/u6QZAfE rather than what yours is. Did I import it wrong? code: Import["i.sstatic.net/dYO3S.png"] as img; $\endgroup$ Commented Nov 13, 2021 at 5:23
2
$\begingroup$

If you need coordinates for the pixels that are black, you can import the image. The information is actually stored in the alpha channel of the image, so we use only that:

img = Binarize@AlphaChannel@Import["https://i.sstatic.net/dYO3S.png"]

Then you can use Position on the image data matrix to get all 1 pixel. I did some transposing and reversing of the matrix so that you get {x,y} coordinates that are in the expected coordinate system:

points = Position[Reverse /@ Transpose[ImageData[img, "Bit"]], 1];
ListPlot[points, AspectRatio -> 1]

enter image description here

$\endgroup$
6
  • $\begingroup$ Is there any way I can minimize the number of data points? I want to keep it to a basic shape, because I will be 3d printing this. I do also need the vertex coordinates. $\endgroup$ Commented Nov 13, 2021 at 4:38
  • $\begingroup$ You can use ImageResize to scale the image down to e.g. 64x64 $\endgroup$
    – halirutan
    Commented Nov 13, 2021 at 4:42
  • $\begingroup$ that works, but how do I get the vertex coordinates? I'm very new to mathematica. $\endgroup$ Commented Nov 13, 2021 at 4:47
  • $\begingroup$ Just use img = Binarize@AlphaChannel@ImageResize[Import["https://i.sstatic.net/dYO3S.png"],64] to load the image. $\endgroup$
    – halirutan
    Commented Nov 13, 2021 at 4:53
  • 1
    $\begingroup$ Another way to get the coordinates of all the black points in the image: ImageValuePositions[Binarize[ColorNegate@img], 1] $\endgroup$
    – tad
    Commented Nov 13, 2021 at 5:09

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.