iase2off - Structuring element to neighbor offsets

Synopse

Create an array of offsets of the neighborhood specified by a structuring element.

  • off = iase2off(Bc,option='neigh')
    • Output
      • off: ndarray, 2D, integer, offsets of the neighbors. Rows are the number of neighbors, columns are the offsets related to each dimension.
    • Input
      • Bc: ndarray, 2D, structuring element to specify the neighbors of a pixel
      • option: string: neigh - all pixels but the center, fw - forward neighbors, bw - backward neighbors.

Description

Creates an array of offsets for the neighbors of a pixel. The structuring element must be binary (flat) and . Each row of the output array represents a neighbor offsets. Each column gives the offsets in each dimension. The function is useful for algorithms that need to scan the neighbors of a pixel.

Function Code

01. import numpy as np
02. def iase2off(Bc,option='neigh'):
03.     '''Converts structuring element to list of neighbor offsets in graph image'''
04.     h,w = Bc.shape
05.     hc,wc = h/2,w/2
06.     B = Bc.copy()
07.     B[hc,wc] = 0  # remove origin
08.     off = np.transpose(B.nonzero()) - np.array([hc,wc])
09.     if option == 'neigh':
10.         return off  # 2 columns x n. of neighbors rows
11.     elif option == 'fw':
12.         i = off[:,0] * w + off[:,1]
13.         return off[i>0,:]  # only neighbors higher than origin in raster order
14.     elif option == 'bw':
15.         i = off[:,0] * w + off[:,1]
16.         return off[i<0,:]  # only neighbors less than origin in raster order
17.     else:
18.         assert 0,'options are neigh, fw or bw. It was %s'% option
19.         return None

Examples

4-neighborhood

01. import ia870 as mm
02. B4 = mm.iasecross(1)
03. off4 = mm.iase2off(B4)
04. print 'off4=\n', off4
05. off4fw = mm.iase2off(B4,'fw')
06. print 'off4fw=\n', off4fw
07. off4bw = mm.iase2off(B4,'bw')
08. print 'off4bw=\n', off4bw
09. 
10. B = zeros_like(mm.iaseshow(B4))
11. for r,c in off4 + array([[1,1]]):
12.     B[r,c] = True
13. adshow(mm.iaseshow(B,'EXPAND'),'4-neigh')
14. 
15. B = zeros_like(mm.iaseshow(B4))
16. for r,c in off4fw + array([[1,1]]):
17.     B[r,c] = True
18. adshow(mm.iaseshow(B,'EXPAND'),'4-neigh forward')
19. 
20. B = zeros_like(mm.iaseshow(B4))
21. for r,c in off4bw + array([[1,1]]):
22.     B[r,c] = True
23. adshow(mm.iaseshow(B,'EXPAND'),'4-neigh backward')
off4=
[[-1  0]
 [ 0 -1]
 [ 0  1]
 [ 1  0]]
off4fw=
[[0 1]
 [1 0]]
off4bw=
[[-1  0]
 [ 0 -1]]

4-neigh

4-neigh forward

4-neigh backward

8-neighborhood

1. import ia870 as mm
2. B8 = mm.iasebox(1)
3. off8 = mm.iase2off(B8)
4. print 'off8=\n', off8
5. off8p = mm.iase2off(B8,'fw')
6. print 'off8p=\n', off8p
7. off8b = mm.iase2off(B8,'bw')
8. print 'off8b=\n', off8b
off8=
[[-1 -1]
 [-1  0]
 [-1  1]
 [ 0 -1]
 [ 0  1]
 [ 1 -1]
 [ 1  0]
 [ 1  1]]
off8p=
[[ 0  1]
 [ 1 -1]
 [ 1  0]
 [ 1  1]]
off8b=
[[-1 -1]
 [-1  0]
 [-1  1]
 [ 0 -1]]

See Also

Contributions

  • Roberto Lotufo, 25mai2014: initial function.