[iagenimages] [Up] [iahisteq] Lessons

iait
Illustrate the contrast transform function

Description

The intensity transform takes the form s=T(v) where v is the input gray scale value and s is the output gray scale value. This transform has many names: contrast transform, color lookup table, colormap, etc. This transform function T can be implemented by a simple table. In the examples here, we can show many of the applications one can use the contrast transform function

Demo Script

Identity intensity function

The simplest intensity function is the identify s=v. This transform is a line of 45 degrees. It makes the output image the same as the input image.

>>> import Numeric

                  
>>> f = iaramp([100,100], 10, [0,255])

                  
>>> it = Numeric.arange(256)

                  
>>> g = iaapplylut(f, it)

                  
>>> iashow(f)
(100, 100) Min= 0 Max= 255 Mean=127.200 Std=81.37
>>> iashow(g)
(100, 100) Min= 0 Max= 255 Mean=127.200 Std=81.37
f g

Visualizing the intensity transform function

It is common to visualize the intensity transform function in a plot, T(v) x v.

>>> g,d = iaplot(it)
>>> g('set data style boxes')

                  
>>> g.plot(d)
>>> 
it d

Changing a particular color of an image

To change a given gray scale value v1 of the input image to another gray scale value s1, we can change the identity function such that T(v1)=s1. Suppose we want to change any pixel with value 0 to 255.

>>> it1 = 1*it

                  
>>> it1[0] = 255

                  
>>> print it1[0:5] # show the start of the intensity table
[255   1   2   3   4]
>>> g = iaapplylut(f, it1)

                  
>>> iashow(g)
(100, 100) Min= 28 Max= 255 Mean=152.700 Std=77.37
g

Negative of an image

To invert the gray scale of an image, we can apply an intensity transform of the form T(v) = 255 - v. This transform will make dark pixels light and light pixels dark.

>>> v = Numeric.arange(255)

                  
>>> Tn = 255 - v

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

                  
>>> g = iaapplylut(f, Tn)

                  
>>> iashow(g)
(256, 256) Min= 4 Max= 255 Mean=117.934 Std=87.20
>>> g,d = iaplot(Tn)
>>> g('set data style boxes')

                  
>>> g.plot(d)
>>> 
g Tn
d

Thresholding

A common operation in image processing is called thresholding. It assigns value 1 to all pixels equal or above a threshold value and assigns zero to the others. The threshold operator converts a gray scale image into a binary image. It can be easily implemented using the intensity transform. In the example below the threshold value is 128.

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

                  
>>> thr = Numeric.concatenate((Numeric.zeros(128), Numeric.ones(128)))

                  
>>> g = iaapplylut(f, thr)

                  
>>> iashow(g)
(256, 256) Min= 0 Max= 1 Mean=0.649 Std=0.48
>>> g,d = iaplot(g)
>>> g('set data style boxes')

                  
>>> g.plot(d)
>>> 
g g
d

Threshold band

A variation of the thresholding is when the output is one for a range of the input gray scale values. In the example below, only pixels between values 100 and 120 are turned to one, the others to zero.

>>> t1 = Numeric.zeros(256)

                  
>>> t1[100:121] = 1

                  
>>> g = iaapplylut(f, t1)

                  
>>> iashow(g)
(256, 256) Min= 0 Max= 1 Mean=0.012 Std=0.11
>>> g,d = iaplot(t1)
>>> g('set data style boxes')

                  
>>> g.plot(d)
>>> 
g t1
d

Generalized Threshold

A generalization of the previous case is to assign different values (classes) to different input ranges. This is a typical classification decision. For pixels from 0 and t1, assign 1; from t1 to t2, assign 2, etc. In the example below, the pixels are classified in three categories: class 1: dark pixels (between 0 and 50) corresponding to the cameraman clothing, class 3: medium gray pixels (from 51 to 180), and class 2: white pixels (from 181 to 255), corresponding to the sky.

>>> t2 = Numeric.zeros(256)

                  
>>> t2[0:51] = 1

                  
>>> t2[51:181] = 3

                  
>>> t2[181:256] = 2

                  
>>> g = iaapplylut(f, t2)

                  
>>> iashow(g)
(256, 256) Min= 1 Max= 3 Mean=1.910 Std=0.70
>>> g,d = iaplot(t2)
>>> g('set data style boxes')

                  
>>> g.plot(d)
>>> 
g t2
d

Crescent functions

When the intensity transform is a crescent function, if v1 >= v1 then T(v1) >= T(v2). In this class of intensity transforms, the order of the gray scale does not change, i.e., if a gray scale value is darker than another in the input image, in the transformed image, it will still be darker. The intensity order does not change. This particular intensity transforms are of special interest to image enhancing as our visual system does not feel confortable to non-crescent intensity transforms. Note the negation and the generalized threshold examples. The identity transform is a crescent function with slope 1. If the slope is higher than 1, for a small variation of v, there will be a larger variation in T(v), increasing the contrast around the gray scale v. If the slope is less than 1, the effect is opposite, the constrast around v will be decreased. A logarithm function has a higher slope at its beginning and lower slope at its end. It is normally used to increase the contrast of dark areas of the image.

>>> v = Numeric.arange(256)

                  
>>> Tlog = (256./Numeric.log(256.)) * Numeric.log(v + 1)

                  
>>> f = ianormalize(iaread('lenina.pgm'), [0, 255])

                  
>>> g = iaapplylut(f.astype('b'), Tlog)

                  
>>> iashow(f)
(256, 256) Min= 0 Max= 255 Mean=61.170 Std=54.05
>>> iashow(g)
(256, 256) Min= 0.0 Max= 256.0 Mean=177.107 Std=34.66
>>> g,d = iaplot(Tlog)
>>> g('set data style boxes')

                  
>>> g.plot(d)
>>> 
f g
Tlog d

Normalization function

Sometimes the input image has ranges in floating point or ranges not suitable for displaying or processing. The normalization function is used to fit the range of the gray scales, preserving the linear gray scale relationship of the input image. The intensity function that provides the normalization is a straight line segment which can be defined by its two extremities. Suppose the input gray scales ranges from m1 to m2 and we want to normalize the image to the range of M1 to M2. The two extremities points are (m1,M1) and (m2,M2). The equation for the intensity normalization function is point-slope form of the line equation: T(v) - M1 =(M2-M1)/(m2-m1)*(v - m1). The function ianormalize does that.

[iagenimages] [Up] [iahisteq] http://www.python.org