[mmgdist] [Up] [mmclohole] Image Transforms

mmopentransf
Open transform.

Synopsis

y = mmopentransf( f, type = 'OCTAGON', n = 65535, Bc = None, Buser = None )

Implemented in Python.

Input

f Image Binary image.
type String

Disk family: 'OCTAGON', 'CHESSBOARD', 'CITY-BLOCK', 'LINEAR-V', 'LINEAR-H', 'LINEAR-45R', 'LINEAR-45L', 'USER'.

Default: 'OCTAGON'

n Double

Maximum disk radii.

Default: 65535

Bc Structuring Element

Connectivity for the reconstructive opening. Used if '-REC' suffix is appended in the 'type' string.

Default: None (3x3 elementary cross)

Buser Structuring Element

User disk, used if 'type' is 'USER'.

Default: None (3x3 elementary cross)

Output

y Image Gray-scale (uint8 or uint16) image.

Description

Compute the open transform of a binary image. The value of the pixels in the open transform gives the largest radii of the disk plus 1, where the open by it is not empty at that pixel. The disk sequence must satisfy the following: if r > s, rB is sB-open, i.e. rB open by sB is equal rB. Note that the Euclidean disk does not satisfy this property in the discrete grid. This function also computes the reconstructive open transform by adding the suffix '-REC' in the 'type' parameter.

Examples

>>> f = mmbinary([
              [0,0,0,0,0,0,0,0],
              [0,0,1,1,1,1,0,0],
              [0,0,1,1,1,1,1,0],
              [0,1,0,1,1,1,0,0],
              [1,1,0,0,0,0,0,0]])

              
>>> print mmopentransf( f, 'city-block')
processing r= 0
processing r= 1
processing r= 2
[[0 0 0 0 0 0 0 0]
 [0 0 1 2 2 2 0 0]
 [0 0 2 2 2 2 2 0]
 [0 1 0 2 2 2 0 0]
 [1 1 0 0 0 0 0 0]]
>>> print mmopentransf( f, 'linear-h')
processing r= 0
processing r= 1
processing r= 2
processing r= 3
[[0 0 0 0 0 0 0 0]
 [0 0 2 2 2 2 0 0]
 [0 0 3 3 3 3 3 0]
 [0 1 0 2 2 2 0 0]
 [2 2 0 0 0 0 0 0]]
>>> print mmopentransf( f, 'linear-45r')
processing r= 0
processing r= 1
processing r= 2
processing r= 3
processing r= 4
[[0 0 0 0 0 0 0 0]
 [0 0 1 4 1 2 0 0]
 [0 0 4 1 2 1 1 0]
 [0 4 0 2 1 1 0 0]
 [4 1 0 0 0 0 0 0]]
>>> print mmopentransf( f, 'user',10,mmsecross(),mmbinary([0,1,1]))
processing r= 0
processing r= 1
processing r= 2
processing r= 3
processing r= 4
[[0 0 0 0 0 0 0 0]
 [0 0 4 4 4 4 0 0]
 [0 0 5 5 5 5 5 0]
 [0 1 0 3 3 3 0 0]
 [2 2 0 0 0 0 0 0]]
>>> print mmopentransf( f, 'city-block-rec')
processing r= 0
processing r= 1
processing r= 2
[[0 0 0 0 0 0 0 0]
 [0 0 2 2 2 2 0 0]
 [0 0 2 2 2 2 2 0]
 [0 1 0 2 2 2 0 0]
 [1 1 0 0 0 0 0 0]]
>>> f=mmreadgray('numbers.tif')

              
>>> mmshow(f)

              
>>> g=mmopentransf(f,'OCTAGON')
processing r= 0
processing r= 1
processing r= 2
processing r= 3
processing r= 4
>>> mmshow(g)

            
f g

The open transform stores all the openings by the disk with radii r
>>> b=mmsedisk(3,'2D','OCTAGON')

              
>>> g1=mmopen(f,b)

              
>>> mmshow(g1)

              
>>> g2=mmcmp(g,'>',3)

              
>>> print mmis(g1,'==',g2)
1.0
g1

Equation

Sequence of convex structuring elements of increasing size
Algorithm implemented

Interpretation

Limitations

As this function may take a long time to execute, there is a printout notifying the status of the execution. The radii of the disk is informed for each open executed. Due to the behavior of the structuring element in the frame of the image, one may want to insert a zero border around the image.

Source Code

def mmopentransf(f, type='OCTAGON', n=65535, Bc=None, Buser=None):
    from Numeric import zeros
    from string import find, upper
    if Bc is None: Bc = mmsecross()
    if Buser is None: Buser = mmsecross()
    assert mmisbinary(f),'Error: input image is not binary'
    type = upper(type)
    rec_flag = find(type,'-REC')
    if rec_flag != -1:
        type = type[:rec_flag] # remove the -rec suffix
    flag = not ((type == 'OCTAGON')  or
                (type == 'CHESSBOARD') or
                (type == 'CITY-BLOCK'))
    if not flag:
        n  = min(n,min(f.shape))
    elif  type == 'LINEAR-H':
        se = mmbinary([1, 1, 1])
        n  = min(n,f.shape[1])
    elif  type =='LINEAR-V':
        se = mmbinary([[1],[1],[1]])
        n  = min(n,f.shape[0])
    elif  type == 'LINEAR-45R':
        se = mmbinary([[0, 0, 1],[0, 1, 0],[1, 0, 0]])
        n  = min(n,min(f.shape))
    elif  type == 'LINEAR-45L':
        se = mmbinary([[1, 0, 0],[0, 1, 0],[0, 0, 1]])
        n  = min(n,min(f.shape))
    elif  type == 'USER':
        se = Buser
        n  = min(n,min(f.shape))
    else:
        print 'Error: only accepts OCTAGON, CHESSBOARD, CITY-BLOCK, LINEAR-H, LINEAR-V, LINEAR-45R, LINEAR-45L, or USER as type, or with suffix -REC.'
        return []
    k = 0
    y = uint16(zeros(f.shape))
    a = mmbinary([1])
    z = mmbinary([0])
    while not (mmisequal(a,z) or (k>=n)):
        print 'processing r=',k
        if flag:
            a = mmopen(f,mmsesum(se,k))
        else:
            a = mmopen(f,mmsedisk(k,'2D',type))
        y = mmaddm(y, mmgray(a,'uint16',1))
        k = k+1
    if rec_flag != -1:
        y = mmgrain(mmlabel(f,Bc),y,'max')
    return y
    

See also

mmopen Morphological opening.
mmsedisk Create a disk or a semi-sphere structuring element.
mmpatspec Pattern spectrum (also known as granulometric size density).
[mmgdist] [Up] [mmclohole] Python