2

I am programming a small application for android and I need to compare between two pictures. The pictures are represented as two byte array.

Is there an algorithm to compare between them? I need to check the changes which has been made between them.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Shiran Maor
  • 39
  • 2
  • 3
  • 2
    Do they need to be exactly equal or is there some kind of closeness threshold you are shooting for? If they need to be equal you can use `Arrays.equals()`: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#equals(byte[], byte[]) – Hunter McMillen Aug 04 '12 at 14:34
  • they dont have to be exactly equal. i am programming a motion detector, so i want to compare frames each x time to check if any changes has been made. thanks – Shiran Maor Aug 04 '12 at 14:38
  • Assuming the byte arrays contain only image data (without file headers, which may be different between two seemingly identical images), a simple algorithm to find the amount of different byes would be just to go over the arrays byte by byte and compare, summing the differences... – EyalAr Aug 04 '12 at 14:58
  • Looking at the various comments, it appears that what is being asked for is an algorithm that will compare two images and provide a description of the differences between the two. The root question seems to be how to describe the differences between two images. So it is not just a question of determining which pixels are different but rather providing some kind of measure as to how the images differ. Does there need to be image recognition or would providing a measurement of difference be sufficient? – Richard Chambers Aug 04 '12 at 16:31
  • Actually this question has been asked in stackoverflow before. take a look at the Related sidebar. For instance, http://stackoverflow.com/questions/189943/how-can-i-quantify-difference-between-two-images – Richard Chambers Aug 04 '12 at 16:34

1 Answers1

2

It depends on whether you just want to flag that they are different, or record each of the differences between them.

To merely check that they are different (as Hunter McMillen pointed out) - http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html:

boolean different = Arrays.equals(byteArray1, byteArray2);

To store the individual differences (assuming your pictures are of the same size):

byte[] differenceArray = new byte[byteArray1.length];
for (int i = 0; i < byteArray1.length; i++) {
    differenceArray[i] = (byte) (byteArray1[i] - byteArray2[i]);
}

EDIT:

If you want two dimensions (eg 800x600) to your byte array, you could do:

byte[][] differenceArray = new byte[byteArray1.length][byteArray1[0].length];
for (int x = 0; x < byteArray1.length; x++) {
    for (int y = 0; y < byteArray1[0].length; y++) {
        differenceArray[x][y] = (byte) (byteArray1[x][y] - byteArray2[x][y]);
    }
}

The difference array would be all 0 if there are no differences between the two images, otherwise the numbers would be the differences between the bytes.

Dazed
  • 1,069
  • 9
  • 34
Quetzalcoatl
  • 3,037
  • 2
  • 18
  • 27
  • I think you didn't understand what i asked. let just say my pictures are 800*600 pixels. I want to convert my byte array to array[800][600] and than compare between them sorry if I wasn't clear enough – Shiran Maor Aug 04 '12 at 15:04
  • What about these solutions isn't what you were looking for? The first bit of code shows how to tell whether the two arrays are an exact match, an the second bit of code shows how to store the differences between two images, each cell of the array being non-zero if and only if the represented pixel in the two images changed. – G. Bach Aug 04 '12 at 15:08
  • If you want additional dimensions then you could wrap a second for-loop around them. Array.equals() is likely better than my first solution anyhow. – Quetzalcoatl Aug 04 '12 at 15:34
  • The difference array will be all zeros if the two images are the same, otherwise the numbers will represent the numerical distances between the bytes. – Quetzalcoatl Aug 04 '12 at 22:47