Search in sources :

Example 1 with Rect

use of org.bytedeco.opencv.opencv_core.Rect 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)

Example 2 with Rect

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

the class OpenCvUtils method collect.

private static int collect(int strictness, List<Region> found, boolean findAll, RobotBase robot, Mat source, Mat target, double scale) {
    int targetWidth = target.cols();
    int targetHeight = target.rows();
    int targetMinVal = targetWidth * targetHeight * TARGET_MINVAL_FACTOR * strictness;
    Mat result = new Mat();
    int[] minData = templateAndMin(strictness, scale, source, target, result);
    int minValue = minData[2];
    if (minValue > targetMinVal) {
        logger.debug("no match at scale {}, minVal: {} / {} at {}:{}", scale, minValue, targetMinVal, minData[0], minData[1]);
        if (robot != null && robot.debug) {
            Rect rect = new Rect(minData[0], minData[1], targetWidth, targetHeight);
            Mat temp = drawOnImage(source, rect, Scalar.RED);
            show(temp, scale + " " + minData[0] + ":" + minData[1] + " " + minValue + " / " + targetMinVal);
        }
        return minData[2];
    }
    logger.debug("found match at scale {}, minVal: {} / {} at {}:{}", scale, minValue, targetMinVal, minData[0], minData[1]);
    if (findAll) {
        List<int[]> points = getPointsBelowThreshold(result, targetMinVal);
        for (int[] p : points) {
            Region region = toRegion(robot, p, scale, targetWidth, targetHeight);
            found.add(region);
        }
    } else {
        Region region = toRegion(robot, minData, scale, targetWidth, targetHeight);
        found.add(region);
    }
    return minValue;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) Rect(org.bytedeco.opencv.opencv_core.Rect) Point(org.bytedeco.opencv.opencv_core.Point)

Aggregations

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