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;
}
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;
}
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 };
}
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;
}
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);
}
Aggregations