romankh3 / image-comparison

Published on Maven Central Java Library that compares 2 images with the same sizes and shows the differences visually by drawing rectangles. Some parts of the image can be excluded from the comparison. Can be used for automation QA tests.
https://t.me/romankh3
Apache License 2.0
343 stars 105 forks source link

[QUESTION] Difference Percentage Calculations Don't Match #233

Open PeerHalvorsen opened 2 years ago

PeerHalvorsen commented 2 years ago

I'm trying to understand why getDifferencePercent(BufferedImage img1, BufferedImage img2) on line 138 of the ImageComparisonUtil class is returning a different result than the percent calculated in line 313 of the ImageComparison class.

The algorithm used to acquire the count of different pixels in populateTheMatrixOfTheDifferences() on line 234 seems to be similar to what's being done in getDifferencePercent(BufferedImage img1, BufferedImage img2) but the end result isn't the same.

It seems like if I'm trying to set an allowed percentage difference using setAllowingPercentOfDifferentPixels() I can't use the output of getDifferencePercent as a gauge since that value doesn't match what's calculated during the image comparison inside isAllowedPercentOfDifferentPixels(long countOfDifferentPixels)

nikmazur commented 2 years ago

I ran into the same issue when writing my own project: getDifferencePercent was returning an incorrect percentage. It seems that it was calculating the total number of different pixels incorrectly - instead of increasing an overall counter for each different pixel it was summing a long of differences from getRGB method.

Created a new pull request to fix this.