Search in sources :

Example 1 with Point2f

use of org.bytedeco.opencv.opencv_core.Point2f in project karate by karatelabs.

the class OpenCvUtils method drawOnImage.

public static Mat drawOnImage(Mat image, Point2fVector points) {
    Mat dest = image.clone();
    int radius = 5;
    Scalar red = new Scalar(0, 0, 255, 0);
    for (int i = 0; i < points.size(); i++) {
        Point2f p = points.get(i);
        circle(dest, new Point(Math.round(p.x()), Math.round(p.y())), radius, red);
    }
    return dest;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) Point2f(org.bytedeco.opencv.opencv_core.Point2f) Point(org.bytedeco.opencv.opencv_core.Point) Point(org.bytedeco.opencv.opencv_core.Point) AbstractScalar(org.bytedeco.opencv.opencv_core.AbstractScalar) Scalar(org.bytedeco.opencv.opencv_core.Scalar)

Example 2 with Point2f

use of org.bytedeco.opencv.opencv_core.Point2f in project qupath by qupath.

the class DelaunayTriangulation method computeDelaunay.

void computeDelaunay(final List<PathObject> pathObjectList, final double pixelWidth, final double pixelHeight) {
    if (pathObjectList.size() <= 2)
        return;
    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<Point2f> 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 Point2f((float) x, (float) y));
    }
    // Create Delaunay triangulation, updating vertex map
    Subdiv2D subdiv = new Subdiv2D();
    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++) {
        Point2f 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();
// }
}
Also used : Rect(org.bytedeco.opencv.opencv_core.Rect) Point2f(org.bytedeco.opencv.opencv_core.Point2f) PathObject(qupath.lib.objects.PathObject) ArrayList(java.util.ArrayList) Subdiv2D(org.bytedeco.opencv.opencv_imgproc.Subdiv2D) ROI(qupath.lib.roi.interfaces.ROI)

Aggregations

Point2f (org.bytedeco.opencv.opencv_core.Point2f)2 ArrayList (java.util.ArrayList)1 AbstractScalar (org.bytedeco.opencv.opencv_core.AbstractScalar)1 Mat (org.bytedeco.opencv.opencv_core.Mat)1 Point (org.bytedeco.opencv.opencv_core.Point)1 Rect (org.bytedeco.opencv.opencv_core.Rect)1 Scalar (org.bytedeco.opencv.opencv_core.Scalar)1 Subdiv2D (org.bytedeco.opencv.opencv_imgproc.Subdiv2D)1 PathObject (qupath.lib.objects.PathObject)1 ROI (qupath.lib.roi.interfaces.ROI)1