A Look at Contrast Stretching
A Look at Contrast Stretching
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
3 | 11
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; }
( )= ( )+ ( )
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.
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.
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.
(0 255)
Befor noise
T1 x; y
( ) = 255 ( ) = 255
min
min
min min
+ 255
I x; y
I (x;y ) max
min
(1)
Thus we can observe a change in the slope and intercept of the linear scaling function. The transformation function for uncorrupte images 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
(b) Stretched
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) ;
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
(1
2) 100
Vm in
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
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
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.
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.
(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
(0 255)
(e) 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
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
(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
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