How check if images are identical?

edited December 2013 in How To...

I'm receiving a Syphon stream (Syphon library for Processing 1.0-RC1) and applying CPU intensive OpenCV (OpenCV for Processing 0.4.2) on it. But the received frames don't change very often. I would like to check, if the new frame is different from the last frame and only do further calculations on it, if it's really different from the last frame.

How can I check if these frames identical in the fastest way?

I tried stuff like that, but it always prints "true" no matter what.

PGraphics syphonInput;
PImage oldFrame;

syphonInput = syphonClient.getGraphics(syphonInput);
syphonInput.loadPixels();

if (oldFrame != null){
    println(Arrays.equals(syphonInput.pixels, oldFrame.pixels));
}
oldFrame = syphonClient.getGraphics(syphonInput);
oldFrame.loadPixels();

Thanks!

Answers

  • Upps, found a mistake:

    PGraphics syphonInput,oldFrame;
    

      syphonInput = syphonClient.getGraphics(syphonInput);
    
      syphonInput.loadPixels();
      if (oldFrame != null){
        println(Arrays.equals(syphonInput.pixels, oldFrame.pixels));
      }
      oldFrame = syphonClient.getGraphics(oldFrame);
      oldFrame.loadPixels();
    

    The comparison works now. But I still don't know, if this is the fastest way. Does anybody know?

  • edited December 2013

    If the library's dev(s) implemented both equals() & hashCode(),
    you can use some of the same approach as a HashMap does,
    like checking whether the object's changed its internal structure since its last state:

    int lastSyphonHash;  // keeps previous calculated hashCode()
    
    // Checks whether current hashCode() is still the same as previous 1:
    if ( lastSyphonHash == (lastSyphonHash = syphonClient.hashCode()) ) {}
    
  • but Arrays.equals() will fail as soon as it finds a non-matching byte. hashing both means accessing each byte of both arrays and lots of maths...

  • edited December 2013

    You're right! For such heavy objects, hashCode() & equals() can be very slow indeed! :o3

  • edited December 2013

    Thanks for your answers!

    Looked into the library and don't think there is some hashing implemented anyways.

    but Arrays.equals() will fail as soon as it finds a non-matching byte.

    Does that mean there might be a situation where this doesn't work? Seems to work now at least.

  • edited December 2013

    That means as soon it reads a byte (pixel) which isn't the same as the corresponding byte reading for the other array, it returns false immediately, skipping the rest of the other byte checks! ;))

Sign In or Register to comment.