# Function to get pixel x,y coordinates for a line ROI

#1

Sorry, I have looked around a lot for this I am unable to find the solution. I am stuck on this part:
Could you please help me write a function which will output x,y coordinates in raw pixels of a line drawn using line tool (including for oblique lines).

Many Thanks
Harry

#2

Good day Harry,

you may try the following ImageJ demo-macro (revised):

``````path = getDirectory("Where to store the values?");
newImage("TestImage", "8-bit ramp", 256, 256, 1);
makeLine(40, 30, 150, 210);
getSelectionCoordinates( x, y );
a = getProfile();
str = "";
for ( i=0; i < a.length; i++ ) str += "" + x[i] + "\t" + y[i] + "\t" + a[i] + "\n";
File.saveString( str, path + "myProfile.txt" );
``````

Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it.

HTH

Herbie

#3

Dear Herbie

I am trying to create a PlugInFrame, my following function display the array of pixels along the line, but in subpixel array. The image is 16 bit DCM, `double [] line1 = l.getPixels();` generated a subpixel level array using pixel spacing (0.117/0.117 in my image).

How can I get pixel x,y coordinates of 15th (say) item in array this subpixel array?

``````    public void findPoint() {
ImagePlus imp = IJ.getImage();
Roi roi = imp.getRoi();
ImageProcessor ip = imp.getProcessor();
if (roi==null) {
IJ.showMessage("Error", "Select a ROI first!");
}
if (!roi.isLine()) { // or -  if (type==Roi.LINE) {
IJ.log("draw a line roi first");
} else {
IJ.log("good, line selected");
rm.runCommand("Show All");
roi.setName("Line1Drawn");
Line l = (Line)roi;
double x1=l.x1d;
double y1=l.y1d;
double x2=l.x2d;
double y2=l.y2d;
double [] line1 = l.getPixels();
IJ.log("length of line : " + line1.length);
IJ.log("length of line getLength : " + l.getLength()); //Returns the length of this line in mm
IJ.log("length of line getRawLength : " + l.getRawLength()); //Returns the length of this line in pixels.
IJ.log("getWidth of line getWidth : " + l.getWidth());
IJ.log(" x1:" + x1 + " y1:" + y1 + " x2:" + x2 + " y2:" + y2);
for (int i=0; i<line1.length; i++) {
IJ.log("value : "+ i + " : " + line1[i]);
}
}
// Obtain array of pixel values for each profile
//double[] profile1 = roi.getPixels();
}
``````

Hope my question is clear.

Cheers
Harry

#4

Harry!

Could you please help me write a function which will output x,y coordinates in raw pixels of a line drawn using line tool (including for oblique lines).

You didnâ€™t mention that you are coding a plugin.

Just for the record:
Did you try my macro and does it what you expect? Please report back!

[â€¦] generated a subpixel level array using pixel spacing (0.117/0.117 in my image).
How can I get pixel x,y coordinates of 15th (say) item in array this subpixel array?

I think you need the Java equivalent of â€śgetSelectionCoordinates( x, y );â€ť

Good luck

Herbie

#5

Sorry about the confusion Herbie, I wanted as plugin, but yes your code worked okay for me.

I have somehow managed to work a little bit not sure if it is the best method though.

``````public void findPoint() {
ImagePlus imp = IJ.getImage();
Roi roi = imp.getRoi();
ImageProcessor ip = imp.getProcessor();
if (roi==null) {
IJ.showMessage("Error", "Select a ROI first!");
}
if (!roi.isLine()) { // or -  if (type==Roi.LINE) {
IJ.log("draw a line roi first");
} else {
IJ.log("good, line selected");
rm.runCommand("Show All");
roi.setName("Line1Drawn");
FloatPolygon p = roi.getInterpolatedPolygon();
Point[] points = new Point[p.npoints];
for (int i=0; i<p.npoints; i++) {
points[i] = new Point((int)Math.round(p.xpoints[i]),(int)Math.round(p.ypoints[i]));
IJ.log("x : " + points[i].x + "y : " + points[i].y + " pixel =" + ip.getPixel(points[i].x, points[i].y));
}
}
// Obtain array of pixel values for each profile
//double[] profile1 = roi.getPixels();
}
``````

Many thanks for your help.

Harry