private void fillPolygon(ImageProcessor ip, Polygon p, boolean internContour) { PolygonRoi proi = new PolygonRoi(p, PolygonRoi.POLYGON); Rectangle r = proi.getBounds(); PolygonFiller pf = new PolygonFiller(); pf.setPolygon(proi.getXCoordinates(), proi.getYCoordinates(), proi.getNCoordinates()); ip.setRoi(r); ImageProcessor objectMask = pf.getMask(r.width, r.height); ip.fill(objectMask); if(!internContour){ ip.drawPolygon(p); } }
protected PolygonRoi flipROI(PolygonRoi roi) { if (roi.isSplineFit()) { roi = (PolygonRoi)roi.clone(); roi.removeSplineFit(); } Polygon polygon = roi.getPolygon(); for (int i = 0; i < polygon.npoints / 2; i++) { int dummy = polygon.xpoints[i]; polygon.xpoints[i] = polygon.xpoints[polygon.npoints - 1 - i]; polygon.xpoints[polygon.npoints - 1 - i] = dummy; dummy = polygon.ypoints[i]; polygon.ypoints[i] = polygon.ypoints[polygon.npoints - 1 - i]; polygon.ypoints[polygon.npoints - 1 - i] = dummy; } return new PolygonRoi(polygon, roi.getType()); }
PolygonRoi trimFloatPolygon(PolygonRoi roi, double length) { FloatPolygon poly = roi.getFloatPolygon(); float[] x = poly.xpoints; float[] y = poly.ypoints; int type = roi.getType()==Roi.FREELINE?Roi.POLYLINE:Roi.POLYGON; if (type==Roi.POLYLINE && distance>0.0) { x[i2] = (float)x2; i2++; PolygonRoi p = new PolygonRoi(x, y, i2, type); if (roi.getStroke()!=null) p.setStrokeWidth(roi.getStrokeWidth()); p.setStrokeColor(roi.getStrokeColor()); p.setDrawOffset(roi.getDrawOffset()); p.setName(roi.getName()); imp.setRoi(p); return p;
protected Polygon getPolygon(int sliceIndex) { PolygonRoi roi = rois[sliceIndex]; if (roi.isSplineFit()) { roi = (PolygonRoi)roi.clone(); roi.removeSplineFit(); } return roi.getPolygon(); }
/** * Returns the convex hull of the blob. * @return The convex hull as polygon */ public Polygon getConvexHull() { PolygonRoi roi = new PolygonRoi(outerContour, Roi.POLYGON); Polygon hull = roi.getConvexHull(); if(hull==null){ return getOuterContour(); } return hull; }
void doIrregularSetup(Roi roi) { n = ((PolygonRoi)roi).getNCoordinates(); int[] ix = ((PolygonRoi)roi).getXCoordinates(); int[] iy = ((PolygonRoi)roi).getYCoordinates(); x = new double[n]; y = new double[n];
PolygonRoi trimPolygon(PolygonRoi roi, double length) { int[] x = roi.getXCoordinates(); int[] y = roi.getYCoordinates(); int n = roi.getNCoordinates(); x = smooth(x, n); y = smooth(y, n); float[] curvature = getCurvature(x, y, n); Rectangle r = roi.getBounds(); double threshold = rodbard(length); int type = roi.getType()==Roi.FREELINE?Roi.POLYLINE:Roi.POLYGON; if (type==Roi.POLYLINE && distance>0.0) { x[i2] = x2 + r.x; i2++; PolygonRoi p = new PolygonRoi(x, y, i2, type); if (roi.getStroke()!=null) p.setStrokeWidth(roi.getStrokeWidth()); p.setStrokeColor(roi.getStrokeColor()); p.setName(roi.getName()); imp.setRoi(p); return p;
public Roi seam2roi(int[] seam, boolean vertical) { int[] other = new int[vertical ? h : w]; for (int i = 0; i < other.length; i++) other[i] = i; return new PolygonRoi(vertical ? seam : other, vertical ? other : seam, seam.length, PolygonRoi.FREELINE); } }
public void mark(int slice, int distance) { image.setPosition(slice); if (distance < 0) { unmark(); return; } PolygonRoi roi = rois[slice - 1]; if (roi == null) { image.killRoi(); return; } image.setRoi(roi); roi = (PolygonRoi)roi.clone(); roi.fitSplineForStraightening(); if (distance >= roi.getNCoordinates()) { unmark(); return; } Rectangle bounds = roi.getBounds(); int x = roi.getXCoordinates()[distance] + bounds.x; int y = roi.getYCoordinates()[distance] + bounds.y; Overlay overlay = new Overlay(new OvalRoi(x - 10, y - 10, 21, 21)); image.setOverlay(overlay); }
if (roi==null) return null; if (roi.getState()==Roi.CONSTRUCTING) roi.exitConstructingMode(); if (roi.isSplineFit()) roi.removeSplineFit(); int type = roi.getType(); int n = roi.getNCoordinates(); double len = roi.getLength(); roi.fitSplineForStraightening(); if (roi.getNCoordinates()<2) return null; FloatPolygon p = roi.getFloatPolygon(); n = p.npoints; ImageProcessor ip = imp.getProcessor(); roi.removeSplineFit(); else imp.draw();
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; }
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(); }
@Override public void mouseDragged(MouseEvent e) { if (activeHandle >= 0) { ImagePlus image = getImagePlus(e); Roi roi = image.getRoi(); if (roi != null) { if (roi instanceof PolygonRoi) { PolygonRoi polygonROI = (PolygonRoi)roi; Rectangle bounds = polygonROI.getBounds(); polygonROI.getXCoordinates()[activeHandle] = getOffscreenX(e) - bounds.x; polygonROI.getYCoordinates()[activeHandle] = getOffscreenY(e) - bounds.y; setRoi(image, polygonROI); } } } e.consume(); }
public int numVertices() { return poly.getNCoordinates(); }
int[] xCoordinates = roi.getPolygon().xpoints; int[] yCoordinates = roi.getPolygon().ypoints; String st1 = roi.getTypeAsString(); String points = "1"; for (int i = 0; i < xCoordinates.length; i++){ store.setPolylineText(roi.getName(), roiNum, shape); if (c >= 0) { store.setPolylineTheC(unwrap(c), roiNum, shape); if (roi.getStrokeWidth() > 0) { store.setPolylineStrokeWidth(new Length((roi.getStrokeWidth()), UNITS.PIXEL), roiNum, shape); if (roi.getStrokeColor() != null) { store.setPolylineStrokeColor(toOMExmlColor(roi.getStrokeColor()), roiNum, shape); if (roi.getFillColor() != null) { store.setPolylineFillColor(toOMExmlColor(roi.getFillColor()) , roiNum, shape); store.setPolygonText(roi.getName(), roiNum, shape); if (c >= 0) { store.setPolygonTheC(unwrap(c), roiNum, shape); if (roi.getStrokeWidth() > 0) { store.setPolygonStrokeWidth(new Length((roi.getStrokeWidth()), UNITS.PIXEL), roiNum, shape); if (roi.getStrokeColor() != null) {
private void addPolyLineToOverlay(final FloatPolygon p, final int z_position, final int roi_id, final Overlay overlay) { if (p.npoints > 0) { if (p.npoints == 1) { // create 1-pixel length lines for single points p.xpoints[0] -= 0.5f; p.ypoints[0] -= 0.5f; p.addPoint(p.xpoints[0] + 0.5f, p.ypoints[0] + 0.5f); } final PolygonRoi polyline = new PolygonRoi(p, Roi.FREELINE); polyline.enableSubPixelResolution(); // polyline.fitSplineForStraightening(); if (name == null) setDefaultName(); polyline.setStrokeColor(getColor()); polyline.setName(String.format(name + "-%04d-Z%d", roi_id, z_position)); polyline.setPosition(z_position + 1); // index 1 overlay.add(polyline); } }
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; }
/** * Calculates the min feret diameter of the outer contour * @return The feret diameter of the outer contour. */ public double getMinFeretDiameter() { PolygonRoi proi = new PolygonRoi(outerContour, PolygonRoi.POLYLINE); ImagePlus imp = new ImagePlus(); imp.setCalibration(cal); proi.setImage(imp); return proi.getFeretValues()[2]; }
/** * {@inheritDoc} * <p> * This includes some additional points near the vertices of the polyline. * </p> */ @Override public boolean test(final RealLocalizable t) { final float[] x = roi.getFloatPolygon().xpoints; final float[] y = roi.getFloatPolygon().ypoints; final double xt = t.getDoublePosition(0); final double yt = t.getDoublePosition(1); for (int i = 1; i < getRoi().getNCoordinates(); i++) { if (lineContains(x[i - 1], y[i - 1], x[i], y[i], xt, yt, roi .getStrokeWidth())) return true; } return false; }
/** * Calculates the feret diameter of the outer contour * @return The feret diameter of the outer contour. */ public double getFeretDiameter() { PolygonRoi proi = new PolygonRoi(outerContour, PolygonRoi.POLYLINE); ImagePlus imp = new ImagePlus(); imp.setCalibration(cal); proi.setImage(imp); return proi.getFeretsDiameter(); }