Last active
June 26, 2020 17:08
-
-
Save andmax/b83d9c81556b5f87fac11649267e022b to your computer and use it in GitHub Desktop.
List all neighbor indices at a certain distance from a given pixel position
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The problem is to find all neighbor indices l_neighbors at a distance d from a given pixel position pp in any dimension | |
# It uses itertools.product to do n-dimensional list cross product | |
import itertools | |
pp, d = [2, 2], 2 # Example pp pixel position and d distance | |
li = [ list(range(ppi-d, ppi+d+1)) for ppi in pp ] # list of each 1-dimensional pp neighbors for each dimension | |
l_neighbors = [ pli for pli in itertools.product(*li) if any([ p in [l[0], l[-1]] for p, l in zip(pli, li) ]) ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment