User:Nehagode/Constrast Stretching

Contrast Stretching is a technique of image processing. It is an image enhancement technique that attempts to improve the contrast in an image by `stretching' the range of intensity values it contains to span a desired range of values, e.g. the the full range of pixel values that the image type concerned allows.

How it Works


Before the stretching can be performed it is necessary to specify the upper and lower pixel value limits over which the image is to be normalized. Often these limits will just be the minimum and maximum pixel values that the image type concerned allows. For example, for 8-bit gray level images, the lower and upper limits might be 0 and 255. Call the lower and the upper limits a and b respectively.

The simplest sort of normalization then scans the image to find the lowest and highest pixel values currently present in the image. Call these c and d. Then each pixel P is scaled using the following function:"$P_{out} = (P_{in} - c) (\frac{b-a}{d-c}) + a$"

Contrast Stretching in MATLAB
To apply linear contrast stretch to an image, we try to map our intensities [xmin, xmax] in image pixels 0 to 255 range which is done by our function histogramlinear. This function will receive the image that we are trying to do contrast stretching on. An example code in MATLAB is given below:

How to obtain the function for contrast stretching?
Suppose we are asked to obtain the gray level transformation function that stretches gray scale range [0, 50] into [0, 100], stretches the range [50, 80] into [100, 150] shifts range [80, 130] to [150, 200] and compresses the range [130, 250] into [200, 250].

Let the resulting function be S = $$\begin{bmatrix} \alpha r & 0< r \leq a \\ S1+\beta (r-a) & a < r \leq b \\ S2 +\gamma (r-b) & b < r \leq c \\ S3 +\delta (r-c) & c < r \leq L-1 \end{bmatrix}$$ where

r is the pixel value, a = 50, b = 80, c = 130, L-1 = 250, S1 = 100, S2 = 150, S3 = 200.

Obtaining the constants using the formula for calculating slope,

$$\alpha = \frac{100 - 0}{50 - 0} = 2 $$

$$\beta = \frac{150 - 100}{80 - 50} = \frac{50}{30} = 1.67 $$

$$\gamma = \frac{200 - 150}{130 - 80} = \frac{50}{50} = 1 $$

$$\delta = \frac{250 - 200}{250 - 130} = \frac{50}{120} = 0.42 $$

now substituting all the constants we get the gray level transformation function as:

S = $$\begin{bmatrix} 2r & 0< r \leq 50 \\ 100+ 1.67(r-50) & 50 < r \leq 80 \\ 150 +(r-b) & 80 < r \leq 130 \\ 200 +0.42 (r-130) & 130 < r \leq 250 \end{bmatrix}$$

We can now apply this function on every pixel value in the image to get the transformed image.

Applying the Transformation Function on an Image
Suppose we are given an input image F= $$\begin{bmatrix} 7 & 12 & 2 & 3 & 4 \\ 10 & 15 & 1 & 6 & 7 \\ 12 & 4 & 6 & 15 & 12 \\ 8 & 2 & 7 & 15 & 2\\ 11 & 13 & 3 & 3 & 5\end{bmatrix}$$ and its gray-level transformation function is given as S = $$\begin{bmatrix} 0.5 r & 0< r \leq 8 \\ 4+ (r-8) &  8< r \leq 12 \\ 8 +2.33 (r-12) & 12 < r \leq 15 \end{bmatrix}$$.

Now, we apply this function to the input image and get the output image A.

Case 1: when $$0 \leq r \leq 8, then \quad S = 0.5r$$

We can see that for F(0,0) r=7,

so A(0,0) = 0.5r = 0.5 x 7 = 3.5

Similarly applying for all values of r that satisfy the condition, we get

F = $$\begin{bmatrix} 3.5 &- &1 & 1.3 & 2 \\ -&- & 0.5 & 3 & 3.5 \\ -& 2 & 3 &- &- \\ 4 & 1 & 3.5 &-&1 \\ -&- & 1.5&1.5&2.5 \end{bmatrix}$$

Case 2: when $$8 < r \leq 12, then \quad S = 4 + (r-8) = r -4$$

Applying for all values of r that satisfy the condition, we get

F = $$\begin{bmatrix} 3.5 &8 &1 & 1.3 & 2 \\ 6&- & 0.5 & 3 & 3.5 \\ 8& 2 & 3 &- &8 \\ 4 & 1 & 3.5 &-&1 \\ 7&- & 1.5&1.5&2.5 \end{bmatrix}$$

Case 3: when $$12 < r \leq 15, then \quad S = 8 + 2.33(r-12) = 2.33r - 19.96 $$

Applying for all values of r that satisfy the condition, we get

F = $$\begin{bmatrix} 3.5 &8 &1 & 1.3 & 2 \\ 6&15 & 0.5 & 3 & 3.5 \\ 8& 2 & 3 &15 &8 \\ 4 & 1 & 3.5 &15&1 \\ 7&10 & 1.5&1.5&2.5 \end{bmatrix}$$

This is the final output image after applying the gray level transformation function.