public void fitSpline() { double length = getUncalibratedLength(); int evaluationPoints = (int)(length/2.0); if (ic!=null) { double mag = ic.getMagnification(); if (mag<1.0) evaluationPoints *= mag;; } if (evaluationPoints<100) evaluationPoints = 100; fitSpline(evaluationPoints); }
void fitSpline() { Roi roi = imp.getRoi(); if (roi==null) {noRoi("Spline"); return;} int type = roi.getType(); boolean segmentedSelection = type==Roi.POLYGON||type==Roi.POLYLINE; if (!(segmentedSelection||type==Roi.FREEROI||type==Roi.TRACED_ROI||type==Roi.FREELINE)) {IJ.error("Spline Fit", "Polygon or polyline selection required"); return;} if (roi instanceof EllipseRoi) return; PolygonRoi p = (PolygonRoi)roi; Undo.setup(Undo.ROI, imp); if (!segmentedSelection && p.getNCoordinates()>3) { if (p.subPixelResolution()) p = trimFloatPolygon(p, p.getUncalibratedLength()); else p = trimPolygon(p, p.getUncalibratedLength()); } String options = Macro.getOptions(); if (options!=null && options.indexOf("straighten")!=-1) p.fitSplineForStraightening(); else if (options!=null && options.indexOf("remove")!=-1) p.removeSplineFit(); else p.fitSpline(); imp.draw(); LineWidthAdjuster.update(); }
protected void resamplePolygon(Polygon polygon, int npoints) { int[] x = new int[npoints], y = new int[npoints]; PolygonRoi roi = new PolygonRoi(polygon, Roi.POLYLINE); roi.fitSpline(); FloatPolygon floatPolygon = roi.getFloatPolygon(); float[] x1 = floatPolygon.xpoints, y1 = floatPolygon.ypoints; for (int i = 0; i < npoints; i++) { float index = i * (floatPolygon.npoints - 1) / (float)(npoints - 1); int j = (int)Math.floor(index); float f1 = index - j; if (f1 < 1e-5) { x[i] = (int)Math.round(x1[j]); y[i] = (int)Math.round(y1[j]); } else { float f2 = 1 - f1; x[i] = (int)Math.round(x1[j] * f2 + x1[j + 1] * f1); y[i] = (int)Math.round(y1[j] * f2 + y1[j + 1] * f1); } } polygon.npoints = npoints; polygon.xpoints = x; polygon.ypoints = y; }
void fitSpline() { Roi roi = imp.getRoi(); if (roi==null) {noRoi("Spline"); return;} int type = roi.getType(); boolean segmentedSelection = type==Roi.POLYGON||type==Roi.POLYLINE; if (!(segmentedSelection||type==Roi.FREEROI||type==Roi.TRACED_ROI||type==Roi.FREELINE)) {IJ.error("Spline Fit", "Polygon or polyline selection required"); return;} if (roi instanceof EllipseRoi) return; PolygonRoi p = (PolygonRoi)roi; Undo.setup(Undo.ROI, imp); if (!segmentedSelection && p.getNCoordinates()>3) { if (p.subPixelResolution()) p = trimFloatPolygon(p, p.getUncalibratedLength()); else p = trimPolygon(p, p.getUncalibratedLength()); } String options = Macro.getOptions(); if (options!=null && options.indexOf("straighten")!=-1) p.fitSplineForStraightening(); else if (options!=null && options.indexOf("remove")!=-1) p.removeSplineFit(); else p.fitSpline(); imp.draw(); LineWidthAdjuster.update(); }
public void fitSpline() { double length = getUncalibratedLength(); int evaluationPoints = (int)(length/2.0); if (ic!=null) { double mag = ic.getMagnification(); if (mag<1.0) evaluationPoints *= mag;; } if (evaluationPoints<100) evaluationPoints = 100; fitSpline(evaluationPoints); }
protected static void resamplePolygon(Polygon polygon, int npoints) { int[] x = new int[npoints], y = new int[npoints]; PolygonRoi roi = new PolygonRoi(polygon, Roi.POLYLINE); roi.fitSpline(); FloatPolygon floatPolygon = roi.getFloatPolygon(); float[] x1 = floatPolygon.xpoints, y1 = floatPolygon.ypoints; for (int i = 0; i < npoints; i++) { float index = i * (floatPolygon.npoints - 1) / (float)(npoints - 1); int j = (int)Math.floor(index); float f1 = index - j; if (f1 < 1e-5) { x[i] = (int)Math.round(x1[j]); y[i] = (int)Math.round(y1[j]); } else { float f2 = 1 - f1; x[i] = (int)Math.round(x1[j] * f2 + x1[j + 1] * f1); y[i] = (int)Math.round(y1[j] * f2 + y1[j + 1] * f1); } } polygon.npoints = npoints; polygon.xpoints = x; polygon.ypoints = y; }
@Override public void fitSpline() { if (getNCoordinates() > 2) { copyXpToXpf(); super.fitSpline(); copyXpfToXp(); } }
public void adjustmentValueChanged(AdjustmentEvent event) { int sliceNumber = Integer.parseInt(textField.getText()); image.setSlice(sliceNumber); PolygonRoi roi = rois[sliceNumber - 1]; if (roi != null && !roi.isSplineFit()) roi.fitSpline(); if (roi == null) image.killRoi(); else image.setRoi(roi); } }
public void deleteHandle(double ox, double oy) { if (imp==null) return; if (nPoints<=1) { imp.deleteRoi(); return; } boolean splineFit = xSpline!=null; if (splineFit) removeSplineFit(); FloatPolygon points = getFloatPolygon(); int pointToDelete = getClosestPoint(ox, oy, points); if (pointToDelete>=0) { deletePoint(pointToDelete); if (splineFit) fitSpline(splinePoints); imp.draw(); } }
private String setSplineAnchors(ImagePlus imp, boolean polyline) { double[] x = getFirstArray(); int n = x.length; double[] y = getLastArray(); if (y.length!=n) interp.error("Arrays are not the same length"); float[] xcoord = new float[n]; float[] ycoord = new float[n]; for (int i=0; i<n; i++) { xcoord[i] = (float)x[i]; ycoord[i] = (float)y[i]; } Roi roi = null; if (polyline) roi = new PolygonRoi(xcoord, ycoord, n, PolygonRoi.POLYLINE); else roi = new PolygonRoi(xcoord, ycoord, n, PolygonRoi.POLYGON); ((PolygonRoi)roi).fitSpline(); imp.setRoi(roi); return null; }
public void deleteHandle(double ox, double oy) { if (imp==null) return; if (nPoints<=1) { imp.deleteRoi(); return; } boolean splineFit = xSpline!=null; if (splineFit) removeSplineFit(); FloatPolygon points = getFloatPolygon(); int pointToDelete = getClosestPoint(ox, oy, points); if (pointToDelete>=0) { deletePoint(pointToDelete); if (splineFit) fitSpline(splinePoints); imp.draw(); } }
private String setSplineAnchors(ImagePlus imp, boolean polyline) { double[] x = getFirstArray(); int n = x.length; double[] y = getLastArray(); if (y.length!=n) interp.error("Arrays are not the same length"); float[] xcoord = new float[n]; float[] ycoord = new float[n]; for (int i=0; i<n; i++) { xcoord[i] = (float)x[i]; ycoord[i] = (float)y[i]; } Roi roi = null; if (polyline) roi = new PolygonRoi(xcoord, ycoord, n, PolygonRoi.POLYLINE); else roi = new PolygonRoi(xcoord, ycoord, n, PolygonRoi.POLYGON); ((PolygonRoi)roi).fitSpline(); imp.setRoi(roi); return null; }
public void itemStateChanged(ItemEvent e) { boolean selected = e.getStateChange()==ItemEvent.SELECTED; ImagePlus imp = WindowManager.getCurrentImage(); if (imp==null) {checkbox.setState(false); return;}; Roi roi = imp.getRoi(); int type = roi!=null?roi.getType():null; if (roi==null || !(roi instanceof PolygonRoi) || type==Roi.FREEROI || type==Roi.FREELINE || type==Roi.ANGLE) { checkbox.setState(false); return; }; PolygonRoi poly = (PolygonRoi)roi; boolean splineFit = poly.isSplineFit(); if (selected && !splineFit) { poly.fitSpline(); Prefs.splineFitLines = true; imp.draw(); } else if (!selected && splineFit) { poly.removeSplineFit(); Prefs.splineFitLines = false; imp.draw(); } }
public void itemStateChanged(ItemEvent e) { boolean selected = e.getStateChange()==ItemEvent.SELECTED; ImagePlus imp = WindowManager.getCurrentImage(); if (imp==null) {checkbox.setState(false); return;}; Roi roi = imp.getRoi(); int type = roi!=null?roi.getType():null; if (roi==null || !(roi instanceof PolygonRoi) || type==Roi.FREEROI || type==Roi.FREELINE || type==Roi.ANGLE) { checkbox.setState(false); return; }; PolygonRoi poly = (PolygonRoi)roi; boolean splineFit = poly.isSplineFit(); if (selected && !splineFit) { poly.fitSpline(); Prefs.splineFitLines = true; imp.draw(); } else if (!selected && splineFit) { poly.removeSplineFit(); Prefs.splineFitLines = false; imp.draw(); } }
public static void main(String[] args) { PolygonRoiPublic roi = new PolygonRoiPublic(new int[] { 800, 800}, new int[] { 500, 500 }, 2, PolygonRoi.POLYLINE); //roi.addHandle(800, 500); roi.addHandle(100, 200); roi.addHandle(100, 500); roi.addHandle(800, 250); IJ.getImage().setRoi(roi); //IJ.getImage().setRoi((PolygonRoi)roi.clone()); PolygonRoi poly = new PolygonRoi(getXCoordinates(roi), getYCoordinates(roi), getNCoordinates(roi), PolygonRoi.POLYLINE); poly.fitSpline(); //IJ.getImage().setRoi(poly); }
protected static float[] getProfile(ImageProcessor ip, PolygonRoi roi, boolean fitSpline, int lineWidth) { if (fitSpline && !roi.isSplineFit()) { roi = (PolygonRoi)roi.clone(); roi.fitSpline(); } ImagePlus dummy = new ImagePlus("dummy", ip); dummy.setRoi(roi); Straightener straightener = new Straightener(); ip = straightener.straightenLine(dummy, lineWidth); int w = ip.getWidth(); float[] result = new float[w]; // need to average explicitely for (int j = 0; j < w; j++) { float value = ip.getf(j, 0); for (int k = 1; k < lineWidth; k++) value += ip.getf(j, k); result[j] = value / lineWidth; } return result; } }
double[] getWideLineProfile(ImagePlus imp, int lineWidth) { Roi roi = (Roi)imp.getRoi().clone(); ImageProcessor ip2 = (new Straightener()).straightenLine(imp, lineWidth); int width = ip2.getWidth(); int height = ip2.getHeight(); if (ip2 instanceof FloatProcessor) return getColumnAverageProfile(new Rectangle(0,0,width,height),ip2); profile = new double[width]; double[] aLine; ip2.setInterpolate(false); for (int y=0; y<height; y++) { aLine = ip2.getLine(0, y, width-1, y); for (int i=0; i<width; i++) profile[i] += aLine[i]; } for (int i=0; i<width; i++) profile[i] /= height; imp.setRoi(roi); if (roi.getType()==Roi.POLYLINE&& !((PolygonRoi)roi).isSplineFit()) { ((PolygonRoi)roi).fitSpline(); imp.draw(); } return profile; }
double[] getWideLineProfile(ImagePlus imp, int lineWidth) { Roi roi = (Roi)imp.getRoi().clone(); ImageProcessor ip2 = (new Straightener()).straightenLine(imp, lineWidth); int width = ip2.getWidth(); int height = ip2.getHeight(); if (ip2 instanceof FloatProcessor) return getColumnAverageProfile(new Rectangle(0,0,width,height),ip2); profile = new double[width]; double[] aLine; ip2.setInterpolate(false); for (int y=0; y<height; y++) { aLine = ip2.getLine(0, y, width-1, y); for (int i=0; i<width; i++) profile[i] += aLine[i]; } for (int i=0; i<width; i++) profile[i] /= height; imp.setRoi(roi); if (roi.getType()==Roi.POLYLINE&& !((PolygonRoi)roi).isSplineFit()) { ((PolygonRoi)roi).fitSpline(); imp.draw(); } return profile; }
protected void moveHandle(int sx, int sy) { if (clipboard!=null) return; int ox = ic.offScreenX(sx); int oy = ic.offScreenY(sy); if (xpf!=null) { double offset = getOffset(-0.5); double xbase = getXBase(); double ybase = getYBase(); xpf[activeHandle] = (float)(ic.offScreenXD(sx)-xbase+offset); ypf[activeHandle] = (float)(ic.offScreenYD(sy)-ybase+offset); } else { xp[activeHandle] = ox-x; yp[activeHandle] = oy-y; } if (xSpline!=null) { fitSpline(splinePoints); imp.draw(); } else { if (!subPixelResolution() || (type==POINT&&nPoints==1)) resetBoundingRect(); if (type==POINT && width==0 && height==0) {width=1; height=1;} updateClipRectAndDraw(); } String angle = type==ANGLE?getAngleAsString():""; IJ.showStatus(imp.getLocationAsString(ox,oy) + angle); }
protected void moveHandle(int sx, int sy) { if (clipboard!=null) return; int ox = ic.offScreenX(sx); int oy = ic.offScreenY(sy); if (xpf!=null) { double offset = getOffset(-0.5); double xbase = getXBase(); double ybase = getYBase(); xpf[activeHandle] = (float)(ic.offScreenXD(sx)-xbase+offset); ypf[activeHandle] = (float)(ic.offScreenYD(sy)-ybase+offset); } else { xp[activeHandle] = ox-x; yp[activeHandle] = oy-y; } if (xSpline!=null) { fitSpline(splinePoints); imp.draw(); } else { if (!subPixelResolution() || (type==POINT&&nPoints==1)) resetBoundingRect(); if (type==POINT && width==0 && height==0) {width=1; height=1;} updateClipRectAndDraw(); } String angle = type==ANGLE?getAngleAsString():""; IJ.showStatus(imp.getLocationAsString(ox,oy) + angle); }