-
Notifications
You must be signed in to change notification settings - Fork 6
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
- add average color comparison #7
base: master
Are you sure you want to change the base?
Conversation
src/ImageComparator.php
Outdated
@@ -298,6 +301,8 @@ private function compareHashes(array $hash1, array $hash2, int $precision): floa | |||
|
|||
$percentage = $similarity->dividedBy($totalBits, $precision, RoundingMode::HALF_UP) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@D4lv1k I'd suggest renaming this variable to $hashSimilarity
src/ImageComparator.php
Outdated
@@ -281,7 +284,7 @@ private function normalizeAsResource(string|GdImage $image): GdImage | |||
* @throws MathException | |||
* @throws NumberFormatException | |||
*/ | |||
private function compareHashes(array $hash1, array $hash2, int $precision): float | |||
private function compareHashes(array $hash1, array $hash2, int $precision, float $similarColor): float |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@D4lv1k let's rename a new param to $colorSimilarity
src/ImageComparator.php
Outdated
@@ -347,4 +352,62 @@ private function processImagePixels( | |||
|
|||
return $pixels; | |||
} | |||
|
|||
public function calculateRGBDistance($color1, $color2): float |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@D4lv1k let's add type-hints for function parameters. Also, shouldn't this function have private
visibility?
src/ImageComparator.php
Outdated
return sqrt(pow($r1 - $r2, 2) + pow($g1 - $g2, 2) + pow($b1 - $b2, 2)); | ||
} | ||
|
||
public function calculateSimilarity($color1, $color2): float |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@D4lv1k let's add type-hints for function parameters. Also, shouldn't this function have private
visibility?
tests/Unit/ImageComparatorTest.php
Outdated
@@ -272,6 +272,24 @@ public function testCompareBlurredImages(): void | |||
$this->assertGreaterThan(65.00, $result); | |||
} | |||
|
|||
public function testCompare1(): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function testCompare1(): void | |
public function testCompareSolildColors($image1, $image2): void |
You can also use dataProviders and pass needed colors as function parameters. You can see DataProviders examples in other tests
@@ -49,8 +49,11 @@ public function compare( | |||
): float { | |||
$hash1 = $this->hashImage($sourceImage); // this one should never be rotated | |||
$hash2 = $this->hashImage($comparedImage, $rotation); | |||
$color1 = $this->getAverageRGB($sourceImage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@D4lv1k I'd suggest calculating color similarity to a private function
Changed the composer.lock file to update dependencies. Developed methods to calculate the distance between colors and compute the average RGB color value of an image based on Euclidean distance. This allowed for considering color similarity when comparing images, thus providing a more accurate similarity assessment. Additionally, color similarity was incorporated into the comparison of image hashes, helping to adjust the comparison result. Tests were added to verify the correctness of the image comparison method, and existing tests were fixed. These changes improved the accuracy of image comparison, preventing unjustified 100% matches between similar but not identical images. |
This PR addresses the issue with comparing solid color images described here: #6