[ianeg] [Up] [iasub2ind] Image Information and Manipulation

ianormalize
Normalize the pixels values between the specified range.

Synopsis

g = ianormalize( f, range )

Implemented in Python.

Input

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

input image.

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

vector: minimum and maximum values in the output image, respectively.

Output

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

normalized image.

Description

Normalize the input image f. The minimum value of f is assigned to the minimum desired value and the maximum value of f, to the maximum desired value. The minimum and maximum desired values are given by the parameter range.

Examples

>>> import Numeric

              
>>> f = Numeric.array([100., 500., 1000.])

              
>>> g1 = ianormalize(f, [0,255])

              
>>> print g1
[   0.          113.33333333  255.        ]
>>> g2 = ianormalize(f, [-1,1])

              
>>> print g2
[-1.         -0.11111111  1.        ]
>>> g3 = ianormalize(f, [0,1])

              
>>> print g3
[ 0.          0.44444444  1.        ]
>>> #
>>> f = Numeric.array([-100., 0., 100.])

              
>>> g4 = ianormalize(f, [0,255])

              
>>> print g4
[   0.   127.5  255. ]
>>> g5 = ianormalize(f, [-1,1])

              
>>> print g5
[-1.  0.  1.]

Equation

Source Code

def ianormalize(f, range):
    from Numeric import asarray, ravel, Float, ones, reshape
    f = asarray(f)
    if f.typecode() in ['D', 'F']:
        iaerror('error: cannot normalize complex data')
        return None
    faux = ravel(f).astype(Float)
    minimum = min(faux)
    maximum = max(faux)
    lower = range[0]
    upper = range[1]
    if upper == lower:
        iaerror('error: image is constant, cannot normalize')
        return f
    if minimum == maximum:
        g = ones(f.shape)*(upper + lower) / 2.
    else:
        g = (faux-minimum)*(upper-lower) / (maximum-minimum) + lower
    g = reshape(g, f.shape)
    T = f.typecode()
    if T == 'b': # UnsignedInt8
        if upper > 255:
            iaerror('ianormalize: warning, upper valuer larger than 255. Cannot fit in uint8 image')
        g = g.astype('b')
    ### Nao ha' uint16 no Numeric (foi implementado no numarray)
    return g
    
[ianeg] [Up] [iasub2ind] http://www.python.org