Login  Register

Re: Analyze Particle -> Perimeter

Posted by Jonathan Hilmer on Jul 18, 2006; 7:16pm
URL: http://imagej.273.s1.nabble.com/Analyze-Particle-Perimeter-tp3702116p3702117.html

My background isn't in Java, so forgive me if I confuse some technical details.

First, it depends on the shape.  If you have a rectangle, it comes
from this code:

/** Returns the perimeter length. */
        public double getLength() {
                double pw=1.0, ph=1.0;
                if (imp!=null) {
                        Calibration cal = imp.getCalibration();
                        pw = cal.pixelWidth;
                        ph = cal.pixelHeight;
                }
                return 2.0*width*pw+2.0*height*ph;
        }

There are other functions called depending on the shape.  I think the
most general is provided by ShapeRoi.java, which is used for irregular
objects:

/**Returns the length of this shape (perimeter, if shape is closed). */
        public double getLength() {
                if(shape==null) return 0.0;
                Rectangle2D r2d = shape.getBounds2D();
                double w = r2d.getWidth();
                double h = r2d.getHeight();
                if(w==0 && h==0) return 0.0;
                PathIterator pIter;
                flatten = true;
                if(flatten) pIter = getFlatteningPathIterator(shape, flatness);
                else pIter = shape.getPathIterator(new AffineTransform());
                double[] par = new double[1];
                parsePath(pIter, par, null, null, null);
                flatten = false;
                return par[0];
        }

The important part is in the last few lines: the function returns the
length of the shape via the 'getPathIterator'.  This isn't unique to
ImageJ, but comes from java.awt.Shape.  I'm not entirely sure how or
when the object in ImageJ becomes associated with or interpreted as a
Java shape, but the perimeter ultimately comes from stepping around
the border of an ImageJ object defined by a Java class.