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

- add average color comparison #7

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

Conversation

D4lv1k
Copy link
Collaborator

@D4lv1k D4lv1k commented Nov 19, 2024

This PR addresses the issue with comparing solid color images described here: #6

@D4lv1k D4lv1k self-assigned this Nov 19, 2024
@@ -298,6 +301,8 @@ private function compareHashes(array $hash1, array $hash2, int $precision): floa

$percentage = $similarity->dividedBy($totalBits, $precision, RoundingMode::HALF_UP)
Copy link
Collaborator

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

@@ -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
Copy link
Collaborator

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

@@ -347,4 +352,62 @@ private function processImagePixels(

return $pixels;
}

public function calculateRGBDistance($color1, $color2): float
Copy link
Collaborator

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?

return sqrt(pow($r1 - $r2, 2) + pow($g1 - $g2, 2) + pow($b1 - $b2, 2));
}

public function calculateSimilarity($color1, $color2): float
Copy link
Collaborator

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?

@@ -272,6 +272,24 @@ public function testCompareBlurredImages(): void
$this->assertGreaterThan(65.00, $result);
}

public function testCompare1(): void
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
Copy link
Collaborator

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

@D4lv1k
Copy link
Collaborator Author

D4lv1k commented Dec 5, 2024

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.

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.

2 participants