Hi Giovanni,

a line between points (x, y) and (x + n, y) has (n + 1) points, because both end points are included.
Pixel coordinates in ImageJ range from 0 to (width - 1), so you should use
   roi = Line(0, int(h/2), w-1, int(h/2))

For lines of a width different from the image width, this works consistently. The inconsistent behavior only occurs for horizontal lines with a width equal to the image width or vertical lines with a length equal to the image height. (Irrespective of whether the starting point is 0 or not).

It is caused by ImageProcessor.getLine:

https://github.com/imagej/ImageJ/blob/master/ij/process/ImageProcessor.java#L1120

In this code, the case of horizontal lines with a length equal to the image width or vertical lines with a length equal to the image height is an exception; the end point is not included (for a reason that I do not understand).


BTW, for the non-python users, I paste a javascript version of a similar macro below.

Michael
________________________________________________________________

imp = IJ.openImage("http://imagej.net/images/Cell_Colony.jpg";);
imp.show();
h = imp.getHeight();
w = imp.getWidth();

for (i=1; i<=5; i++) {
    roi = new Line(0.0,Math.floor(h/2),406.0,Math.floor(h/2));
    roi.setLineWidth(i);
    imp.setRoi(roi);
    y = roi.getPixels();
print("Line thickness: "+i+" - Number of points on the X axis: "+y.length);
}
________________________________________________________________
On 11.10.24 17:01, Cardone, Giovanni wrote:
Hi,

I discovered that when tracing an horizontal line on an image and retrieving 
the profile intensity, the number of points is artificially increased by one 
when setting a line width larger than one.
Here is a python script that reproduces the issue

from ij import IJ

from ij.gui import Line

imp = IJ.openImage("http://imagej.net/images/Cell_Colony.jpg";)

imp.show()

h = imp.getHeight()

w = imp.getWidth()

for line_thickness in (1, 5):

     roi = Line(0,int(h/2),w,int(h/2))

     roi.setLineWidth(line_thickness)

     imp.setRoi(roi)

     y = roi.getPixels()

     print("Line thickness: %d - Number of points on the X axis: 
%d"%(line_thickness,len(y)))

This inconsistency only happens when the line is perfectly horizontal. I looked 
at the code and it seems that the extra point at the end of the profile is 
generated by the function PolygonRoi.fitSplineForStraightening() called by 
Straightener.straightenLine().
In my tests I could see this happening only when the Line Roi is horizontal, and it affects also a 
profile plot (e.g. when calling IJ.run(imp, "Plot Profile", "")).

In my script I was able to bypass the issue by using instead a rectangular Roi 
with height equal to the line thickness, but I thought it was important to be 
aware of this anomaly.

Best,
Giovanni

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

Reply via email to