Contrast Stretching

In the previous blog, we discussed the meaning of contrast in image processing, how to identify low and high contrast images and at last, we discussed the cause of low contrast in an image. In this blog, we will learn about the methods of contrast enhancement.

Below figure summarizes the Contrast Enhancement process pretty well.

Source: OpenCV

Depending upon the transformation function used, Contrast Enhancement methods can be divided into Linear and Non-Linear.

The linear method includes Contrast-Stretching transformation that uses Piecewise Linear functions while Non-linear method includes Histogram Equilisation, Gaussian Stretch etc. which uses Non-Linear transformation functions that are obtained automatically from the histogram of the input image.

In this blog, we will discuss only the Linear methods. Rest we will discuss in the next blogs.

Contrast stretching as the name suggests is an image enhancement technique that tries to improve the contrast by stretching the intensity values of an image to fill the entire dynamic range. The transformation function used is always linear and monotonically increasing.

Below figure shows a typical transformation function used for Contrast Stretching.

By changing the location of points (r1, s1) and (r2, s2), we can control the shape of the transformation function. For example,

  1. When r1 =s1 and r2=s2, transformation becomes a Linear function.
  2. When r1=r2, s1=0 and s2=L-1, transformation becomes a thresholding function.
  3. When (r1, s1) = (rmin, 0) and (r2, s2) = (rmax, L-1), this is known as Min-Max Stretching.
  4. When (r1, s1) = (rmin + c, 0) and (r2, s2) = (rmax – c, L-1), this is known as Percentile Stretching.

Let’s understand Min-Max and Percentile Stretching in detail.

In Min-Max Stretching, the lower and upper values of the input image are made to span the full dynamic range. In other words, Lower value of the input image is mapped to 0 and the upper value is mapped to 255. All other intermediate values are reassigned new intensity values according to the following formulae

Sometimes, when Min-Max is performed, the tail ends of the histogram becomes long resulting in no improvement in the image quality. So, it is better to clip a certain percentage like 1%, 2% of the data from the tail ends of the input image histogram. This is known as Percentile Stretching. The formulae is same as Min-Max but now the Xmax and Xmin are the clipped values.

Let’s understand Min-Max and Percentile Stretching with an example. Suppose we have an image whose histogram looks like this

Clearly, this histogram has a left tail with few values(around 70 to 120). So, when we apply Min-max Stretching, the result looks like this

Clearly, Min-Max stretching doesn’t improve the results much. Now, let’s apply Percentile Stretching

As we clipped the long tail of input histogram, Percentile stretching produces much superior results than the Min-max stretching.

Let’s see how to perform Min-Max Stretching using OpenCV-Python

For a color image, either change it into greyscale and then apply contrast stretching or change it into another color model like HSV and then apply contrast stretching on V. For percentile stretching, just change the min and max values with the clipped value. Rest all the code is the same.

So, always plot histogram and then decide which method to follow. Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

3 thoughts on “Contrast Stretching

  1. Abhiram Natarajan

    Inefficiency – calculate the min and max only 1 time, else its a wasteful computation for larger images.

    Reply

Leave a Reply