[iaroi] [Up] [iapad] Image Information and Manipulation

iacrop
Crop an image to find the minimum rectangle.

Synopsis

g = iacrop( f, side = 'all', color = 'black' )

Implemented in Python.

Input

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

input image.

side String.

side of the edge which will be removed. Possible values: 'all', 'left', 'right', 'top', 'bottom'.

Default: 'all'

color String.

color of the edge. Possible values: 'black', 'white'.

Default: 'black'

Output

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

Examples

>>> f = iaread('club.pgm')

              
>>> iashow(f)
(178, 158) Min= 0 Max= 255 Mean=68.220 Std=112.88
>>> g = iacrop(f)

              
>>> iashow(g)
(124, 108) Min= 0 Max= 255 Mean=143.266 Std=126.53
f g

Source Code

def iacrop(f, side='all', color='black'):
    from Numeric import asarray, NewAxis, sometrue, nonzero
    ##if len(f.shape) == 1: f = f[NewAxis,:]
    ##aux1, aux2 = sometrue(f,0), sometrue(f,1)
    ##col, row = nonzero(aux1), nonzero(aux2)
    ##g = f[row[0]:row[-1]+1, col[0]:col[-1]+1]
    f = asarray(f)
    if len(f.shape) == 1: f = f[NewAxis,:]
    if color == 'white': f = ianeg(f)
    aux1, aux2 = sometrue(f,0), sometrue(f,1)
    col, row = nonzero(aux1), nonzero(aux2)
    if (not col) and (not row):
        return None
    if   side == 'left':   g = f[:, col[0]::]
    elif side == 'right':  g = f[:, 0:col[-1]+1]
    elif side == 'top':    g = f[row[0]::, :]
    elif side == 'bottom': g = f[0:row[-1]+1, :]
    else:                  g = f[row[0]:row[-1]+1, col[0]:col[-1]+1]
    if color == 'white': g = ianeg(g)
    return g
    

See also

iapad Extend the image inserting a frame around it.
[iaroi] [Up] [iapad] http://www.python.org