1

So, I'm using a file sharing app on Android. It creates a duplicate copy which is uploaded to it's server.

PROBLEM The following code works for a duplicate copy I manually create. That is, I long press and copy the file into the same directory with a File Manager. Then my function returns true. When it compares the duplicate image due to the app and the original image, I get false.

MD5-checksums are different so that is out of the options.

CODE

    public boolean equals(Bitmap bitmap1, Bitmap bitmap2) {
        ByteBuffer buffer1 = ByteBuffer.allocate(bitmap1.getHeight()
                * bitmap1.getRowBytes());
        bitmap1.copyPixelsToBuffer(buffer1);

        ByteBuffer buffer2 = ByteBuffer.allocate(bitmap2.getHeight()
                * bitmap2.getRowBytes());
        bitmap2.copyPixelsToBuffer(buffer2);

        return Arrays.equals(buffer1.array(), buffer2.array());
    }

Here are the images :

Original image - original

Duplicate image created by the app - duplicate

My code currently returns false while comparing these two images. How do I get the code to return true?

Community
  • 1
  • 1
Karthik Balakrishnan
  • 4,353
  • 6
  • 37
  • 69
  • Your problem is not very clear please make is more clear. – Ali Imran Jan 05 '13 at 13:49
  • @AliImran Is that better? – Karthik Balakrishnan Jan 05 '13 at 13:51
  • Your this line is confusing `When it compares the duplicate image due to the app and the original image` here what do you mean by saying `due to the app` ? – Ali Imran Jan 05 '13 at 13:53
  • @AliImran The third-party app which I use for file sharing creates a copy of the image and uploads it. That is the duplicate image I'm referring to by "due to the app". – Karthik Balakrishnan Jan 05 '13 at 13:56
  • The problem is related to third party app. did try to debug your code? and check the size of `ByteBuffer` as it must be different from original image and this is because the third party app may be using some compression in the original image. – Ali Imran Jan 05 '13 at 14:15
  • @AliImran Checked everything. I'm guessing the EXIF information is modified. However, I can't use the metadata comparison for finding duplicates since it's not a foolproof way. Some images might lack certain tags. I'm guessing the third party app compresses it in someway. – Karthik Balakrishnan Jan 05 '13 at 14:20
  • @AliImran you are confusing him, stop with that. This is a basic matter, which is due to the use of lossy compression. – mmgp Jan 05 '13 at 14:21
  • @mmgp Yes you are right. – Ali Imran Jan 05 '13 at 14:27

1 Answers1

1

Your problem is due to artefacts created by JPEG compression, if you can always keep the images in PNG then your problem is most likely solved. If you can't do that, then you need a better algorithm to compare the images. This is exactly the same problem discussed at Comparing image in url to image in filesystem in python

For instance, running the algorithms mentioned in the earlier discussion, we get a similarity of more than 99%. With that similarity value, you can say the images are the same.

Community
  • 1
  • 1
mmgp
  • 18,901
  • 3
  • 53
  • 80
  • Say jpegs have widely been used. If I convert the original and duplicate to .png formats and then compare their checksums, will they be the same? Or if I compare them with the code I've shown will it return true? – Karthik Balakrishnan Jan 05 '13 at 13:58
  • No, the checksums will be different. You have to start and end with PNG, if you mix JPEG in the middle then you are using lossy compression. – mmgp Jan 05 '13 at 13:59
  • Note that the metric named as NRMSE in the linked discussion is very easy to implement, you could consider using it. – mmgp Jan 05 '13 at 14:00
  • I'm new to image processing. So, could you link me to the basics so that I understand the contents of the link you've posted? – Karthik Balakrishnan Jan 05 '13 at 14:07
  • @mgmp Couldn't I just compare the pixels in different areas of the picture? – Karthik Balakrishnan Jan 05 '13 at 14:14
  • @Torcellite both methods are effectively comparing the pixels, what else could they be doing ? To point you anywhere, I need to better understand what are your doubts. – mmgp Jan 05 '13 at 14:16
  • @mgmp Could you tell me if there's a way to compare an image with lossy compression? I'm not really able to understand the contents of the link you've posted. Also, if there's a resized version of a .png file, will the code I've used above work? – Karthik Balakrishnan Jan 05 '13 at 14:27
  • 1
    The linked discussion is comparing images, which in this case were lossy compressed. So, yes, there are ways to do it, and you have been pointed to two of them (a complex one, and a simple one). What do you mean by having a resized image ? Do you want to compare resized and non-resized images ? The linked discussion includes a pretty good link related to MSE (https://ece.uwaterloo.ca/~z70wang/publications/SPM09.pdf) which is very close to the thing named as NRMSE on the discussion. – mmgp Jan 05 '13 at 14:31
  • By "resized images", I mean this - Say I have an 80kb .png file, I share it via the app. It creats a duplicate which is around 16kb. Can I compare it by using pixel to pixel comparison? – Karthik Balakrishnan Jan 05 '13 at 14:37
  • 1
    If the images have the same dimensions (width x height) then you can directly use the metrics mentioned earlier. This doesn't complicate the comparison in any way. – mmgp Jan 05 '13 at 14:39