[iaycbcr2rgb] [Up] [iageorigid] Geometric Manipulations

iaffine
Affine transform.

Synopsis

g = iaffine( f, T )

Implemented in Python.

Input

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

Affine matrix for the geometric transformation.

Output

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

Description

Apply the affine transform to the coordinate pixels of image f. The resultant image g has the same domain of the input image. Any pixel outside this domain is not shown and any pixel that does not exist in the original image has the nearest pixel value. This method is based on the inverse mapping. An affine transform is a geometrical transformation that preserves the parallelism of lines but not their lengths and angles. The affine transform can be a composition of translation, rotation, scaling and shearing. To simplify this composition, these transformations are represented in homogeneous coordinates using 3x3 matrix T. The origin is at coordinate (0,0).

Examples

>>> f = Numeric.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]])

              
>>> print f
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]
>>> T = Numeric.array([[1,0,0],[0,1,0],[0,0,1]], 'd')

              
>>> print T
[[ 1.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  0.  1.]]
>>> g = iaffine(f,T)

              
>>> print g
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]
>>> T[0,0] = 0.5

              
>>> print T
[[ 0.5  0.   0. ]
 [ 0.   1.   0. ]
 [ 0.   0.   1. ]]
>>> g = iaffine(f,T)

              
>>> print g
[[ 1  3  5  5  5]
 [ 6  8 10 10 10]
 [11 13 15 15 15]]

Equation

Source Code

def iaffine(f, T):
    from Numeric import asarray, ravel, NewAxis, concatenate, reshape, zeros, matrixmultiply, maximum, minimum, take
    from LinearAlgebra import inverse
    f, T = asarray(f), asarray(T)
    faux = ravel(f)
    if len(f.shape) == 1:
        f = f[NewAxis,:]
    (m, n) = f.shape
    (x, y) = iameshgrid(range(n),range(m))
    aux = concatenate((reshape(x, (1,m*n)), reshape(y, (1,m*n))))
    aux = concatenate((aux, zeros((1, m*n))))
    XY = matrixmultiply(inverse(T), aux)
    X, Y = XY[0,:], XY[1,:]
    X = maximum(0, minimum(n-1, X))
    Y = maximum(0, minimum(m-1, Y))
    XYi = iasub2ind(f.shape, map(round, Y), map(round, X))
    g = take(faux, XYi)
    g = reshape(g, f.shape)
    return g
    

See also

iageorigid 2D Rigid body geometric transformation and scaling.
[iaycbcr2rgb] [Up] [iageorigid] http://www.python.org