Re: geometric challenge :)

Posted by Rasband, Wayne (NIH/NIMH) [E] on
URL: http://imagej.273.s1.nabble.com/Macro-code-to-fit-a-spline-to-two-segments-tp5009069p5009356.html

On Aug 26, 2014, at 8:43 AM, Gabriel Landini wrote:

> Hi Jerome,
>
> On Tuesday 26 Aug 2014 13:48:36 Jerome Mutterer wrote:
>> You could create the ABC polyline, fit a spline to it and measure its
>> length:
> [...]
>
> Nice one, more brains think better than one :-)
> The straight line is 704.987 pixels which is correct, but the curved 705.450.
> Is that correct, less than  half pixel longer?

Hard to believe but the following macro, which calculates the line length by summing the lengths of the segments, reports the same lengths as the Measure command. There are 3 segments in the base line and 352 in the spline fitted version.

-wayne

  newImage("Untitled", "8-bit black", 900, 500, 1);
  makeLine(203,212,449,320,872,427);
  printLength();
  Overlay.addSelection("cyan");
  makeLine(203,212,449,320,872,427);
  run("Fit Spline");
  printLength();
  Overlay.addSelection("magenta");

  function printLength() {
     List.setMeasurements;
     len1 = List.getValue('Length');
     getSelectionCoordinates(x, y);
     len2 = 0;
     for (i=0; i<x.length-1; i++) {
        dx = x[i+1] - x[i];
        dy = y[i+1] - y[i];
        d = sqrt(dx*dx+dy*dy);
        len2 += d;
     }
     print(len1, len2, x.length);
  }

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