[iageorigid] [Up] [iaptrans] Geometric Manipulations

iaresize
Resizes an image.

Synopsis

g = iaresize( f, new_shape )

Implemented in Python.

Input

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

[h w], new image dimensions

Output

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

Examples

>>> import Numeric

              
>>> f = Numeric.array([[10,20,30],[40,50,60]])

              
>>> print f
[[10 20 30]
 [40 50 60]]
>>> g = iaresize(f, [5,7])

              
>>> print g
[[10 10 10 20 20 30 30]
 [10 10 10 20 20 30 30]
 [10 10 10 20 20 30 30]
 [40 40 40 50 50 60 60]
 [40 40 40 50 50 60 60]]

Source Code

def iaresize(f, new_shape):
    from Numeric import array, asarray, ravel, reshape, ceil, concatenate, zeros, matrixmultiply, maximum, minimum, take
    from LinearAlgebra import inverse
    def calc(f, new_shape=new_shape):
        from Numeric import array, ravel, reshape, ceil, concatenate, zeros, matrixmultiply, maximum, minimum, take
        from LinearAlgebra import inverse
        Sh = 1.* new_shape[0]/f.shape[0]
        Sw = 1.* new_shape[1]/f.shape[1]
        T = array([[Sw,0,0],[0,Sh,0],[0,0,1]])
        faux = ravel(f)
        if len(f.shape) == 1:
            f = reshape(f, (1, f.shape[0]))
        m, n = ceil(Sh*f.shape[0]), ceil(Sw*f.shape[1])
        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+1)
        X, Y = XY[0,:], XY[1,:]
        X = maximum(1, minimum(f.shape[1], X))
        Y = maximum(1, minimum(f.shape[0], Y))
        XYi = iasub2ind(f.shape, map(round, Y-1), map(round, X-1))
        g = take(faux, XYi)
        g = reshape(g, (m,n))
        g = g[0:new_shape[0], 0:new_shape[1]]
        return g
    f = asarray(f)
    if len(f.shape) == 3: # imagem colorida
        g = zeros(concatenate(([3],new_shape)))
        for i in range(f.shape[0]):
            g[i,:,:] = calc(f[i,:,:])
    else:
        g = calc(f)
    return g
    
[iageorigid] [Up] [iaptrans] http://www.python.org