calgorithmimage-processinggraph-theoryclique-problem

Efficient algorithm for looping over all neighbor pairs (2 point cliques) in 2-D array


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!


Solution

  • 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).