Search in sources :

Example 6 with Mat

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

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

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

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

the class OpenCvUtils method drawOnImage.

public static Mat drawOnImage(Mat image, Rect overlay, Scalar color) {
    Mat dest = image.clone();
    rectangle(dest, overlay, color);
    return dest;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat)

Example 10 with Mat

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

the class TesseractRunner method testTess.

@Test
public void testTess() {
    // File source = new File("src/test/java/some-text.png");
    RobotBase robot = ChromeJavaRunner.getRobot();
    Element window = robot.window("Safari");
    // window = robot.window("Preview");
    robot.delay(1000);
    BufferedImage bi = window.getRegion().captureGreyScale();
    Mat mat = OpenCvUtils.toMat(bi);
    Tesseract tess = new Tesseract(new File("tessdata"), "eng");
    tess.process(mat, false);
    String text = tess.getAllText();
    logger.debug("all text: {}", text);
    tess.highlightWords(robot, robot.screen, 20000);
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Aggregations

Mat (org.bytedeco.opencv.opencv_core.Mat)76 Point (org.bytedeco.opencv.opencv_core.Point)23 ArrayList (java.util.ArrayList)15 MatVector (org.bytedeco.opencv.opencv_core.MatVector)15 PointerScope (org.bytedeco.javacpp.PointerScope)14 FloatIndexer (org.bytedeco.javacpp.indexer.FloatIndexer)14 BufferedImage (java.awt.image.BufferedImage)12 IntIndexer (org.bytedeco.javacpp.indexer.IntIndexer)10 Scalar (org.bytedeco.opencv.opencv_core.Scalar)10 Test (org.junit.jupiter.api.Test)10 DoubleIndexer (org.bytedeco.javacpp.indexer.DoubleIndexer)9 Size (org.bytedeco.opencv.opencv_core.Size)9 UByteIndexer (org.bytedeco.javacpp.indexer.UByteIndexer)8 List (java.util.List)7 org.bytedeco.opencv.global.opencv_core (org.bytedeco.opencv.global.opencv_core)7 WritableRaster (java.awt.image.WritableRaster)6 IOException (java.io.IOException)6 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 Collectors (java.util.stream.Collectors)6