Function ianormalize

Synopse

Normalize the pixels values between the specified range.

  • g = ianormalize(f, range)
    • g: Output: ndmage, same type as f.
    • f: Input: ndimage.
    • range: Image. vector: minimum and maximum values in the output image, respectively.

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. The data type of the normalized image is the same data type of input image.

 1 from numpy import *
 2 
 3 def ianormalize(f, range=[0,255]):
 4 
 5     f = asarray(f)
 6     range = asarray(range)
 7     if f.dtype.char in ['D', 'F']:
 8         raise Exception, 'error: cannot normalize complex data'
 9     faux = ravel(f).astype(float)
10     minimum = faux.min()
11     maximum = faux.max()
12     lower = range[0]
13     upper = range[1]
14     if upper == lower:
15         g = ones(f.shape) * maximum
16     if minimum == maximum:
17         g = ones(f.shape) * (upper + lower) / 2.
18     else:
19         g = (faux-minimum)*(upper-lower) / (maximum-minimum) + lower
20     g = reshape(g, f.shape)
21 
22     if f.dtype == uint8:
23         if upper > 255:
24             raise Exception,'ianormalize: warning, upper valuer larger than 255. Cannot fit in uint8 image'
25     g = g.astype(f.dtype) # set data type of result the same as the input image
26     return g

Examples

Example 1

 1 from ia636 import ianormalize
 2 
 3 f = array([100., 500., 1000.])
 4 g1 = ianormalize(f, [0,255])
 5 print g1
 6 g2 = ianormalize(f, [-1,1])
 7 print g2
 8 g3 = ianormalize(f, [0,1])
 9 print g3
10 #
11 f = array([-100., 0., 100.])
12 g4 = ianormalize(f, [0,255])
13 print g4
14 g5 = ianormalize(f, [-1,1])
15 print g5
16 g6 = ianormalize(f, [-0.5,0.5])
17 print g6
18 #
19 f = arange(10).astype('uint8')
20 g7 = ianormalize(f)
21 print g7
22 #
23 f = array([1,1,1])
24 g8 = ianormalize(f)
25 print g8
[   0.          113.33333333  255.        ]
[-1.         -0.11111111  1.        ]
[ 0.          0.44444444  1.        ]
[   0.   127.5  255. ]
[-1.  0.  1.]
[-0.5  0.   0.5]
[  0  28  56  85 113 141 170 198 226 255]
[127 127 127]

Equation

See Also

Contributions

  • Marcos Fernandes, course IA368S, 1st semester 2013