iaNlut

Synopse

Create an array of raster offsets of the neighborhood of all pixels of an image.

  • Nlut = iaNlut(s,offset)
    • Output
      • Nlut: ndarray, 2D, integer, offsets of the neighbors. One row for each pixel, addressed in linear raster offset. Columns are the 1D offsets of its neighbors. If the neighbor is outside imagem domain, the value n is set, where n is the number of pixels in the image.
    • Input
      • s: tuple, shape of the image
      • offset: ndarray: offsets of a pixel.

Description

Precomputes an array of 1D raster offsets for the neighbors of every pixel in the image which has a shape s. The number of rows is equal the number of pixels in the image and the number of columns is the number of neighbors of every pixel. If the neighbor is output the imagem domain, its offset is set to the number of pixels in the image. This make it convenient to avoid testing this condition in a loop, by concatenating a single pixel in the raster of the image to be scanned.

Function Code

01. import numpy as np
02. def iaNlut(s,offset):
03.     '''Precompute array of neighbors. Optimized by broadcast.
04. 
05.     s - image shape
06.     offset - offset matrix, 2 columns (dh,dw) by n. of neighbors rows
07.     '''
08.     H,W = s
09.     n = H*W
10.     hi = np.arange(H).reshape(-1,1)
11.     wi = np.arange(W).reshape(1,-1)
12.     hoff = offset[:,0]
13.     woff = offset[:,1]
14.     h = hi + hoff.reshape(-1,1,1)
15.     w = wi + woff.reshape(-1,1,1)
16.     h[(h<0) | (h>=H)] = n
17.     w[(w<0) | (w>=W)] = n
18.     Nlut = np.clip(h * W + w,0,n)
19.     return Nlut.reshape(offset.shape[0],-1).transpose()

Examples

4-neighborhood offsets of an image with 3 rows and 4 columns

1. import ia870 as mm
2. 
3. B4 = mm.iasecross(1)
4. off4 = mm.iase2off(B4)
5. f = zeros((3,4),uint8)
6. Nlut4 = mm.iaNlut(f.shape,off4)
7. print 'Nlut4=\n', Nlut4
Nlut4=
[[12 12  1  4]
 [12  0  2  5]
 [12  1  3  6]
 [12  2 12  7]
 [ 0 12  5  8]
 [ 1  4  6  9]
 [ 2  5  7 10]
 [ 3  6 12 11]
 [ 4 12  9 12]
 [ 5  8 10 12]
 [ 6  9 11 12]
 [ 7 10 12 12]]

See Also

  • ia870:iase2off - Computes offsets of a neighborhood specified by an structuring element

Contributions

  • Roberto Lotufo, 25mai2014: initial function.