Example usage for org.opencv.imgproc Subdiv2D initDelaunay

List of usage examples for org.opencv.imgproc Subdiv2D initDelaunay

Introduction

In this page you can find the example usage for org.opencv.imgproc Subdiv2D initDelaunay.

Prototype

public void initDelaunay(Rect rect) 

Source Link

Usage

From source file:de.hu_berlin.informatik.spws2014.ImagePositionLocator.TriangleImagePositionLocator.java

License:Open Source License

/**
 * Builds ProjectionTriangles from triangulated markers.
 * Requires OpenCV!/*w w  w .ja  va 2 s. com*/
 */
public void newMarkerAdded(List<Marker> markers) {
    if (markers.size() < 2)
        return;

    if (markers.size() == 2) {
        //Guess third marker
        projs = new ArrayList<ProjectionTriangle>();
        projs.add(new ProjectionTriangle(markers.get(0), markers.get(1)));
    } else {
        Subdiv2D subdiv = new Subdiv2D();
        subdiv.initDelaunay(new Rect(0, 0, imageSize.x, imageSize.y));

        for (Marker m : markers)
            System.out.println("-> " + m.realpoint.longitude + " / " + m.realpoint.latitude);
        for (Marker m : markers)
            subdiv.insert(new Point(m.imgpoint.x, m.imgpoint.y));

        MatOfFloat6 mafloat = new MatOfFloat6();
        subdiv.getTriangleList(mafloat);
        float[] tmparray = mafloat.toArray();

        ArrayList<ProjectionTriangle> tmplist = new ArrayList<ProjectionTriangle>();
        for (int i = 0; i < tmparray.length; i += 6) {
            Marker m1 = findMarkerByPoint(markers, tmparray[i], tmparray[i + 1]);
            Marker m2 = findMarkerByPoint(markers, tmparray[i + 2], tmparray[i + 3]);
            Marker m3 = findMarkerByPoint(markers, tmparray[i + 4], tmparray[i + 5]);

            if (m1 != null && m2 != null && m3 != null)
                tmplist.add(new ProjectionTriangle(m1, m2, m3, settings.getMaxDissimilarityPercent(),
                        settings.getBadTriWeightPenalty(), settings.getMinTriAngleSize()));
        }

        for (ProjectionTriangle mainPt : tmplist) {
            for (ProjectionTriangle subPt : tmplist) {
                if (mainPt != subPt)
                    mainPt.tryAddToProjGroup(subPt);
            }
        }

        projs = tmplist;
    }
}

From source file:qupath.opencv.features.DelaunayTriangulation.java

License:Open Source License

void computeDelaunay(final List<PathObject> pathObjectList, final double pixelWidth, final double pixelHeight) {

    if (pathObjectList.size() <= 2)
        return;/*from  www .  j a v a 2  s. co  m*/

    this.vertexMap = new HashMap<>(pathObjectList.size(), 1f);

    // Extract the centroids
    double minX = Double.POSITIVE_INFINITY;
    double minY = Double.POSITIVE_INFINITY;
    double maxX = Double.NEGATIVE_INFINITY;
    double maxY = Double.NEGATIVE_INFINITY;
    List<Point> centroids = new ArrayList<>(pathObjectList.size());
    for (PathObject pathObject : pathObjectList) {
        ROI pathROI = null;

        // First, try to get a nucleus ROI if we have a cell - otherwise just get the normal ROI
        pathROI = getROI(pathObject);

        // Check if we have a ROI at all
        if (pathROI == null) {
            centroids.add(null);
            continue;
        }
        double x = pathROI.getCentroidX();
        double y = pathROI.getCentroidY();
        if (Double.isNaN(x) || Double.isNaN(y)) {
            centroids.add(null);
            continue;
        }
        if (x < minX)
            minX = x;
        else if (x > maxX)
            maxX = x;
        if (y < minY)
            minY = y;
        else if (y > maxY)
            maxY = y;

        centroids.add(new Point(x, y));
    }

    // Create Delaunay triangulation, updating vertex map
    Subdiv2D subdiv = new Subdiv2D2();
    Rect bounds = new Rect((int) minX - 1, (int) minY - 1, (int) (maxX - minX) + 100,
            (int) (maxY - minY) + 100);
    subdiv.initDelaunay(bounds);
    for (int i = 0; i < centroids.size(); i++) {
        Point p = centroids.get(i);
        if (p == null)
            continue;
        int v = subdiv.insert(p);
        vertexMap.put(v, pathObjectList.get(i));
    }

    updateNodeMap(subdiv, pixelWidth, pixelHeight);

    //      // Connect only the closest paired nodes
    //      Map<DelaunayNode, Double> medianDistances = new HashMap<>();
    //      for (DelaunayNode node : nodeMap.values()) {
    //         medianDistances.put(node, node.medianDistance());
    //      }
    //      
    //      for (DelaunayNode node : nodeMap.values()) {
    //         if (node.nNeighbors() <= 2)
    //            continue;
    //         double distance = medianDistances.get(node);
    //         Iterator<DelaunayNode> iter = node.nodeList.iterator();
    //         while (iter.hasNext()) {
    //            DelaunayNode node2 = iter.next();
    //            if (distance(node, node2) >= distance) {
    //               node2.nodeList.remove(node);
    //               iter.remove();
    //            }
    //         }
    //      }

    //      // Optionally require a minimum number of connected nodes
    //      List<DelaunayNode> toRemove = new ArrayList<>();
    //      for (DelaunayNode node : nodeMap.values()) {
    //         if (node.nNeighbors() <= 2) {
    //            toRemove.add(node);
    //         }
    //      }
    //      for (DelaunayNode node : toRemove) {
    //         for (DelaunayNode node2 : node.nodeList)
    //            node2.nodeList.remove(node);
    //         node.nodeList.clear();
    //      }
    //      for (DelaunayNode node : nodeMap.values()) {
    //         node.ensureDistancesUpdated();
    //         node.ensureTrianglesCalculated();
    //      }
}