Function iaconv

Synopse

2D or 3D linear discrete convolution.

  • g = iaconv(f, h)
    • g: Image, dtype = float64
    • f: Image. input image.
    • h: Image. PSF (point spread function), or kernel. The origin is at the array origin.
 1 import numpy as np
 2 
 3 def iaconv(f, h):
 4     f, h = np.asarray(f), np.asarray(h,float)
 5     if len(f.shape) == 1: f = f[np.newaxis,:]
 6     if len(h.shape) == 1: h = h[np.newaxis,:]
 7     if f.size < h.size:
 8         f, h = h, f
 9     g = np.zeros(np.array(f.shape) + np.array(h.shape) - 1)
10     if f.ndim == 2:
11         H,W = f.shape
12         for (r,c) in np.transpose(np.nonzero(h)):
13             g[r:r+H, c:c+W] += f * h[r,c]
14 
15     if f.ndim == 3:
16         D,H,W = f.shape
17         for (d,r,c) in np.transpose(np.nonzero(h)):
18             g[d:d+D, r:r+H, c:c+W] += f * h[d,r,c]
19 
20     return g

Description

Perform a 2D or 3D discrete linear convolution. The resultant image dimensions are the sum of the input image dimensions minus 1 in each dimension.

Examples

Example 1

 1 import numpy as np
 2 import ia636 as ia
 3 
 4 f = np.zeros((5,5))
 5 f[2,2] = 1
 6 print 'f:\n', f
 7 h = np.array([[1,2,3],
 8               [4,5,6]])
 9 print 'h=\n',h
10 a = ia.iaconv(f,h)
11 print 'a.dtype',a.dtype
12 print 'a:\n',a
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=
[[1 2 3]
 [4 5 6]]
a.dtype float64
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.]]

Example 2

1 f = np.array([[1,0,0,0],
2               [0,0,0,0]])
3 print f
4 h = np.array([1,2,3])
5 print h
6 a = ia.iaconv(f,h)
7 print a
[[1 0 0 0]
 [0 0 0 0]]
[1 2 3]
[[ 1.  2.  3.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]]

Example 3

1 f = np.array([[1,0,0,0,0,0],
2               [0,0,0,0,0,0]])
3 print f
4 h = np.array([1,2,3,4])
5 print h
6 a = ia.iaconv(f,h)
7 print a
[[1 0 0 0 0 0]
 [0 0 0 0 0 0]]
[1 2 3 4]
[[ 1.  2.  3.  4.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.]]

Example 4

 1 f = adreadgray('cameraman.pgm')
 2 h = np.array([[ 1, 2, 1],
 3               [ 0, 0, 0],
 4               [-1,-2,-1]])
 5 g = ia.iaconv(f,h)
 6 gn = ia.ianormalize(g, [0,255])
 7 adshow(f,title='input')
 8 adshow(gn,title='filtered')
 9 print ia.iaimginfo(f)
10 print ia.iaimginfo(g)
<type 'numpy.ndarray'> (256, 256) uint8 0 251
<type 'numpy.ndarray'> (258, 258) float64 -1004.000000 943.000000

input

filtered

Limitations

Both image and kernel are internally converted to double.

Equation

See Also

  • iapconv - 2D, 3D Periodic convolution (kernel origin at center).
  • iapconv2 - 2D Periodic convolution (kernel origin at array origin).