title: Finding Neighboring Pixels [Python]
body: <p>Please bear with me as this is my first post after a long-time (unregistered) user of the site.</p>
  <p>I have an image file where there are specific regions in the image of interest. These regions have be identified by testing each pixel/cell individually and determining if it is of interest - appending those of interest to a new list.</p>
  <p>So I have a list of the form:</p>
  <pre><code>[[22,25], [22,29], [23,24], ...]
  </code></pre>
  <p>which contains the indices of all my special pixels where on a macro scale if I were to plot these images as white on an otherwise black image; I would have essentially two contiguous white masses. I want to sort this list into another nested list in which they are grouped by continuity. That is all of the pixels that are associate with a white mass will be grouped together in a nested list</p>
  <p>My Thought process was to just grab the first point (fpoint) and then test all other points (pts) with respect to that point by testing the x and y index to see if the difference of the two points is not greater than one for both x and y. so each test would be something like this:</p>
  <pre><code>if ( (np.absolute(fpoint[0] - pts[0]) &lt;= 1) and (np.absolute(fpoint[1] - pts[1]) &lt;= 1) ):
       #add this point to the same nested list as my fpoint
       #remove this point from the initial list and continue to chisel down the original list until it is all sorted into my new list
  </code></pre>
  <p>Seeing as this method in one pass I may get up to 8 neighboring points I will need to keep track of those to test each of them individually as I have removed them from my old list.</p>
  <p>So by the end of this I hope to have something like:</p>
  <pre><code>groupedList = [[[22,23], [21,24], [20,24], ...], [[102, 51], [102, 50], [100, 55]...]]
  </code></pre>
  <p><strong>To reiterate in a 1-dimensional case:</strong></p>
  <p>let's say I have a list:</p>
  <pre><code>[1, 22, 5, 20, 3, 4, 21, 2]
  </code></pre>
  <p>At the end of my function I hope to have a list similar to:</p>
  <pre><code>[ [1, 5, 3, 4, 2], [22, 20, 21] ]
  </code></pre>
  <p>Because as a set, 1, 5, 3, 4, and 2 are 'contiguous' as are 22, 20, and 21.
  I do not care about the order of the numbers as long as they are separated.</p>
  <p>Something about a recursive bit of code sounds like an elegant solution to my problem but I am just not able to produce it.
  Thank you for your time,
  Heath M.</p>
  <hr />
  <h2>SOLUTION</h2>
  <p>Accredited to <a href="https://stackoverflow.com/users/12541034/anders-munch">Anders Munch</a></p>
  <p>Taking what Anders Munch had given me, I was able to come to this point which proved satisfactory!</p>
  <pre><code>def candidate_neighbors(node):
      return ((node[0]-1, node[1]-1), (node[0]-1, node[1]), (node[0]-1, node[1]+1), (node[0], node[1]-1), 
              (node[0], node[1]+1), (node[0]+1, node[1]-1), (node[0]+1, node[1]), (node[0]+1, node[1]+1))
  
  def neighboring_groups(nodes):
      remain = set(nodes)
      while len(remain) &gt; 0:
          visit = [remain.pop()]
          group = []
          while len(visit) &gt; 0:
              node = visit.pop()
              group.append(node)
              for nb in candidate_neighbors(node):
                  if nb in remain:
                      remain.remove(nb)
                      visit.append(nb)
          yield tuple(group)
  
  nodes = ((22, 23), (22, 24), (21, 23), (1, 5), (2, 6), (21, 22), (3, 5))
  
  print(tuple(neighboring_groups(nodes)))
  </code></pre>
  <pre><code>(((1, 5), (2, 6), (3, 5)), ((22, 24), (22, 23), (21, 22), (21, 23)))
  </code></pre>