Login  Register

Re: minimum distance between objects

Posted by Stephan Saalfeld on May 25, 2010; 5:12pm
URL: http://imagej.273.s1.nabble.com/minimum-distance-between-objects-tp3688186p3688187.html

Hi,

assuming that with point objects, you refer to a PointRoi, copy the
following BeanShell-snippet into either the Fiji Beanshell Interpreter
or save it with the file-ending .bsh and drag it to the Fiji toolbar.


import ij.*;
import ij.gui.*;

points = IJ.getImage().getRoi();
pointsX = points.getXCoordinates();
pointsY = points.getYCoordinates();
for ( i = 0; i < pointsX.length; ++i ) {
  dMin = Integer.MAX_VALUE;
  iMin = -1;
  for ( j = 0; j < pointsX.length; ++j ) {
    if ( i == j ) continue;
    dX = pointsX[ j ] - pointsX[ i ];
    dY = pointsY[ j ] - pointsY[ i ];
    d = dX * dX + dY * dY;
    if ( dMin > d ) {
      dMin = d;
      iMin = j;
    }
  }
  IJ.log( ( i + 1 ) + " " + ( iMin + 1 ) + " " + Math.sqrt( dMin ) );
}

It prints n lines with three space separated numbers into the log
window:

<index> <nearest point index> <minimal distance>

Best regards,
Stephan





On Tue, 2010-05-25 at 14:54 +0200, Johannes-Paul M. Koch wrote:

> Dear Listers,
>
> I have two images with a set of point objects (derived from a previous
> calculation, i.e. basically, centers of mass of various particles).
>
> I want now to calculate the distance of each point (object) in image A to
> the closest point (object) in image B (and only to this closest one...).
>
> I suppose, the code I am looking for should 1) calculate ALL distances
> from point 1 in image A to ALL points in image B, 2) identify the shortest
> and 3) return the value of that distance.
>
> I had a look at Ben's plugin
> (http://imagejdocu.tudor.lu/doku.php?id=plugin:analysis:minimum_separation_distance:start)
> However, this does sort of a different job. On top, I admit that I am not
> so familiar with java (thanks, Wayne, for the macro language!), so it is
> not so easy for me to extract the necessary information out of this
> plugin.
>
> Is there anything already available (plugin, macro) that does this job, or
> at least comes close such that I can build upon it? Or do I have to look
> more closely at Ben's plugin (maybe someone could point out what I would
> have to look at!)?
>
>
> Many thanks,
> Johannes