[iacontour] [Up] [iapconv] Image Filtering

iaconv
2D convolution.

Synopsis

g = iaconv( 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 convolution. The kernel origin is at the center of image h.

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 = iaconv(f,h)

              
>>> print a
[[0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0]
 [0 0 1 2 3 0 0]
 [0 0 4 5 6 0 0]
 [0 0 0 0 0 0 0]
 [0 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 = iaconv(f,h)

              
>>> print a
[[1 2 3 0 0 0]
 [0 0 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 = iaconv(f,h)

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

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

              
>>> g = iaconv(f,h)

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

              
>>> iashow(gn)
(258, 258) Min= 0.0 Max= 255.0 Mean=131.495 Std=19.08
gn

Limitations

Both image and kernel are internally converted to double.

Equation


where AxB and CxD are the dimensions of f(x,y) and h(x,y) respectively.

Source Code

def iaconv(f, h):
    from Numeric import asarray, NewAxis, zeros, array, product
    f, h = asarray(f), asarray(h)
    if len(f.shape) == 1: f = f[NewAxis,:]
    if len(h.shape) == 1: h = h[NewAxis,:]
    if product(f.shape) < product(h.shape):
        f, h = h, f
    g = zeros(array(f.shape) + array(h.shape) - 1)
    for i in range(h.shape[0]):
        for j in range(h.shape[1]):
            g[i:i+f.shape[0], j:j+f.shape[1]] += h[i,j] * f
    return g
    

See also

iaconv 2D convolution.
[iacontour] [Up] [iapconv] http://www.python.org