# How to split a spline at regular intervals

Dear All,
I have written a macro that given a strait line, divides it into equal length segments and also generates a series of perpendicular lines crossing the start/end point of each segment. The macro is (sorry if it looks very basic but I am just learning this stuff…):

``````getLine(x0,y0,x11,y11,width);
dx = x11 -x0;
dy = y11 -y0;
xstep = dx/11;
ystep = dy/11;

// Interpolate segment points
xs=x0 - 1*xstep
x1 = x0 + 1*xstep;
x2 = x0 + 2*xstep;
x3 = x0 + 3*xstep;
x4 = x0 + 4*xstep;
x5 = x0 + 5*xstep;
x6 = x0 + 6*xstep;
x7 = x0 + 7*xstep;
x8 = x0 + 8*xstep;
x9 = x0 + 9*xstep;
x10 = x0 + 10*xstep;
x11 = x0 + 11*xstep;
x12 = x0 + 12*xstep;
x13 = x0 + 13*xstep
ys=y0 - 1*ystep
y1 = y0 + 1*ystep;
y2 = y0 + 2*ystep;
y3 = y0 + 3*ystep;
y4 = y0 + 4*ystep;
y5 = y0 + 5*ystep;
y6 = y0 + 6*ystep;
y7 = y0 + 7*ystep;
y8 = y0 + 8*ystep;
y9 = y0 + 9*ystep;
y10 = y0 + 10*ystep;
y11 = y0 + 11*ystep;
y12 = y0 + 12*ystep;
y13 = y0 + 13*ystep;

// Create sections at regular intervals along the highlited Total Body Length of the seal

makeLine(xs, ys, x1, y1);
Roi.setStrokeColor(255,126,0);
run("Rotate...", "angle=90");
roiManager("Add");

makeLine(xs, ys, x3, y3);
Roi.setStrokeColor(255,126,0);
run("Rotate...", "angle=90");
roiManager("Add");

makeLine(x0, y0, x4, y4);
Roi.setStrokeColor(255,126,0);
run("Rotate...", "angle=90");
roiManager("Add");

makeLine(x1, y1, x5, y5);
Roi.setStrokeColor(255,126,0);
run("Rotate...", "angle=90");
roiManager("Add");

makeLine(x2, y2, x6, y6);
Roi.setStrokeColor(255,126,0);
run("Rotate...", "angle=90");
roiManager("Add");

makeLine(x3, y3, x7, y7);
Roi.setStrokeColor(255,126,0);
run("Rotate...", "angle=90");
roiManager("Add");

makeLine(x4, y4, x8, y8);
Roi.setStrokeColor(255,126,0);
run("Rotate...", "angle=90");
roiManager("Add");

makeLine(x5, y5, x9, y9);
Roi.setStrokeColor(255,126,0);
run("Rotate...", "angle=90");
roiManager("Add");

makeLine(x6, y6, x10, y10);
Roi.setStrokeColor(255,126,0);
run("Rotate...", "angle=90");
roiManager("Add");

makeLine(x7, y7, x11, y11);
Roi.setStrokeColor(255,126,0);
run("Rotate...", "angle=90");
roiManager("Add");

makeLine(x8, y8, x12, y12);
Roi.setStrokeColor(255,126,0);
run("Rotate...", "angle=90");
roiManager("Add");

makeLine(x10, y10, x12, y12);
Roi.setStrokeColor(255,126,0);
run("Rotate...", "angle=90");
roiManager("Add");

makeLine(x0, y0, x11, y11);
Roi.setStrokeColor(255,126,0);
roiManager("Add");
run("Measure");
``````

There are cases though where I need to obtain the same result using a segmented line/polyline. To better “mimic” the shape of the object I want to measure, I fit a spline to my polyline. And that’s easily done. How can I split the spline into equal segment and run a perpendicular line at the begin/end of each of them? Any help is greatly appreciated. That’s what I pieced together so far:

``````makeLine(203,212,449,320,872,427);
run("Properties... ", "stroke=red");
roiManager('add');
run("Fit Spline");
run("Properties... ", "stroke=green");
roiManager('add');
run("Measure");
``````

Nino

Nino,
I have a tool macro that I think you could modify to achieve the task you describe. Here’s an example image of what you can do with this tool.

You can get the code here:

Best regards,

Jerome.

2 Likes

For the record: this same question was also asked on stackoverflow and on the mailing list.

Please disclose when cross-posting, so others having similar issues can find the solution as well. And now let’s try to keep the discussion in one place.

1 Like

Thank you Jerome for the feedback. I will try your code an I will let you know whether I need any further help. Very much appreciated.
N

Hi Jerome,
I have had the time to look at your code. It seems exactly what I need but when trying it on a random image i get the following error. No idea at all what it means. Any advice?
Cheers,
N

``````Memory	*	35MB of 12212MB (<1%)
nImages()	*	1
getTitle()	*	"TW.png"
xp	*	array[1903]
yp	*	array[1903]
x	*	1916
y	*	882
z	*	0
flags	*	0

---		---
Error:		Undefined variable in line 27:
makeLine ( x , y , xp [ <cp> ] , yp [ cp ] ) ;
``````

The code I shared is a tool macro. You need to install it, and it will be run when you click inside an image. It’s not meant to be run manually. In addition, this tool needs a selection (spline) in the ROI Manager. See https://imagej.nih.gov/ij/developer/macro/macros.html#tools for details about tool macros.

I would suggest you rather read this related post : Help needed creating a segmented line with marks every 20µm

Cool, Thank you very much I will get back to you after I try the code again.
N

Hi Jerome,
I have difficulties with modifying your macro. would it be possible to have a privare email exchange?
regards,
N