Example usage for org.openqa.selenium Rectangle Rectangle

List of usage examples for org.openqa.selenium Rectangle Rectangle

Introduction

In this page you can find the example usage for org.openqa.selenium Rectangle Rectangle.

Prototype

public Rectangle(int x, int y, int height, int width) 

Source Link

Usage

From source file:com.seleniumtests.ut.util.imaging.TestImageDetector.java

License:Apache License

@Test(groups = { "ut" })
public void searchPicturesByTemplate3() throws IOException {
    ImageDetector detector = new ImageDetector(createFileFromResource("tu/images/infolidays.png"),
            createFileFromResource("tu/images/bouton_enregistrer3.png"), 0.06);
    detector.detectExactZoneWithScale();
    Assert.assertEquals(detector.getDetectedRectangle(), new Rectangle(677, 1595, 220, 155));
    Assert.assertEquals(detector.getSizeRatio(), 1.2, 0.05);
}

From source file:com.seleniumtests.ut.util.imaging.TestImageDetector.java

License:Apache License

@Test(groups = { "ut" })
public void searchPicturesByTemplate4() throws IOException {
    ImageDetector detector = new ImageDetector(createFileFromResource("tu/images/RIB.png"),
            createFileFromResource("tu/images/creditMutuelLogo.png"), 0.06);
    detector.detectExactZoneWithScale();
    Assert.assertEquals(detector.getDetectedRectangle(), new Rectangle(604, 147, 77, 493));
    Assert.assertEquals(detector.getSizeRatio(), 1.0, 0.05);
}

From source file:com.seleniumtests.ut.util.imaging.TestImageDetector.java

License:Apache License

/**
 * Search an image inside an other one, no rotation, no resizing
 * @throws IOException /*from w ww . ja v a  2 s .co  m*/
 */
@Test(groups = { "ut" }, enabled = false)
public void searchPictureWithoutRotation() throws IOException {
    ImageDetector detector = new ImageDetector(createFileFromResource("tu/images/RIB.png"),
            createFileFromResource("tu/images/creditMutuelLogo.png"));
    detector.detectCorrespondingZone();
    Assert.assertEquals(detector.getRotationAngle(), 0);
    Assert.assertEquals(detector.getDetectedRectangle(), new Rectangle(604, 147, 76, 492));
}

From source file:com.seleniumtests.ut.util.imaging.TestImageDetector.java

License:Apache License

/**
 * Search an image inside an other one, no rotation, no resizing
 * @throws IOException //w w  w . j  a v a2  s  . com
 */
@Test(groups = { "ut" }, enabled = false)
public void searchPictureWithoutRotationDriverPage() throws IOException {
    ImageDetector detector = new ImageDetector(createFileFromResource("tu/images/driverTestPage.png"),
            createFileFromResource("tu/images/logo_text_field.png"));
    detector.detectCorrespondingZone();
    Assert.assertEquals(detector.getRotationAngle(), 0);
    Assert.assertEquals(detector.getDetectedRectangle(), new Rectangle(3, 714, 94, 138));
}

From source file:com.seleniumtests.ut.util.imaging.TestImageDetector.java

License:Apache License

/**
 * Search an image inside an other one, 90 rotation, no resizing
 * @throws IOException /*  w w w  .  jav  a  2 s  . com*/
 */
@Test(groups = { "ut" }, enabled = false)
public void searchPictureWith90degRotation() throws IOException {
    ImageDetector detector = new ImageDetector(createFileFromResource("tu/images/p9.png"),
            createFileFromResource("tu/images/vosAlertesRotate90.png"));
    detector.detectCorrespondingZone();
    Assert.assertEquals(detector.getRotationAngle(), 90);
    Assert.assertEquals(detector.getDetectedRectangle(), new Rectangle(574, 136, 29, 107));
}

From source file:com.seleniumtests.ut.util.imaging.TestImageDetector.java

License:Apache License

/**
 * Search an image inside an other one, 180 rotation, no resizing
 * @throws IOException //from  w  ww.j av  a2s  . co  m
 */
@Test(groups = { "ut" }, enabled = false)
public void searchPictureWith180degRotation() throws IOException {
    ImageDetector detector = new ImageDetector(createFileFromResource("tu/images/p9.png"),
            createFileFromResource("tu/images/vosAlertesRotate180.png"));
    detector.detectCorrespondingZone();
    Assert.assertEquals(detector.getRotationAngle(), 180);
    Assert.assertEquals(detector.getDetectedRectangle(), new Rectangle(574, 135, 29, 107));
}

From source file:com.seleniumtests.ut.util.imaging.TestImageDetector.java

License:Apache License

/**
 * Search an image inside an other one, 180 rotation, no resizing
 * @throws IOException //from  www.j a  va  2 s .c o m
 */
@Test(groups = { "ut" }, enabled = false)
public void searchPictureWith270degRotation() throws IOException {
    ImageDetector detector = new ImageDetector(createFileFromResource("tu/images/p9.png"),
            createFileFromResource("tu/images/vosAlertesRotate270.png"));
    detector.detectCorrespondingZone();
    Assert.assertEquals(detector.getRotationAngle(), 270);
    Assert.assertEquals(detector.getDetectedRectangle(), new Rectangle(575, 135, 29, 107));
}

From source file:com.seleniumtests.ut.util.imaging.TestImageDetector.java

License:Apache License

/**
 * Search an image inside an other one, 0 rotation, 80% resizing
 * @throws IOException /*  w  w w.j ava 2s . com*/
 */
@Test(groups = { "ut" }, enabled = false)
public void searchPictureWithResizing() throws IOException {
    ImageDetector detector = new ImageDetector(createFileFromResource("tu/images/RIB.png"),
            createFileFromResource("tu/images/creditMutuelLogo0.8.png"));
    detector.detectCorrespondingZone();
    Assert.assertEquals(detector.getRotationAngle(), 0);
    Assert.assertEquals(detector.getDetectedRectangle(), new Rectangle(603, 147, 77, 493));
}

From source file:com.seleniumtests.util.imaging.ImageDetector.java

License:Apache License

/**
 * Detect the object inside the scene//from ww w .j  a va  2  s. co m
 * We also search the scale of the scene from 20% to 120% scale by steps
 * steps are 10%, with 0.6 accuracy
 * then when a good match is found, we search around by 5% scale steps with 0.7 accuracy
 * then when a good match is found, we search around by 2.5% scale steps with 0.8 accuracy
 * 
 * example:
 * first pass: scales are: 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200
 *             good matches are found around 600 and 700
 * second pass: scales are 550, 600, 650, 700, 750
 *             good matches are found at 650
 * third pass: scales are 625, 650, 675
 * 
 * The best match is at 675
 */
public void detectExactZoneWithScale() {

    Mat sceneImageMat = Imgcodecs.imread(sceneImage.getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
    Mat objectImageMat = Imgcodecs.imread(objectImage.getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);

    List<TemplateMatchProperties> matches = Collections.synchronizedList(new ArrayList<>());

    Map<Integer, Double> scaleSteps = new LinkedHashMap<>();
    scaleSteps.put(100, 0.6);
    scaleSteps.put(50, 0.7);
    scaleSteps.put(25, 0.8);

    int currentStep = 100;

    Set<Integer> computedScales = new HashSet<>();

    while (currentStep >= 25) {
        final double currentThreshold = scaleSteps.get(currentStep);

        // first loop
        Set<Integer> localScales = Collections.synchronizedSet(new HashSet<>());
        if (currentStep == 100) {
            for (int scale = 200; scale < 1200; scale += currentStep) {
                localScales.add(scale);
            }
        } else {
            if (matches.isEmpty()) {
                throw new ImageSearchException("no matches");
            }
            for (TemplateMatchProperties tmpM : matches) {
                if (tmpM.isActive()) {
                    localScales.add(tmpM.getMatchScale() - currentStep);
                    localScales.add(tmpM.getMatchScale() + currentStep);
                }
            }
        }

        ExecutorService executorService = Executors
                .newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        for (int scale : localScales) {
            if (computedScales.contains(scale)) {
                continue;
            }
            computedScales.add(scale);

            // resize to scale factor
            final int localScale = scale;
            Size sz = new Size(sceneImageMat.cols() * scale / 1000.0,
                    sceneImageMat.rows() * localScale / 1000.0);

            // skip if resized image is smaller than object
            if (sz.width < objectImageMat.cols() || sz.height < objectImageMat.rows()) {
                continue;
            }

            executorService.submit(() -> {

                Mat resizeSceneImageMat = new Mat();
                Imgproc.resize(sceneImageMat, resizeSceneImageMat, sz);

                try {
                    TemplateMatchProperties match = detectExactZone2(resizeSceneImageMat, objectImageMat,
                            localScale, currentThreshold);
                    matches.add(match);
                } catch (ImageSearchException e) {
                }

            });
        }

        executorService.shutdown();
        try {
            executorService.awaitTermination(10, TimeUnit.SECONDS);
        } catch (Exception e) {
            logger.info("Could not compute scale within 10 seconds", e);
        }

        // shortcut if we find a very good match
        double cleanThreshold = currentThreshold;
        matches.sort((TemplateMatchProperties t1,
                TemplateMatchProperties t2) -> -(t1.getMatchValue().compareTo(t2.getMatchValue())));
        if (!matches.isEmpty() && matches.get(0).getMatchValue() > 0.9) {
            cleanThreshold = 0.9;
            currentStep = Math.min(currentStep, 50);
        }
        currentStep = currentStep / 2;

        // clean matches from too low matching values
        for (TemplateMatchProperties t : matches) {
            if (t.getMatchValue() < cleanThreshold) {
                t.setActive(false);
            }
        }
    }

    // get the best match
    matches.sort((TemplateMatchProperties t1,
            TemplateMatchProperties t2) -> -(t1.getMatchValue().compareTo(t2.getMatchValue())));

    if (!matches.isEmpty()) {
        TemplateMatchProperties bestMatch = matches.get(0);
        if (bestMatch.getMatchValue() < 1 - detectionThreshold) {
            throw new ImageSearchException(
                    String.format("No match found for threshold %.2f, match found with value %.2f",
                            1 - detectionThreshold, bestMatch.getMatchValue()));
        }

        detectedRectangle = new Rectangle((int) (bestMatch.getMatchLoc().x / bestMatch.getDoubleScale()),
                (int) (bestMatch.getMatchLoc().y / bestMatch.getDoubleScale()),
                (int) (objectImageMat.rows() / bestMatch.getDoubleScale()),
                (int) (objectImageMat.cols() / bestMatch.getDoubleScale()));

        if (debug) {
            try {
                Imgproc.rectangle(sceneImageMat, new Point(detectedRectangle.x, detectedRectangle.y),
                        new Point(detectedRectangle.x + detectedRectangle.width,
                                detectedRectangle.y + detectedRectangle.height),
                        new Scalar(0, 255, 0));

                showResultingPicture(sceneImageMat);
            } catch (IOException e) {
            }
        }
        rotationAngle = 0;
        sizeRatio = detectedRectangle.width / (double) objectImageMat.cols();

    } else {
        throw new ImageSearchException("no matching has been found");
    }

}

From source file:com.seleniumtests.util.imaging.ImageDetector.java

License:Apache License

/**
 * Record detected zone as a rectangle//from   w  w  w  .  ja  va 2 s  .co  m
 * Take into account the rotating angle so that resulting rectangle correspond to points (origin point depends on rotation)
 * @param p1   corner corresponding to top left corner of origin possibly rotated
 * @param p2   corner corresponding to top right corner of origin possibly rotated
 * @param p3   corner corresponding to bottom right corner of origin possibly rotated
 * @param p4   corner corresponding to bottom left corner of origin possibly rotated
 */
protected void recordDetectedRectangle(Point p1, Point p2, Point p3, Point p4) {
    switch ((int) rotationAngle) {
    case 0:
        detectedRectangle = new Rectangle((int) p1.x, (int) p1.y, (int) Math.abs(p4.y - p1.y),
                (int) Math.abs(p2.x - p1.x));
        break;
    case 90:
        detectedRectangle = new Rectangle((int) p4.x, (int) p4.y, (int) Math.abs(p3.y - p4.y),
                (int) Math.abs(p1.x - p4.x));
        break;
    case 180:
        detectedRectangle = new Rectangle((int) p3.x, (int) p3.y, (int) Math.abs(p2.y - p3.y),
                (int) Math.abs(p4.x - p3.x));
        break;
    case 270:
        detectedRectangle = new Rectangle((int) p2.x, (int) p2.y, (int) Math.abs(p1.y - p2.y),
                (int) Math.abs(p3.x - p2.x));
        break;
    default:
        break;
    }
}