[mmmat2set] [Up] [mmreadgray] Image Information and Manipulation

mmset2mat
Converts image representation from set to matrix

Synopsis

M = mmset2mat( A )

Implemented in Python.

Input

A Image

Tuple with array of pixel coordinates and optional array of corresponding pixel values

Output

M Image

Image in matrix format, origin (0,0) at the matrix center

Description

Return an image in the matrix format built from a tuple of an array of pixel coordinates and a corresponding array of pixel values

Examples

>>> coord=int32([
  [ 0,0],
  [-1,0],
  [ 1,1]])

              
>>> A=mmset2mat((coord,))

              
>>> print A
[[0 1 0]
 [0 1 0]
 [0 0 1]]
>>> print mmdatatype(A)
binary
>>> vu = uint8([1,2,3])

              
>>> f=mmset2mat((coord,vu))

              
>>> print f
[[0 2 0]
 [0 1 0]
 [0 0 3]]
>>> print mmdatatype(f)
uint8
>>> vi = int32([1,2,3])

              
>>> g=mmset2mat((coord,vi))

              
>>> print g
[[-2147483647           2 -2147483647]
 [-2147483647           1 -2147483647]
 [-2147483647 -2147483647           3]]
>>> print mmdatatype(g)
int32

Source Code

def mmset2mat(A):
    from MLab import max
    from Numeric import put, ones, ravel, shape, NewAxis, array, asarray
    if len(A) == 2:            
        x, v = A
        v = asarray(v)
    elif len(A) == 1:
        x = A[0]
        v = ones((len(x),), '1')
    else:
        raise TypeError, 'Argument must be a tuple of length 1 or 2'
    if len(x) == 0:  return array([0]).astype(v.typecode())
    if len(x.shape) == 1: x = x[NewAxis,:]
    dh,dw = max(abs(x))
    h,w = (2*dh)+1, (2*dw)+1 
    M=ones((h,w)) * mmlimits(v)[0]
    offset = x[:,0] * w + x[:,1] + (dh*w + dw)
    put(M,offset,v)
    M = M.astype(v.typecode())
    return M
    

See also

mmmat2set Converts image representation from matrix to set
[mmmat2set] [Up] [mmreadgray] Python