get continuous pixel coordinates from a line ROI

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

get continuous pixel coordinates from a line ROI

albertoduina@virgilio.it
Hi all,
this question appear many times in the mailing list and Wayne has posted a  
macro for the solution.
This is a plugin subroutine prototype for the same problem, it works BUT  
if the Roi is an oblique line the profiX and the profiZ arrays have  
different length (profiZ 1 element more than profiX). If the start and end  
of the oblique line are on the image boundaries the last element of profiZ  
is every time 0 (i have tested many times).
Other than that the subroutine work apparently well.


//------------------------------------
        public static double[][] decomposer(ImagePlus imp1) {

                Line line = (Line) imp1.getRoi();
                if (line == null || !(line.isLine())) {
                        IJ.error("Line selection required.");
                        return null;
                }
                double[] profiZ = line.getPixels();

                // now for obtain the pixels coordinate i need a PolygonRoi
                float[] xcoord = new float[2];
                float[] ycoord = new float[2];
                xcoord[0] = (float) line.x1d;
                ycoord[0] = (float) line.y1d;
                xcoord[1] = (float) line.x2d;
                ycoord[1] = (float) line.y2d;
                imp1.setRoi(new PolygonRoi(xcoord, ycoord, 2, Roi.POLYLINE));
                PolygonRoi polyline = (PolygonRoi) imp1.getRoi();
                polyline.fitSplineForStraightening();
                FloatPolygon fp1 = polyline.getFloatPolygon();
                int len = fp1.xpoints.length;
                double[] profiX = new double[len];
                double[] profiY = new double[len];
                for (int i1 = 0; i1 < len; i1++) {
                        profiX[i1] = (double) fp1.xpoints[i1];
                        profiY[i1] = (double) fp1.ypoints[i1];
                }
                if (profiX.length != profiZ.length) {
                        IJ.log("different length, profi2x= " + profiX.length
                                        + " profi2z= " + profiZ.length);
                }
                double[][] out1 = new double[3][profiX.length];
                out1[0] = profiX;
                out1[1] = profiY;
                out1[2] = profiZ;
                return out1;
        }
//----------------------------------

Can somebody suggest a workaround for have a perfect plugin solution?
Thanks
Alberto Duina

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: get continuous pixel coordinates from a line ROI

Rasband, Wayne (NIH/NIMH) [E]
On Aug 22, 2013, at 10:47 AM, Alberto Duina wrote:

> Hi all,
> this question appear many times in the mailing list and Wayne has posted a macro for the solution.
> This is a plugin subroutine prototype for the same problem, it works BUT if the Roi is an oblique line the profiX and the profiZ arrays have different length (profiZ 1 element more than profiX). If the start and end of the oblique line are on the image boundaries the last element of profiZ is every time 0 (i have tested many times).
> Other than that the subroutine work apparently well.

You can use the Roi.getInterpolatedPolygon() method to get continuous pixel coordinates from a line selection. Here is a JavaScript example:

  imp = IJ.createImage("Untitled", "8-bit black", 100, 100, 1);
  imp.show();
  line = new Line(14, 13, 20, 9);
  line.setStrokeColor(Color.red);
  imp.setRoi(line);
  IJ.run(imp, "To Selection", "");
  imp.setOverlay(new Overlay(line));
  p = line.getInterpolatedPolygon();
  points = new PointRoi(p.xpoints, p.ypoints, p.npoints);
  imp.setRoi(points);

The ImageJ 1.48b daily build fixes a bug that causes getInterpolatedPolygon() to sometimes return fewer than the expected number of points.

-wayne

> //------------------------------------
> public static double[][] decomposer(ImagePlus imp1) {
>
> Line line = (Line) imp1.getRoi();
> if (line == null || !(line.isLine())) {
> IJ.error("Line selection required.");
> return null;
> }
> double[] profiZ = line.getPixels();
>
> // now for obtain the pixels coordinate i need a PolygonRoi
> float[] xcoord = new float[2];
> float[] ycoord = new float[2];
> xcoord[0] = (float) line.x1d;
> ycoord[0] = (float) line.y1d;
> xcoord[1] = (float) line.x2d;
> ycoord[1] = (float) line.y2d;
> imp1.setRoi(new PolygonRoi(xcoord, ycoord, 2, Roi.POLYLINE));
> PolygonRoi polyline = (PolygonRoi) imp1.getRoi();
> polyline.fitSplineForStraightening();
> FloatPolygon fp1 = polyline.getFloatPolygon();
> int len = fp1.xpoints.length;
> double[] profiX = new double[len];
> double[] profiY = new double[len];
> for (int i1 = 0; i1 < len; i1++) {
> profiX[i1] = (double) fp1.xpoints[i1];
> profiY[i1] = (double) fp1.ypoints[i1];
> }
> if (profiX.length != profiZ.length) {
> IJ.log("different length, profi2x= " + profiX.length
> + " profi2z= " + profiZ.length);
> }
> double[][] out1 = new double[3][profiX.length];
> out1[0] = profiX;
> out1[1] = profiY;
> out1[2] = profiZ;
> return out1;
> }
> //----------------------------------
>
> Can somebody suggest a workaround for have a perfect plugin solution?
> Thanks
> Alberto Duina
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

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