[mmopen] [Up] [mmgdist] Image Transforms

mmdist
Distance transform.

Synopsis

y = mmdist( f, Bc = None, METRIC = None )

Implemented in Python.

Input

f Image Binary image.
Bc Structuring Element

(connectivity)

Default: None (3x3 elementary cross)

METRIC String

'EUCLIDEAN', or 'EUC2' for squared Euclidean.

Default: None

Output

y Image

distance image in uint16, or in int32 datatype with EUC2 option.

Description

mmdist creates the distance image y of the binary image f. The value of y at the pixel x is the distance of x to the complement of f, that is, the distance of x to nearest point in the complement of f. The distances available are based on the Euclidean metrics and on metrics generated by a a regular graph, that is characterized by a connectivity rule defined by the structuring element Bc. The implementation of the Euclidean algorithm is based on LotuZamp:01.

Examples

Simple numeric example
>>> a = mmframe(mmbinary(ones((5,9))),2,4)

              
>>> f4=mmdist(a)

              
>>> f8=mmdist(a,mmsebox())

              
>>> fe=mmdist(a,mmsebox(),'EUCLIDEAN')

            
Image example
>>> f = mmreadgray('gear.tif')

              
>>> f = mmneg(mmgradm(f))

              
>>> d4=mmdist(f)

              
>>> d8=mmdist(f,mmsebox())

              
>>> de=mmdist(f,mmsebox(),'EUCLIDEAN')

              
>>> mmshow(f)

              
>>> mmshow(d4%8)
Warning: Converting input image from int32 to uint16.
>>> mmshow(d8%8)
Warning: Converting input image from int32 to uint16.
>>> mmshow(de%8)
Warning: Converting input image from int32 to uint16.
f d4%8 d8%8 de%8

Equation

distance of a point x to a set X:
distance function:
distance function using structuring element:
Relationship between erosion and distance transform:

Limitations

To generate useful Distance transforms, the structuring elements must be symmetric and have the origin included. The Euclidean distance transform is rounded to the nearest integer, since it is represented as an unsigned integer image. Use the option EUC2 to compute exact squared Euclidean distance transform.

Source Code

def mmdist(f, Bc=None, METRIC=None):
    from string import upper
    from Numeric import zeros, sqrt
    if Bc is None: Bc = mmsecross()
    if METRIC is not None:
       METRIC = upper(METRIC)
    f = mmgray(f,'uint16')
    y = mmintersec(f,0)
    if (METRIC == 'EUCLIDEAN') or (METRIC == 'EUC2'):
        b = int32(zeros((3,3)))
        i=1
        while not mmisequal(f,y):
            a4,a2 = -4*i+2, -2*i+1
            b = int32([[a4,a2,a4],
                       [a2, 0,a2],
                       [a4,a2,a4]])
            y=f
            i=i+1
            f = mmero(f,b)
        if METRIC == 'EUCLIDEAN':
            f = uint16(sqrt(f)+0.5)
    else:
        if mmisequal(Bc, mmsecross()):
            b = int32([[-2147483647,  -1, -2147483647],
                       [         -1,   0,          -1],
                       [-2147483647,  -1, -2147483647]])
        elif mmisequal(Bc, mmsebox()):
            b = int32([[-1,-1,-1],
                       [-1, 0,-1],
                       [-1,-1,-1]])
        else: b = Bc
        while not mmisequal(f,y):
            y=f
            f = mmero(f,b)
    return y
    

See also

mmfreedom Control automatic data type conversion.
mmdtshow Display a distance transform image with an iso-line color table.
mmero Erode an image by a structuring element.
mmgdist Geodesic Distance Transform.
mmsebox Create a box structuring element.
mmsecross Diamond structuring element and elementary 3x3 cross.
[mmopen] [Up] [mmgdist] Python