Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This PR reduces the execution times of an image comparison #245

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mkuehne707
Copy link

PR Details

When comparing two images with multiple different raw regions (regionCount), the matrix (array of length image.getHeight() x image.getWitdth()) was being iterated regionCount - 2 times.
I had a case where there where over 900 iterations over a not-so-small image, which made it quite slow.
With this PR, the image is only being checked once to createRectangles.

Description

I had two screenshots with text where the text size was slightly different. This resulted in a large amount of different pixels, which often were not next to each other.
The time for a comparison of those images was at around 20s.
With the first commit 4ebdad4 i could bring this down to around 3-4s, with the second commit it needs about another second less time.

Related Issue

Might be related to #174 or #153

Motivation and Context

This PR reduces the execution times of an image comparison.

How Has This Been Tested

The unit tests are running without errors, no changes were needed.

To reproduce this scenario, the following test can be used, I did not add this to the tests as of now, because the times will be highly dependent on the hardware and I don't see any other way to test this. Please let me know if I should add this to the tests.

For me, the comparison before my changes took 62s, after the first commit 3.8s and after the second commit 2.7s. 1741 regions are being found.

    @DisplayName("Test speed for large images with many regions")
    @Test
    public void speedTest() {
        //given
        int scaledWidth = 2000;
        int scaledHeight = 6000;
        BufferedImage expected = readImageFromResources("expected#98.png");
        BufferedImage actual = readImageFromResources("actual#98.png");
        expected = ImageComparisonUtil.resize(expected, scaledWidth, scaledHeight);
        // make it a little bit too large (zoom)
        actual =  ImageComparisonUtil.resize(actual, scaledWidth + 100, scaledHeight + 300);
        // then crop it to the right size
        actual = actual.getSubimage(0, 0, scaledWidth, scaledHeight);

        List<Rectangle> excludedAreas = asList(
                new Rectangle(410, 514, 900, 565),
                new Rectangle(410, 636, 900, 754)
        );

        ImageComparison imageComparison = new ImageComparison(expected, actual)
                .setExcludedAreas(excludedAreas)
                .setRectangleLineWidth(5)
                .setPixelToleranceLevel(0.0)
                .setDrawExcludedRectangles(true)
                .setDifferenceRectangleFilling(true, 30.0);

        //when
        long start = System.currentTimeMillis();
        ImageComparisonResult imageComparisonResult = imageComparison.compareImages();
        long end = System.currentTimeMillis();

        //then
        System.out.printf("Comparing images took %sms%n", end - start);
        assertTrue(10000 > end - start, "Image comparison took longer than 10s");
        assertEquals(MISMATCH, imageComparisonResult.getImageComparisonState());
    }

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes. // Please see How Has This Been Tested
  • All new and existing tests passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant