Look At Contrast Stretching Algorithm

Pi19404
March 10, 2013

Contents

Contents
Look At Contrast Stretching Algorithm
0.1 Introduction . . . . . . . . . . . . . . . . . . . . . . 0.2 Contrast Stretching . . . . . . . . . . . . . . . . . 0.3 Effect of Noise on Contrast Stretching . . . 0.3.1 Effect of Gaussian Noise on Contrast 0.4 Modification of Contrast Stretching . . . . . . 0.5 Modfied Contrast Stretching Version 2 . . . 0.6 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Stretching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3
3 3 4 4 6 10 10 11

2 | 11

Look At Contrast Stretching Algorithm

Look At Contrast Stretching Algorithm
0.1 Introduction
In this article we will look at the implementation of of another contrast stretching algorithm.

0.2 Contrast Stretching
The Aim of contrast Stretching algorithm is to increase enhance the image so as to improve the contrast of the image. Contrast stretching (also called Normalization) stretching the range of intensity values it contains to make full use of possible values. The Transformation function is a linear mapping of input to output values. It does so by finding the minimum and maximum pixel intensity in the image which are the range of input intensity values and linearly scaling it to new minimum or maximum value which is would cover the full range of possible output values. For 8 bit images the full range of possbible values is 0-255. Thus the transformation function for contrast stretching is a piecewise linear function. Let min and max denote the minimum and maximum pixel intensities in the image. Let 0 and 255 denote the minimum and maximum pixel intensity that can be possibly be taken by 8 bit image.The implementation is performed using a look up table. For pixel intensities from 0-255 the lookup table is constructed using the transformation function. Then the transformation lookup is performed for each pixel in the input image.

3 | 11

Look At Contrast Stretching Algorithm

For Color Image different strategies can be used.In the present application the Color Image is split into RGB channels and contrast stretching is performed on individual channel and then the channels are merged together to form the RGB image. The method name for used for contrast stretching is MakeGlobalLUT below is a sample code to generate the lookup table transformation to perform contrast stretching.
1 2 3 4 5 6

for ( int i =0; i < _channels ; i ++) { cv :: minMaxLoc ( _channel [ i ] ,& _min1 ,& _max1 ,0 ,0) ; _min [ i ]= _min1 ; _max [ i ]= _max1 ; for ( int k =0; k <256; k ++) _LUT [ k * _channels + i ]= k - _min1 >0? k > _max1 ?255:255*( k - _min1 ) /( _max1 _min1 ) :0; }

0.3 Effect of Noise on Contrast Stretching
Consider the image noise model
I x; y

( )= ( )+ ( )
f x; y n x; y

Each pixel value is randomly affected by noise. The effect of gaussian and implulsive noise is evaluated on contrast stretching.

0.3.1 Effect of Gaussian Noise on Contrast Stretching
As mentioned in the earlier article the effect of gaussian noise is to spread the histogram. Let us consider the below image .For the uncorrupted image mininim and maximum value take by the BGR channels of the image are ; ; ; ; ; respectively.

(151 207) (152 205) (152 205)

Now a gaussian noise of standard deviation 10 is added to the image. The new minimum and maximum value taken by the BGR channels of the image are ; ; ; ; ; respectively.

(137 222) (138 223) (136 222)

4 | 11

Look At Contrast Stretching Algorithm Before the noise was added the range of pixels of channels of the input image are mapped to the range (0-255). After the noise was added the range of pixels of the channes of the input image ; ; ; ; ; .Thus input pixels which originally were stretched to the entire range of ; are using a lower range due to noise.

(42 210) (42 201) (47 204)

(0 255)

For the uncorrupted image the linear scaling fuction obtained is
T1 x; y T1 x; y

Befor noise
T1 x; y

( ) = 255 £ ( ) = 255 £

I (x;y ) max max

       

min

min

min min

+ 255 £
I x; y

I (x;y ) max

 

min

( ) =  714 91 + 4 5 £ ( ) After noise ( ) =  411 + 3 £ ( )
: : T1 x; y I x; y

(1)

Thus we can observe a change in the slope and intercept of the linear scaling function. The transformation function for uncorrupte image’s has large slope so the pixel in the range ; of input image will be occupy a comparitively large dynamic range and exhibit greater contrast.

(151 207)

The transformation function for the corrupted image as a smaller slope and intercept is also different so the pixels in the range ; which represent uncorrupted pixels will occupy a smaller dynamic range leading to a lower contrast.

(151 207)

Thus contrast stretching is affected by noise. Below is example of contrast stretching performed on clean and noisy image and difference is the results can be observed.Apart from the noise the contrast stretching on the noisy image results in a relatively low contrast image.

This demonstrates that contrast stretching is greatly affected by noise. To reduce the effect of noise we need to indentify minimum and maximum intesity values correctly in the presense of noise. A Class GaussianNoise is defined which supports method add gaussian

5 | 11

Look At Contrast Stretching Algorithm

(a) Clean Image

(b) Stretched

(c) Noisy Stretched

noise of desired mean and standard deviation to the input image.
1 2 3 4 5 6 7 8 9 10 11

vector <Mat > _channel , noisyI ; noisyI . create ( input . rows , input . cols , CV_32FC (1) ) ; noisyI . setTo ( cv :: Scalar :: all (0) ) ; input . convertTo ( input , CV_32FC (3) ,1.0 ,0) ; cv :: split ( input , _channel ) ; for ( int i =0; i < input . channels () ; i ++) { randn ( noisyI , Scalar :: all (0) , Scalar :: all ( std1 /2) ) ; // random sample generator cv :: add ( _channel [ i ] , noisyI , _channel [ i ]) ; } cv :: merge ( _channel , input ) ; input . convertTo ( input , CV_8UC (3) ,1.0 ,0) ;

0.4 Modification of Contrast Stretching
The transformation function for contrast stretching is a piecewise linear function. Due to the addition of noise the range of pixels values observed in the image changes and thus the transformation function changes. Thus pixels values in uncorrupted image’s range can be considered as relevant information and we need to operate on restricted range of input values. But we do not have this information.This information needs to be infered based on observed image. One of the common techniques is to observe the image histogram. The range of pixel values can be obtained by clipping of s1 of pixel values from lower side and s2 of pixel values from the upper side

%

%

6 | 11

Look At Contrast Stretching Algorithm of histogram peak. The Simple Color Balance Algorithm Performs this operation using Cumulative distribution function of image histogram. Let N be the number of pixels in the image.The new minimum value for contrast stretching is selected as lowest pixel value V in which has CDF value higher than N £ s = .The new maximum value V ax for contrast stretching is selected as the highest pixel value which has CDF values lower than N £   s = .

1 100

m

m

(1

2) 100

All pixels below are set to 255.

Vm in

are set to zero and all pixel above

Vm ax

This will enable us to reduce the effects of noise. The algorithm can be implemented as follows : 1. Compute Histogram of image 2. Compute the Cumulative distribution function of image 3. Find pixel index i1 and index such that C DF 4. Set all pixels less than 255.

= (1   ) £ %
s2 N i1

i2

such that

C DF

= £ %,and
s1 N

pixel
i2

to 0 and all pixel greater than

to

5. For all pixels in the range i1 ; i2 apply affine transformation to perform contrast stretching.
pi

[

]

= ( (   ) £ )255
pi i1 i2 i1

For color images the above algorithm is applied on each of channels of the image. Let us consider the below image .For the uncorrupted image mininim and maximum value take by the BGR channels of the image are ; ; ; ; ; respectively.

(151 207) (152 205) (152 205) (151 207) (152 205) (152 205)

Let us consider the below image .For the uncorrupted image mininim and maximum value take by the BGR channels of the image are ; ; ; ; ; respectively.

7 | 11

Look At Contrast Stretching Algorithm

Now a gaussian noise of standard deviation 10 is added to the image. The new minimum and maximum value taken by the BGR channels of the image are ; ; ; ; ; respectively.

(137 222) (138 223) (136 222)

Before the noise was added the range of pixels of channels of the input image are mapped to the range (0-255). After the noise was added the range of pixels of the channes of the input image ; ; ; ; ; .Thus input pixels which originally were stretched to the entire range of ; are using a lower range due to noise.

(42 210) (42 201) (47 204)

(0 255)

For modified contrast stretching the range of pixels for channel fo the input image ; ; ; ; ; .Thus even for the noisy image modifed contrast stretcing provide the range of output pixles which is approximately ; the range for uncorrupted image. Thus

(4 250) (8 241) (4 241)

(0 255)

(d) Clean Image

(e) Stretched

(f) Noisy Stretched

(g) Modified

effect after the effect of noise the output image has a contrast that is approximately equal to contrast of image obtained after applying contrast stretching of uncorrupted image. A class Histogram is defined which compute the histogram of the image and find the lower and upper pixels values for given input ratios s1 and s2 .

8 | 11

Look At Contrast Stretching Algorithm

The method BuildHistogram constructs histogram of the image. Each channel of image is prcessed independently.

1 2 3 4 5 6

cv :: Mat Histogram :: BuildHistogram ( cv :: Mat srcImage ) { cv :: Mat histMat ; cv :: calcHist (& srcImage , 1 , 0 , cv :: Mat () , _histMat , 1 , & _histSize , & _histRange , true , false ) ; histMat = _histMat . clone () ; return histMat ; }

The method getLowerUpperThresh accepts the input image ,lower and upper ration and returns pixel intensities correponding to lower and upper ration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

float N = image . rows * image . cols ; float maxth = (1 - s2 ) * N ; float minth = s1 * N ; float cmini =0; float cmaxi = N ; for ( int i = 0; i < histMat . rows ; i ++) { cmini += histMat . at < float >( i ) ; cmaxi -= histMat . at < float >(256 - i ) ; if ( cmini >= minth && lower == true ) { mini = i ; lower = false ; } if ( cmaxi <= maxth && higher == true ) { maxi = 256 - i ; higher = false ; } if ( lower == false && higher == false ) break ;

Based on this updated lower and upper threshold values the contrast stretching is performed
1 2 3 4 5 6

Histogram imgHist ; std :: vector < int > imgThresh ; imgThresh = imgHist . getThresh ( _channel [ i ] , s1 [ i ] , s2 [ i ]) ; cv :: minMaxLoc ( _channel [ i ] ,& _min1 ,& _max1 ,0 ,0) ; for ( int k =0; k <256; k ++) _LUT [ k * _channels + i ]= k - imgThresh [0] <0?0: k > imgThresh [1]?255:(255*( k _min [ i ]) /( _max [ i ] - _min [ i ]) ) ;

9 | 11

Look At Contrast Stretching Algorithm

0.5 Modfied Contrast Stretching Version 2
Another issue with contrast stretching is that if the input image occupies the pixels in the range ; and we apply the contrast stretching on a good contrast image using a modified linear transformation function,it may not necessarily leads to a improved image.

(0 255)

It may be better to retain the original image. The minimum pixels intensities in the image are computed. intensity must be above a specified threshold if we modified transformation.By default the value is set to

maximum and The maximum are to apply 70;

Another Contraint we apply is that max value for threshold be above atleast threshold/2; These thresholds will depend on application being considered

0.6 Code
we define a class ContrastStretching it supports mode as DEFAULT for standard contrast stretching and V1 for modified contrast stretching. The code OpenCV code can be found in code repository https: //github.com/pi19404/m19404/tree/master/ContrastStretching/ContrastStretching or https://code.google.com/p/m19404/source/browse/ContrastStretching/ ContrastStretching

10 | 11

Bibliography

Bibliography
[1] Nicolas Limare et al.  Simplest Color Balance. In: Image Processing On Line 2011 (2011).

doi: 10.5201/ipol.2011.llmps-scb.

11 | 11

Sign up to vote on this title
UsefulNot useful