Search in sources :

Example 1 with Point

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

the class OpenCvUtils method getPointsBelowThreshold.

private static List<int[]> getPointsBelowThreshold(Mat src, double threshold) {
    Mat dst = new Mat();
    threshold(src, dst, threshold, 1, CV_THRESH_BINARY_INV);
    Mat non = new Mat();
    findNonZero(dst, non);
    int len = (int) non.total();
    int xPrev = -BLOCK_SIZE;
    int yPrev = -BLOCK_SIZE;
    int countPrev = 0;
    int xSum = 0;
    int ySum = 0;
    List<int[]> points = new ArrayList(len);
    for (int i = 0; i < len; i++) {
        Pointer ptr = non.ptr(i);
        Point p = new Point(ptr);
        int x = p.x();
        int y = p.y();
        int xDelta = Math.abs(x - xPrev);
        int yDelta = Math.abs(y - yPrev);
        if (xDelta < BLOCK_SIZE && yDelta < BLOCK_SIZE) {
            countPrev++;
            xSum += x;
            ySum += y;
        } else {
            if (countPrev > 0) {
                int xFinal = Math.floorDiv(xSum, countPrev);
                int yFinal = Math.floorDiv(ySum, countPrev);
                // logger.debug("end: {}:{}", xFinal, yFinal);
                points.add(new int[] { xFinal, yFinal });
            }
            xSum = x;
            ySum = y;
            countPrev = 1;
        }
        xPrev = x;
        yPrev = y;
    }
    if (countPrev > 0) {
        int xFinal = Math.floorDiv(xSum, countPrev);
        int yFinal = Math.floorDiv(ySum, countPrev);
        points.add(new int[] { xFinal, yFinal });
    }
    return points;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) ArrayList(java.util.ArrayList) DoublePointer(org.bytedeco.javacpp.DoublePointer) Pointer(org.bytedeco.javacpp.Pointer) Point(org.bytedeco.opencv.opencv_core.Point) Point(org.bytedeco.opencv.opencv_core.Point)

Example 2 with Point

use of org.bytedeco.opencv.opencv_core.Point 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 3 with Point

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

the class OpenCvUtils method templateAndMin.

private static int[] templateAndMin(int strictness, double scale, Mat source, Mat target, Mat result) {
    Mat resized = scale == 1 ? source : rescale(source, scale);
    matchTemplate(resized, target, result, CV_TM_SQDIFF);
    DoublePointer minValPtr = new DoublePointer(1);
    DoublePointer maxValPtr = new DoublePointer(1);
    Point minPt = new Point();
    Point maxPt = new Point();
    minMaxLoc(result, minValPtr, maxValPtr, minPt, maxPt, null);
    int minVal = (int) minValPtr.get();
    int x = minPt.x();
    int y = minPt.y();
    return new int[] { x, y, minVal };
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) DoublePointer(org.bytedeco.javacpp.DoublePointer) Point(org.bytedeco.opencv.opencv_core.Point) Point(org.bytedeco.opencv.opencv_core.Point)

Example 4 with Point

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

the class OpenCVTools method filterSingleZ.

// Alternative weighted sum code that converts to 32-bit
// static void weightedSum(List<Mat> mats, double[] weights, Mat dest) {
// boolean isFirst = true;
// for (int i = 0; i < weights.length; i++) {
// double w = weights[i];
// if (w == 0)
// continue;
// var temp = mats.get(i);
// int type = temp.depth();
// if (type != opencv_core.CV_32F && type != opencv_core.CV_64F) {
// var temp2 = new Mat();
// temp.convertTo(temp2, opencv_core.CV_32F);
// temp = temp2;
// }
// if (isFirst) {
// dest.put(opencv_core.multiply(temp, w));
// isFirst = false;
// } else
// opencv_core.scaleAdd(temp, w, dest, dest);
// if (mats.get(i) != temp)
// temp.release();
// }
// // TODO: Check this does something sensible!
// if (isFirst) {
// dest.create(mats.get(0).size(), mats.get(0).type());
// dest.put(Scalar.ZERO);
// }
// }
/**
 * Apply a filter along the 'list' dimension for a list of Mats, computing the value
 * for a single entry. This is effectively computing a weighted sum of images in the list.
 * <p>
 * Note: this method does not change the depth of the input images.
 * If a floating point output is needed, the Mats should be converted before input.
 *
 * @param mats
 * @param kernel
 * @param ind3D
 * @param border
 * @return
 */
public static Mat filterSingleZ(List<Mat> mats, double[] kernel, int ind3D, int border) {
    // Calculate weights for each image
    int n = mats.size();
    int halfSize = kernel.length / 2;
    int startInd = ind3D - halfSize;
    int endInd = startInd + kernel.length;
    double[] weights = new double[mats.size()];
    int k = 0;
    for (int i = startInd; i < endInd; i++) {
        int ind = ensureInRange(i, n, border);
        weights[ind] += kernel[k];
        k++;
    }
    Mat result = new Mat();
    weightedSum(mats, weights, result);
    return result;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) Point(org.bytedeco.opencv.opencv_core.Point)

Example 5 with Point

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

the class OpenCVTools method getCircularStructuringElement.

/**
 * Create a Mat depicting a circle of the specified radius.
 * <p>
 * Pixels within the circle have the value 1, pixels outside are 0.
 *
 * @param radius
 * @return
 * @deprecated {@link #createDisk(int, boolean)} gives more reliable shapes.
 */
@Deprecated
public static Mat getCircularStructuringElement(int radius) {
    // TODO: Find out why this doesn't just call a standard request for a strel...
    Mat strel = new Mat(radius * 2 + 1, radius * 2 + 1, opencv_core.CV_8UC1, Scalar.ZERO);
    opencv_imgproc.circle(strel, new Point(radius, radius), radius, Scalar.ONE, -1, opencv_imgproc.LINE_8, 0);
    return strel;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) Point(org.bytedeco.opencv.opencv_core.Point)

Aggregations

Mat (org.bytedeco.opencv.opencv_core.Mat)7 Point (org.bytedeco.opencv.opencv_core.Point)7 DoublePointer (org.bytedeco.javacpp.DoublePointer)2 MatVector (org.bytedeco.opencv.opencv_core.MatVector)2 Scalar (org.bytedeco.opencv.opencv_core.Scalar)2 ArrayList (java.util.ArrayList)1 Pointer (org.bytedeco.javacpp.Pointer)1 ByteIndexer (org.bytedeco.javacpp.indexer.ByteIndexer)1 DoubleIndexer (org.bytedeco.javacpp.indexer.DoubleIndexer)1 FloatIndexer (org.bytedeco.javacpp.indexer.FloatIndexer)1 Indexer (org.bytedeco.javacpp.indexer.Indexer)1 IntIndexer (org.bytedeco.javacpp.indexer.IntIndexer)1 ShortIndexer (org.bytedeco.javacpp.indexer.ShortIndexer)1 UByteIndexer (org.bytedeco.javacpp.indexer.UByteIndexer)1 UShortIndexer (org.bytedeco.javacpp.indexer.UShortIndexer)1 AbstractScalar (org.bytedeco.opencv.opencv_core.AbstractScalar)1 Point2f (org.bytedeco.opencv.opencv_core.Point2f)1