0

I need to loop over all (unordered) pairs of pixels in an image that are neighbors of each other without repetition. I am using an 8 point neighborhood. For example:

  x,y| 0   1   2   3   4
  ---+---+---+---+---+---+
   0 |   |   |   |   |   |
     +---+---+---+---+---+
   1 | a | b | c | d |   |
     +---+---+---+---+---+
   2 | e | f | g | h |   |
     +---+---+---+---+---+
   3 | i | j | k | l |   |
     +---+---+---+---+---+
   4 |   |   |   |   |   |
     +---+---+---+---+---+

The neighbors of pixel f are in the 3x3 square around it. Thus, g, for example, forms a 2 point clique with f. If I were to loop over all the rows and columns of the image, this clique would be counted twice, once when f is the center pixel and once when g is the center pixel. Similar inefficiencies would occur with the rest of the cliques.

So what I would like to do, is loop over all the cliques, rather than each pixel. If I were familiar with graph theory, I think some of the answers already given to similar questions would suffice, but as I am not, I would really appreciate any help that you can give with an efficient algorithm in layman's terms. Thanks in advance!

Gillespie
  • 915
  • 1
  • 6
  • 6

1 Answers1

1

Loop the first point over all points. Inner loop the second point over the right, lower-left, lower, and lower-right neighbors (if they exist).

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120
  • Thanks, @David! That's a pretty simple and straightforward way to do it. Should have thought of it. I got tangled up trying to understand graph theory and the clique problem in general :). – Gillespie Oct 05 '14 at 02:43