Login  Register

interpolating polygons

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options Options
Embed post
Permalink
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

interpolating polygons

Tony Shepherd
59 posts
Hi,
I've been scrolling through the source code for ages but I can't find a
method that returns a list of pixel co-ordinates for the whole boundary of a
polygon object (not just the vertices)
-- is there one? or has someone written one? (it needs to somehow
interpolate between the vertices)

Many Thanks
T
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: interpolating polygons

jmutterer
142 posts
Tony, you can run the following macro code which will convert the
polygon to a freehand type of selection.

roiManager("Combine");
getSelectionCoordinates(x,y);

Jerome


On 9/1/06, Tony <[hidden email]> wrote:

> Hi,
> I've been scrolling through the source code for ages but I can't find a
> method that returns a list of pixel co-ordinates for the whole boundary of a
> polygon object (not just the vertices)
> -- is there one? or has someone written one? (it needs to somehow
> interpolate between the vertices)
>
> Many Thanks
> T
>
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: interpolating polygons

Wayne Rasband
1011 posts
In reply to this post by Tony Shepherd
> I've been scrolling through the source code for ages but I can't
> find a method that returns a list of pixel co-ordinates for the
> whole boundary of a polygon object (not just the vertices)
> -- is there one? or has someone written one? (it needs to
> somehow interpolate between the vertices)

The Path_Writer plugin
(http://rsb.info.nih.gov/ij/plugins/path-writer.html) does this.

-wayne
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: interpolating polygons

Tony Shepherd
59 posts
In reply to this post by Tony Shepherd
Thanks Wayne, that works a treat.
I've adapted it to create an object containing connected pixel co-ordinates
if anybody wants to use it (below). Note that the constructor needs an roi
object, which could be an ellipse, polygon or whatever.

T


 public class Tboundary {


    int XY[][];
    int n;  

   // A Tboundary is a list of all connected pixels in the ellipse boundary
   // note that the first and last co-ordinate pairs are the same (first    
      pixel revisited)
   

   public Tboundary(Roi roi)
     {

        Polygon temproi = roi.getPolygon();
        PolygonRoi p = new PolygonRoi(temproi,2);

        int n = p.getNCoordinates();
        int[] x = p.getXCoordinates();
        int[] y = p.getYCoordinates();
        Rectangle r = roi.getBoundingRect();
        int xbase = r.x;
        int ybase = r.y;
        boolean areaPath = roi.getType()<=Roi.TRACED_ROI;
        double length = 0.0;
        double segmentLength;
        int xdelta, ydelta, iLength;
        double[] segmentLengths = new double[n];
        int[] dx = new int[n];
        int[] dy = new int[n];
        for (int i=0; i<(n-1); i++) {
            xdelta = x[i+1] - x[i];
            ydelta = y[i+1] - y[i];
            segmentLength = Math.sqrt(xdelta*xdelta+ydelta*ydelta);
            length += segmentLength;
            segmentLengths[i] = segmentLength;
            dx[i] = xdelta;
            dy[i] = ydelta;
        }
        if (areaPath) {
            xdelta = x[0] - x[n-1];
            ydelta = y[0] - y[n-1];
            segmentLength = Math.sqrt(xdelta*xdelta+ydelta*ydelta);
            length += segmentLength;
            segmentLengths[n-1] = segmentLength;
            dx[n-1] = xdelta;
            dy[n-1] = ydelta;
       }

        int size = (int)(1.1*length);
        int arrayLength = size;
        int[][] xypath = new int[2][size];
        int pathLength = 0;
        double leftOver = 1.0;
        double distance = 0.0;
        int index = -1;
        for (int i=0; i<n; i++) {
            double len = segmentLengths[i];
            if (len==0.0)
                continue;
            double xinc = dx[i]/len;
            double yinc = dy[i]/len;
            double start = 1.0-leftOver;
            double rx = xbase+x[i]+start*xinc;
            double ry = ybase+y[i]+start*yinc;
            double len2 = len - start;
            int n2 = (int)len2;
            for (int j=0; j<=n2; j++) {
                index++;
                if (index<xypath[0].length) {
                    xypath[0][index] = (int)Math.round(rx);
                    xypath[1][index] = (int)Math.round(ry);
                    pathLength = index+1;
                }
                rx += xinc;
                ry += yinc;
            }
            distance += len;
            leftOver = len2 - n2;
        }

        this.XY = xypath;
        this.n = pathLength;

     }
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: interpolating polygons

Tony Shepherd
59 posts
In reply to this post by Tony Shepherd
didn't make that so clear did I.
The point is, you need to set the integer in

PolygonRoi p = new PolygonRoi(temproi,2);

(i.e. 2 above) to match the Roi TYPE you're dealing with. (2=ellipse,
1=polygon etc).