[iaconv] [Up] [iasobel] Image Filtering

iapconv
2D Periodic convolution.

Synopsis

g = iapconv( f, h )

Implemented in Python.

Input

f Image. Gray-scale (uint8 or uint16) or binary image (logical).

Input image.

h Image. Gray-scale (uint8 or uint16) or binary image (logical).

PSF (point spread function), or kernel. The origin is at the array center.

Output

g Image. Gray-scale (uint8 or uint16) or binary image (logical).

Description

Perform a 2D discrete periodic convolution. The kernel origin is at the center of image h. Both image and kernel are periodic with same period. Usually the kernel h is smaller than the image f, so h is padded with zero until the size of f.

Examples

>>> import Numeric

              
>>> f = Numeric.zeros((5,5))

              
>>> f[2,2] = 1

              
>>> print f
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 1 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
>>> h = Numeric.array([[1,2,3],[4,5,6]])

              
>>> print h
[[1 2 3]
 [4 5 6]]
>>> a = iapconv(f,h)

              
>>> print a
[[ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  1.  2.  3.  0.]
 [ 0.  4.  5.  6.  0.]
 [ 0.  0.  0.  0.  0.]]
>>> f = Numeric.array([[1,0,0,0],[0,0,0,0]])

              
>>> print f
[[1 0 0 0]
 [0 0 0 0]]
>>> h = Numeric.array([1,2,3])

              
>>> print h
[1 2 3]
>>> a = iapconv(f,h)

              
>>> print a
[[ 2.  3.  0.  1.]
 [ 0.  0.  0.  0.]]
>>> f = Numeric.array([[1,0,0,0,0,0],[0,0,0,0,0,0]])

              
>>> print f
[[1 0 0 0 0 0]
 [0 0 0 0 0 0]]
>>> h = Numeric.array([1,2,3,4])

              
>>> print h
[1 2 3 4]
>>> a = iapconv(f,h)

              
>>> print a
[[ 2.  3.  4.  0.  0.  1.]
 [ 0.  0.  0.  0.  0.  0.]]
>>> f = iaread('cameraman.pgm')

              
>>> h = [[1,2,1],[0,0,0],[-1,-2,-1]]

              
>>> g = iapconv(f,h)

              
>>> gn = ianormalize(g, [0,255])

              
>>> iashow(gn)
(256, 256) Min= 0.0 Max= 255.0 Mean=131.040 Std=15.65
gn

Equation


where mod(x,N) is given by


the images f and h are periodic with dimension (N,M).

Limitations

Both image and kernel are internally converted to double.

Source Code

def iapconv(f, h):
    from Numeric import asarray, ravel, NewAxis, Float64, concatenate, zeros
    f, h = asarray(f), asarray(h)
    faux, haux = ravel(f), ravel(h)
    if len(f.shape) == 1: f = f[NewAxis,:]
    if len(h.shape) == 1: h = h[NewAxis,:]
    (rows, cols) = f.shape
    (hrows, hcols) = h.shape
    f = f.astype(Float64)
    h = h.astype(Float64)
    dr1 = int((hrows-1)/2.)
    dr2 = hrows-dr1
    dc1 = int((hcols-1)/2.)
    dc2 = hcols-dc1
    p = concatenate((concatenate((f[-dr2+1::,:], f)), f[0:dr1,:])) # Insert lines above and below periodicly
    p = concatenate((concatenate((p[:,-dc2+1::], p), 1), p[:,0:dc1]), 1) # Insert columns at left and right periodcly
    g = zeros((rows,cols))
    for r in range(hrows):
       for c in range(hcols):
          hw = h[hrows-r-1,hcols-c-1]
          if (hw):
            g = g + h[hrows-r-1,hcols-c-1] * p[r:rows+r,c:cols+c]
    return g
    

See also

iaconv 2D convolution.
iaptrans Periodic translation.
[iaconv] [Up] [iasobel] http://www.python.org