Java Code Examples for ij.gui.Roi#POLYGON

The following examples show how to use ij.gui.Roi#POLYGON . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MatPolygonRoiConverter.java    From IJ-OpenCV with GNU General Public License v3.0 6 votes vote down vote up
@Override
public < T> T convert(Object o, Class< T> type) {
    Mat m =(Mat)o;
    opencv_core.MatVector xy = new opencv_core.MatVector(2);
    split(m, xy);
    int h = m.size().height();
    int[] xpoints = new int[h];
    int[] ypoints = new int[h];

    for (int i = 0; i < h; i++) {
        xpoints[i] = xy.get(0).getIntBuffer().get(i);
        ypoints[i] = xy.get(1).getIntBuffer().get(i);
    }

    PolygonRoi pr = new PolygonRoi(xpoints, ypoints, h, Roi.POLYGON);
    return (T)pr;

}
 
Example 2
Source File: Polygon2D.java    From MorphoLibJ with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Converts this polygon into an ImageJ Polygon ROI.
 * 
 * @return the corresponding PolygonRoi
 */
public PolygonRoi createRoi()
{
	// allocate memory for data arrays
	int n = this.vertices.size();
	float[] px = new float[n];
	float[] py = new float[n];
	
	// extract coordinates
	for (int i = 0; i < n; i++)
	{
		Point2D p = this.vertices.get(i);
		px[i] = (float) p.getX();
		py[i] = (float) p.getY();
	}

	// create ROI data structure
	return new PolygonRoi(px, py, n, Roi.POLYGON);
}
 
Example 3
Source File: RotatedRectPolygonRoiConverter.java    From IJ-OpenCV with GNU General Public License v3.0 5 votes vote down vote up
@Override
public < T> T convert(Object o, Class< T> type) {
    RotatedRect rr = (opencv_core.RotatedRect) o;
    double _angle = rr.angle() * Math.PI / 180.0;
    double b = (double) Math.cos(_angle) * 0.5f;
    double a = (double) Math.sin(_angle) * 0.5f;

    opencv_core.Point pt0 = new opencv_core.Point(
            (int) (rr.center().x() - a * rr.size().height() - b * rr.size().width()),
            (int) (rr.center().y() + b * rr.size().height() - a * rr.size().width()));

    opencv_core.Point pt1 = new opencv_core.Point(
            (int) (rr.center().x() + a * rr.size().height() - b * rr.size().width()),
            (int) (rr.center().y() - b * rr.size().height() - a * rr.size().width()));

    opencv_core.Point pt2 = new opencv_core.Point((int) (2 * rr.center().x() - pt0.x()),
            (int) (2 * rr.center().y() - pt0.y()));

    opencv_core.Point pt3 = new opencv_core.Point((int) (2 * rr.center().x() - pt1.x()),
            (int) (2 * rr.center().y() - pt1.y()));

    int[] xpoints = new int[4];
    int[] ypoints = new int[4];

    xpoints[0] = pt0.x();
    ypoints[0] = pt0.y();
    xpoints[1] = pt1.x();
    ypoints[1] = pt1.y();
    xpoints[2] = pt2.x();
    ypoints[2] = pt2.y();
    xpoints[3] = pt3.x();
    ypoints[3] = pt3.y();

    PolygonRoi pr = new PolygonRoi(xpoints, ypoints, 4, Roi.POLYGON);
    return (T)pr;

}
 
Example 4
Source File: Point2fPolygonRoiConverter.java    From IJ-OpenCV with GNU General Public License v3.0 5 votes vote down vote up
@Override
public < T> T convert(Object o, Class< T> type) {
opencv_core.Point2f p = (opencv_core.Point2f) o;
    int[] xpoints = new int[p.sizeof() / 2];
    int[] ypoints = new int[p.sizeof() / 2];

    for (int i = 0; i < p.sizeof(); i = i + 2) {

        xpoints[i / 2] = (int) p.get(i);
        ypoints[i / 2] = (int) p.get(i + 1);
    }
    PolygonRoi pr = new PolygonRoi(xpoints, ypoints, p.sizeof() / 2, Roi.POLYGON);
    return (T)pr;
}
 
Example 5
Source File: EquivalentEllipsePlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
private final static Roi createRoi(Ellipse ellipse)
{
	// Coordinates of ellipse, in pixel coordinates
	Point2D center = ellipse.center();
	double xc = center.getX();
	double yc = center.getY();
	
	double r1 = ellipse.radius1();
	double r2 = ellipse.radius2();
	double theta = Math.toRadians(ellipse.orientation());
	
	double cot = Math.cos(theta);
	double sit = Math.sin(theta);
	
	int nVertices = 100;
	float[] xv = new float[nVertices];
	float[] yv = new float[nVertices];
	for (int i = 0; i < nVertices; i++)
	{
		double t = i * Math.PI * 2.0 / nVertices;
		double x = Math.cos(t) * r1;
		double y = Math.sin(t) * r2;
		
		xv[i] = (float) (x * cot - y * sit + xc);
		yv[i] = (float) (x * sit + y * cot + yc);
	}
	
	return new PolygonRoi(xv, yv, nVertices, Roi.POLYGON);
}
 
Example 6
Source File: InertiaEllipsePlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
private final static Roi createRoi(Ellipse ellipse)
{
	// Coordinates of ellipse, in pixel coordinates
	Point2D center = ellipse.center();
	double xc = center.getX();
	double yc = center.getY();
	
	double r1 = ellipse.radius1();
	double r2 = ellipse.radius2();
	double theta = Math.toRadians(ellipse.orientation());
	
	double cot = Math.cos(theta);
	double sit = Math.sin(theta);
	
	int nVertices = 100;
	float[] xv = new float[nVertices];
	float[] yv = new float[nVertices];
	for (int i = 0; i < nVertices; i++)
	{
		double t = i * Math.PI * 2.0 / nVertices;
		double x = Math.cos(t) * r1;
		double y = Math.sin(t) * r2;
		
		xv[i] = (float) (x * cot - y * sit + xc);
		yv[i] = (float) (x * sit + y * cot + yc);
	}
	
	return new PolygonRoi(xv, yv, nVertices, Roi.POLYGON);
}
 
Example 7
Source File: OrientedBoundingBoxPlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Determines the ROI corresponding to the uncalibrated version of this
 * box, assuming it was defined in calibrated coordinates.
 * 
 * @param box
 *            the oriented box in calibrated coordinates
 * @param calib
 *            the spatial calibration to consider
 * @return the ROI corresponding to the box
 */
private final static Roi createUncalibratedRoi(OrientedBox2D box, Calibration calib)
{
	Point2D center = box.center();
	double xc = center.getX();
	double yc = center.getY();
	double dx = box.length() / 2;
	double dy = box.width() / 2;
	double theta = Math.toRadians(box.orientation());
	double cot = Math.cos(theta);
	double sit = Math.sin(theta);
	
	// coordinates of polygon ROI
	float[] xp = new float[4];
	float[] yp = new float[4];
	
	// iterate over vertices
	double x, y;
	x = xc + dx * cot - dy * sit;
	y = yc + dx * sit + dy * cot;
	xp[0] = (float) ((x - calib.xOrigin) / calib.pixelWidth);
	yp[0] = (float) ((y - calib.yOrigin) / calib.pixelHeight);
	x = xc - dx * cot - dy * sit;
	y = yc - dx * sit + dy * cot;
	xp[1] = (float) ((x - calib.xOrigin) / calib.pixelWidth);
	yp[1] = (float) ((y - calib.yOrigin) / calib.pixelHeight);
	x = xc - dx * cot + dy * sit;
	y = yc - dx * sit - dy * cot;
	xp[2] = (float) ((x - calib.xOrigin) / calib.pixelWidth);
	yp[2] = (float) ((y - calib.yOrigin) / calib.pixelHeight);
	x = xc + dx * cot + dy * sit;
	y = yc + dx * sit - dy * cot;
	xp[3] = (float) ((x - calib.xOrigin) / calib.pixelWidth);
	yp[3] = (float) ((y - calib.yOrigin) / calib.pixelHeight);
	return new PolygonRoi(xp, yp, 4, Roi.POLYGON);
}
 
Example 8
Source File: ObjectFeatureBuilderTiled.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
/**
 * returns ShapeNames features (>= model version 10)
 *
 * @param shape
 * @return
 */
private List<Double> getFeatureShapeDescriptorsNew(Shape shape) {
    List<Double> feats = new ArrayList<Double>(getShapeNames().length);
    double ratio = Double.NaN;
    double perimeter = Double.NaN;
    double area = Double.NaN;
    double circularity = Double.NaN;
    double solidity = Double.NaN;
    double convexity = Double.NaN;
    double compactness = Double.NaN;
    double roundness = Double.NaN;
    double centerX = Double.NaN;
    double centerY = Double.NaN;
    double centroidX = Double.NaN;
    double centroidY = Double.NaN;
    if (shape instanceof Polygon) {
        Polygon p = (Polygon) shape;
        PolygonMetrics pm = new PolygonMetrics(p);
        PolygonRoi pr = new PolygonRoi(p, Roi.POLYGON);
        Polygon convexHull = pr.getConvexHull();
        double[] ferets = pr.getFeretValues();
        double majorAxis = ferets[0];
        //double minorAxis = ferets[2];

        area = pm.getArea();
        perimeter = pm.getPerimeter();
        roundness = pm.getRoundnessNew(area, majorAxis);
        circularity = pm.getCircularity(area, perimeter);
        solidity = pm.getSolidity(convexHull, area);
        convexity = pm.getConvexity(convexHull, perimeter);
        compactness = pm.getCompactness(area, majorAxis);
        ratio = pm.getAspectRatio(ferets);

        // centerX/Y
        Point2D center = pm.getCenter();
        centerX = Math.abs(center.getX());
        centerY = Math.abs(center.getY());

    }

    feats.add(roundness);
    feats.add(circularity);
    feats.add(solidity);
    feats.add(convexity);
    feats.add(compactness);
    feats.add(area);
    feats.add(perimeter);
    feats.add(ratio);
    feats.add(centerX);
    feats.add(centerY);
    return feats;
}