Skip to content

PerceptualHash algorithm produces same results on different images. #16

@ChurikiTenna

Description

@ChurikiTenna

Sometime algorithm: PerceptualHash() produces the exact same results on different images.
Is this expected behavior?

Log:
flutter: 2.png and 1.png
flutter: PerceptualHash: 39.0625%
flutter: 3.png and 1.png
flutter: PerceptualHash: 19.140625%
flutter: 4.png and 1.png
flutter: PerceptualHash: 39.0625% <= same as 2.png
flutter: 5.png and 1.png
flutter: PerceptualHash: 47.265625%

Code:

for (var i = 1; i < 6; i++) {
        ByteData data = await rootBundle.load('lib/images/$i.png');
        Uint8List bytes = data.buffer.asUint8List();
        img.Image image = img.decodeImage(bytes)!;
        if (img1 != null) {
          debugPrint('$i.png and 1.png');
          await imageCompareTest(img1, image);
        }
        img1 = image;
      }

imageCompareTest(img.Image img1, img.Image img2) async {
  Directory tempDir = await getTemporaryDirectory();

  var url1 = '${tempDir.path}/file1.png';
  var url2 = '${tempDir.path}/file2.png';

// it's a bit redundant but crucial for our use-case
  Uint8List bytes1 = Uint8List.fromList(img.encodePng(img1));
  Uint8List bytes2 = Uint8List.fromList(img.encodePng(img2));

  var file1 = File(url1);
  await file1.writeAsBytes(bytes1);
  var file2 = File(url2);
  await file2.writeAsBytes(bytes2);

  var assetResults = await listCompare(
    target: file1,
    list: [
      file2
    ],
    algorithm: PerceptualHash(),
  );

  for (var e in assetResults) {
    print('PerceptualHash: ${e * 100}%');
  }
}

Images used:
1.png
1
2.png
2
3.png
3
4.png
4
5.png
5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions