11

Click here to load reader

A Look At Contrast Stretching

Embed Size (px)

DESCRIPTION

A Look and Contrast Stretching Algorithm and improvement in presence of noise.Description and Implementation provided using OpenCV Libraries

Citation preview

Page 1: A Look At Contrast Stretching

Look At ContrastStretchingAlgorithm

Pi19404

March 10, 2013

Page 2: A Look At Contrast Stretching

Contents

Contents

Look At Contrast Stretching Algorithm 3

0.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30.2 Contrast Stretching . . . . . . . . . . . . . . . . . . . . . . . . . 3

0.3 Effect of Noise on Contrast Stretching . . . . . . . . . . . 4

0.3.1 Effect of Gaussian Noise on Contrast Stretching 4

0.4 Modification of Contrast Stretching . . . . . . . . . . . . . . 6

0.5 Modfied Contrast Stretching Version 2 . . . . . . . . . . . 10

0.6 Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2 | 11

Page 3: A Look At Contrast Stretching

Look At Contrast Stretching Algorithm

Look At Contrast StretchingAlgorithm

0.1 IntroductionIn this article we will look at the implementation of of anothercontrast stretching algorithm.

0.2 Contrast Stretching

The Aim of contrast Stretching algorithm is to increase enhance theimage so as to improve the contrast of the image.

Contrast stretching (also called Normalization) stretching the rangeof intensity values it contains to make full use of possible values. TheTransformation function is a linear mapping of input to output values.

It does so by finding the minimum and maximum pixel intensityin the image which are the range of input intensity values andlinearly scaling it to new minimum or maximum value which is wouldcover the full range of possible output values. For 8 bit images thefull range of possbible values is 0-255.

Thus the transformation function for contrast stretching is apiecewise linear function.

Let min and max denote the minimum and maximum pixel inten-sities in the image. Let 0 and 255 denote the minimum andmaximum pixel intensity that can be possibly be taken by 8 bitimage.The implementation is performed using a look up table.

For pixel intensities from 0-255 the lookup table is constructedusing the transformation function.

Then the transformation lookup is performed for each pixel inthe input image.

3 | 11

Page 4: A Look At Contrast Stretching

Look At Contrast Stretching Algorithm

For Color Image different strategies can be used.In the presentapplication the Color Image is split into RGB channels and contraststretching is performed on individual channel and then the channelsare merged together to form the RGB image.

The method name for used for contrast stretching is MakeGlobalLUTbelow is a sample code to generate the lookup table transformationto perform contrast stretching.

1 for(int i=0;i<_channels;i++){

2 cv:: minMaxLoc (_channel[i],&_min1 ,&_max1 ,0,0);

3 _min[i]= _min1;_max[i]=_max1;

4 for(int k=0;k <256;k++)

5 _LUT[k*_channels+i]=k-_min1 >0?k>_max1 ?255:255*(k-_min1)/(_max1 -

_min1):0;

6 }

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 ofgaussian 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 isto spread the histogram.

Let us consider the below image .For the uncorrupted image mininimand maximum value take by the BGR channels of the image are(151; 207); (152; 205); (152; 205) respectively.

Now a gaussian noise of standard deviation 10 is added to the im-age. The new minimum and maximum value taken by the BGR channelsof the image are (137; 222); (138; 223); (136; 222) respectively.

4 | 11

Page 5: A Look At Contrast Stretching

Look At Contrast Stretching Algorithm

Before the noise was added the range of pixels of channels of theinput image are mapped to the range (0-255).

After the noise was added the range of pixels of the channesof the input image (42; 210); (42; 201); (47; 204).Thus input pixels whichoriginally were stretched to the entire range of (0; 255) are using alower range due to noise.

For the uncorrupted image the linear scaling fuction obtained is

T1(x; y) = 255 � I(x;y)�min

max�min

T1(x; y) = 255 � �minmax�min

+ 255 � I(x;y)max�min

Befor noiseT1(x; y) = �714:91 + 4:5 � I(x; y)After noiseT1(x; y) = �411 + 3 � I(x; y)

(1)

Thus we can observe a change in the slope and intercept of thelinear scaling function.

The transformation function for uncorrupte image’s has large slopeso the pixel in the range (151; 207) of input image will be occupy acomparitively large dynamic range and exhibit greater contrast.

The transformation function for the corrupted image as a smallerslope and intercept is also different so the pixels in the range(151; 207) which represent uncorrupted pixels will occupy a smallerdynamic range leading to a lower contrast.

Thus contrast stretching is affected by noise.

Below is example of contrast stretching performed on clean andnoisy image and difference is the results can be observed.Apartfrom the noise the contrast stretching on the noisy image resultsin a relatively low contrast image.

This demonstrates that contrast stretching is greatly affectedby noise. To reduce the effect of noise we need to indentifyminimum and maximum intesity values correctly in the presense ofnoise.

A Class GaussianNoise is defined which supports method add gaussian

5 | 11

Page 6: A Look At Contrast Stretching

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 vector <Mat > _channel ,noisyI;

2 noisyI.create (input.rows ,input.cols ,CV_32FC (1));

3 noisyI.setTo (cv:: Scalar ::all (0));

4 input.convertTo (input ,CV_32FC (3) ,1.0,0);

5 cv:: split(input ,_channel);

6 for(int i=0;i<input.channels ();i++){

7 randn(noisyI , Scalar ::all (0), Scalar ::all(std1 /2));// random sample

generator

8 cv::add(_channel[i],noisyI ,_channel[i]);

9 }

10 cv:: merge (_channel ,input);

11 input.convertTo (input ,CV_8UC (3) ,1.0,0);

0.4 Modification of Contrast Stretching

The transformation function for contrast stretching is a piecewiselinear function.

Due to the addition of noise the range of pixels values ob-served in the image changes and thus the transformation functionchanges.

Thus pixels values in uncorrupted image’s range can be considered asrelevant information and we need to operate on restricted rangeof input values.

But we do not have this information.This information needs tobe 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 pixelvalues from lower side and s2% of pixel values from the upper side

6 | 11

Page 7: A Look At Contrast Stretching

Look At Contrast Stretching Algorithm

of histogram peak.

The Simple Color Balance Algorithm Performs this operation usingCumulative distribution function of image histogram.

Let N be the number of pixels in the image.The new minimumvalue for contrast stretching is selected as lowest pixel value Vmin

which has CDF value higher than N � s1=100.The new maximum valueVmax for contrast stretching is selected as the highest pixel valuewhich has CDF values lower than N � (1� s2)=100.

All pixels below Vmin are set to zero and all pixel above Vmaxare set to 255.

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 i2 such that CDF = s1 � N%,and pixelindex such that CDF = (1� s2) �N%

4. Set all pixels less than i1 to 0 and all pixel greater than i2 to255.

5. For all pixels in the range [i1; i2] apply affine transformation toperform contrast stretching.

pi =(pi � i1) � 255

(i2 � i1)

For color images the above algorithm is applied on each of channelsof the image.

Let us consider the below image .For the uncorrupted image mininimand maximum value take by the BGR channels of the image are(151; 207); (152; 205); (152; 205) respectively.

Let us consider the below image .For the uncorrupted image mininimand maximum value take by the BGR channels of the image are(151; 207); (152; 205); (152; 205) respectively.

7 | 11

Page 8: A Look At Contrast Stretching

Look At Contrast Stretching Algorithm

Now a gaussian noise of standard deviation 10 is added to the im-age. The new minimum and maximum value taken by the BGR channelsof the image are (137; 222); (138; 223); (136; 222) respectively.

Before the noise was added the range of pixels of channels of theinput image are mapped to the range (0-255).

After the noise was added the range of pixels of the channesof the input image (42; 210); (42; 201); (47; 204).Thus input pixels whichoriginally were stretched to the entire range of (0; 255) are using alower range due to noise.

For modified contrast stretching the range of pixels for chan-nel fo the input image (4; 250); (8; 241); (4; 241).Thus even for the noisyimage modifed contrast stretcing provide the range of output pixleswhich is approximately (0; 255) the range for uncorrupted image. Thus

(d) Clean Image (e) Stretched

(f) Noisy Stretched (g) Modified

effect after the effect of noise the output image has a contrastthat is approximately equal to contrast of image obtained afterapplying contrast stretching of uncorrupted image.

A class Histogram is defined which compute the histogram of theimage and find the lower and upper pixels values for given inputratios s1 and s2.

8 | 11

Page 9: A Look At Contrast Stretching

Look At Contrast Stretching Algorithm

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

1 cv::Mat Histogram :: BuildHistogram(cv::Mat srcImage){

2 cv::Mat histMat;

3 cv:: calcHist (&srcImage , 1, 0, cv::Mat(), _histMat , 1, &_histSize ,

&_histRange , true , false);

4 histMat = _histMat.clone ();

5 return histMat;

6 }

The method getLowerUpperThresh accepts the input image ,lower andupper ration and returns pixel intensities correponding to lower andupper ration.

1 float N = image.rows * image.cols;

2 float maxth = (1-s2)*N;

3 float minth = s1*N;

4 float cmini =0;

5 float cmaxi=N;

6 for (int i = 0; i < histMat.rows; i++){

7 cmini += histMat.at<float >(i);

8 cmaxi -= histMat.at <float >(256-i);

9 if(cmini >= minth && lower==true){

10 mini = i;

11 lower=false;

12 }

13 if(cmaxi <= maxth && higher ==true){

14 maxi = 256-i;

15 higher=false;

16 }

17 if(lower== false && higher == false )

18 break;

Based on this updated lower and upper threshold values the con-trast stretching is performed

1 Histogram imgHist;

2 std::vector <int > imgThresh;

3 imgThresh = imgHist.getThresh(_channel[i],s1[i],s2[i]);

4 cv:: minMaxLoc (_channel[i],&_min1 ,&_max1 ,0,0);

5 for(int k=0;k <256;k++)

6 _LUT[k*_channels+i]=k-imgThresh [0] <0?0:k>imgThresh [1]?255:(255*(k-

_min[i])/(_max[i]-_min[i]));

9 | 11

Page 10: A Look At Contrast Stretching

Look At Contrast Stretching Algorithm

0.5 Modfied Contrast Stretching Version 2

Another issue with contrast stretching is that if the input imageoccupies the pixels in the range (0; 255) and we apply the contraststretching on a good contrast image using a modified linear trans-formation function,it may not necessarily leads to a improved image.

It may be better to retain the original image. The maximum andminimum pixels intensities in the image are computed. The maximumintensity must be above a specified threshold if we are to applymodified transformation.By default the value is set to 70;

Another Contraint we apply is that max value for threshold beabove atleast threshold/2;

These thresholds will depend on application being considered

0.6 Codewe define a class ContrastStretching it supports mode as DEFAULT forstandard contrast stretching and V1 for modified contrast stretch-ing. 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

Page 11: A Look At Contrast Stretching

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