How to get xy coordinates of every pixel in a segmented line

Hello everybody!
I am new to programming in macro language and I am trying to get the x,y coordinates for every pixel of a segmented line I drew by hand. I would need to save them in a .txt file, but I can only get the coordinates of the points used to draw the selection.

How can I do it? :slight_smile:
Best,
Lucrezia

Hi @Lucrezia,

The function you look for is getProfile.

Nico

Hi Nico!

I already tried - maybe in the wrong way - but it gives me only the x coordinates and I miss the y coordinates.

setTool(“polyline”);
waitForUser(“Line selection”,“Specify a segmented line. Press ok when ready.”);
profile = getProfile();
for (i=0; i<profile.length; i++)
print(profile[i]);
setResult(“Value”, i, profile[i]);

Hi,

you can use getSelectionCoordinates to get the start and end x & y points of a line selection, or - for more complex shapes like rectangle or polyline - the coordinates of every corner.

getSelectionCoordinates(xpoints, ypoints);

Array.print(xpoints);
Array.print(ypoints);

Then you could simply interpolate to get all points along the line.
I don’t think a Line Roi really has intermediate points stored. But a hacky option would be to create a mask from the Roi, then turn it into a selection again (which is not a Line Roi anymore!)

run("Create Mask");
run("Create Selection");

then run the snippet above

1 Like

Here is a macro that saves the x,y coordinates of a segmented line using a one pixel interval.

  if (selectionType!=6)
     exit("Segmented line required");
  run("Interpolate", "interval=1");
  getSelectionCoordinates(xpoints, ypoints);
  Table.create("Points");
  Table.setColumn("X", xpoints);
  Table.setColumn("Y", ypoints);
  Table.save("");

Hi @Lucrezia,

In your program

you forgot the {} so only the

is computed.

With this macro (from https://imagej.nih.gov/ij/macros/GetProfileExample.txt)

run("Clear Results");
  profile = getProfile();
  for (i=0; i<profile.length; i++)
      setResult("Value", i, profile[i]);
  updateResults;

  // Plot profile
  Plot.create("Profile", "X", "Value", profile);

you get the resultfile where Value is your y and the row number is your x along your segmented line.

Nico