I have written some code that compares two "BufferedImage" rectangles by doing a pixel by pixel comparison. These BufferedImages are of "playing cards" with rounded corners so I would like to exclude those corner areas during the image comparison process since the varied background could cause the comparison to be "false".

For example, two screenshots of identical cards taken from different locations of the screen will "not match" because the backgrounds are different. If I could exclude the corner area from the comparison, they WOULD match. I could add a bunch of "if" statements like...

if ( (j = 1 && i < 10) ||
 (j = 2 && i < 6) ||
 (j = 3 && i < 5) ||
 (j = 4 && i < 4) ||
 (j = 5 && i < 3) ||
 (j = 6 && i < 2) ||
 (j = 7 && i < 1) ||
 (j = 8 && i < 1) ||
 (j = 9 && i < 1) ) {
i++;
break: skip;
}

...but it seems like there would be a more simple and elegant way of excluding these rounded corner areas from the process.

Here is the code without any "corner exclusion" code...

/****************************************************************
   * CompareTwoImages
   ****************************************************************/
  public static boolean CompareTwoImages(BufferedImage bImage1, BufferedImage bImage2) {
    boolean ret = true;
    int CardWidth = 25;
    int CardHeight = 61;
    int pixel1;   
    int pixel2;
    int tolerance = 0;
        
    try {
      for (int j = 0 ; j < CardHeight; j++) {   // for each card height pixel
        for (int i = 0; i < CardWidth; i++) {     // for each card width pixel          
          pixel1 = bImage1.getRGB(i, j);
          pixel2 = bImage2.getRGB(i, j);
          if ((pixel1 < (pixel2 - tolerance)) || (pixel1 > (pixel2 + tolerance))) {
            ret = false;
          }
        }
      }
    } catch (Exception e) {
      System.err.println("CompareTwoImages >>>> " + e.getMessage());
    }
    return ret;
  }

You could solve this in a generic sort of way by also having a "mask" image - same size as the card images, with each pixel either black or white depending on whether or not you want to compare the corresponding pixels in the two card images.
That way, if the shape or size of the images changes you just need 1 minute in a picture editor for the mask, as opposed to having to change the program or some obscure data file.

You could solve this in a generic sort of way by also having a "mask" image - same size as the card images, with each pixel either black or white depending on whether or not you want to compare the corresponding pixels in the two card images.
That way, if the shape or size of the images changes you just need 1 minute in a picture editor for the mask, as opposed to having to change the program or some obscure data file.

Well, it turns out that the "Alpha Value" of the areas I want to exclude just happens to be equal to 0 (zero), so excluding these areas is a no brainer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.