1

The following image shows what the algorithm has to do.

enter image description here

In the case of 1D arrays, for example string matching, we can use String searching algorithms.

How can we efficiently find such matchings in bitmaps or 2d arrays?

P.S. The given example is only for explanation purpose. Implementing Bejeweled solver does not need complex algorithms, just split the game board and make N x N table, and sample a pixel from each cell and figure out which item is in each cell by checking its RGB value, then we're done. I want to know general image region matching algorithms that will be used in, for example, a macro program that keeps clicking on the matching bitmap regions.

Heejin
  • 4,145
  • 3
  • 23
  • 28
  • 1
    Actually Bejeweled isn't implemented like that. It's board matrix contains a definition of its colors on it's structure. The UI is presented graphical but the underlying logic, not. So solving it just mean traverse the matrix dynamically. – Erre Efe Sep 22 '12 at 14:42
  • How complex are the problems that the algorithm needs to solve? How 'bout blob detection and good 'ol neural net? – Helix Quar Sep 22 '12 at 14:44
  • What have you found on this matter? There are several algorithms listed in wikipedia in the tag [Image processing](http://en.wikipedia.org/wiki/List_of_algorithms#Image_processing). There is a similar question in SO: [Image comparison - fast algorithm](http://stackoverflow.com/questions/843972/image-comparison-fast-algorithm). – Luiggi Mendoza Sep 22 '12 at 14:50
  • Another interesting question in this matter: [Algorithm improvement for Coca-Cola can shape recognition](http://stackoverflow.com/questions/10168686/algorithm-improvement-for-coca-cola-can-shape-recognition). – Luiggi Mendoza Sep 22 '12 at 14:52
  • If you're looking for an algorithm, you don't have to put different language tags into your question. – Makoto Sep 22 '12 at 14:59
  • @Makoto Sorry for that. I removed those tags. – Heejin Sep 22 '12 at 15:01
  • @RandolfR-F The problem is that I don't have to way to access the underlying logic. The problem that I showed is building a Bejeweled solver without having an access to the code or process memory. – Heejin Sep 22 '12 at 15:09
  • @Heejin umm, ok, then of course it should work the way you're trying. Well, taking gradients into play. – Erre Efe Sep 22 '12 at 15:10

1 Answers1

2

It is not a simple thing to do. Most games work around the problem by using regions (as you surmised with your bejeweld example) or color maps, which maintain a second drawing, mapping position by color to a particular item.

Outside of games, routines use matrix transformation to attempt to identify edges and verticies, which tend to reduce the amount of data under consideration for possible matching. The trivial example would be to use a filter with a kernel like

 kernel = [ -1 -1 -1 ]
          [ -1  8 -1 ]
          [ -1 -1 -1 ]

To emphasize any region which doesn't balance with its neighbours. From that you can attempt to detect lines and vertices, greatly reducing the number of items to consider in a match. If you want to detect "near" matches, then you attempt to use a linear transformation to describe the distance to a match by measuring displacement of the vertices, and set up some criteria for deciding if the match is too far from being the same.

A trivial solution, but one that only works with "perfect" data is to just xor the bitmap against the original for every possible offset. If the image is known to be constructed with the exact bitmap, then the xor should result in a zero field the same size as the bitmap. This technique can be somewhat improved in performance by checking a few chosen pixels for exact match before attempting the more expensive xor and verify calculations, but its performance will degrade with larger spaces to consider in a very undesirable manner.

Edwin Buck
  • 64,804
  • 7
  • 90
  • 127