Class ImageComparer

java.lang.Object
io.github.jspinak.brobot.analysis.compare.ImageComparer

@Component public class ImageComparer extends Object
Compares images to determine similarity and find matches between patterns.

This class provides functionality to compare StateImage objects and their underlying Pattern objects to determine how similar they are. It implements a hierarchical comparison approach that can handle comparisons between single images, multiple patterns within StateImages, and lists of StateImages.

The comparison strategy involves finding the smaller image within the larger one, treating the larger image as a scene and the smaller as a search target. This approach is particularly useful for GUI automation where UI elements need to be located within screenshots.

See Also:
  • Constructor Details

  • Method Details

    • compare

      public Match compare(List<StateImage> imgs, StateImage img2)
      Compares multiple StateImage objects against a single target StateImage.

      This method iterates through all StateImage objects in the provided list and compares each one with the target image. It returns the comparison result with the highest similarity score.

      Parameters:
      imgs - List of StateImage objects to compare against the target. Must not be null.
      img2 - The target StateImage to compare against. Must not be null.
      Returns:
      The Match object with the highest similarity score, or a NoMatch if no valid comparisons could be made.
    • compare

      public Match compare(StateImage img1, StateImage img2)
      Compares two StateImage objects by examining all their Pattern combinations.

      This method performs a comprehensive comparison by testing all Pattern objects from img1 against all Pattern objects from img2. The larger image automatically becomes the scene, while the smaller becomes the search target. The returned Match contains details about img2 and its similarity score relative to img1.

      This approach handles StateImages with multiple Pattern variations (e.g., different scales or visual states of the same UI element).

      Parameters:
      img1 - The base StateImage for comparison. Must not be null.
      img2 - The StateImage being compared against img1. Must not be null.
      Returns:
      A Match object containing img2's details and the best similarity score found across all Pattern combinations, or NoMatch if comparison fails.
    • compare

      public Match compare(Pattern p1, Pattern p2)
      Compares two Pattern objects to find the best match of one within the other.

      This is the core comparison method that implements the actual matching logic. It automatically determines which Pattern is larger and treats it as the scene, while the smaller Pattern becomes the search target. The method uses the configured matcher (mock or live) to find all occurrences of the smaller pattern within the larger one.

      The comparison is currently optimized for simplicity, returning the best scoring match when multiple instances are found. Future enhancements may include ActionConfig to customize comparison strategies (e.g., weighting multiple matches vs. single high-quality matches).

      Note: This method handles null inputs gracefully by returning a NoMatch.

      Parameters:
      p1 - The first Pattern to compare. May be null.
      p2 - The second Pattern to compare. May be null.
      Returns:
      A Match object where the smaller Pattern is the searchImage and the larger Pattern is the Scene. Returns NoMatch if either pattern is null, has null BufferedImage, or if size comparison fails.