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.
Depending upon the transformation function used, Contrast
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,
- When r1 =s1 and r2=s2, transformation becomes a Linear function.
- When r1=r2, s1=0 and s2=L-1, transformation becomes a thresholding function.
- When (r1, s1) = (rmin, 0) and (r2, s2) = (rmax, L-1), this is known as Min-Max Stretching.
- 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
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
import numpy as np
# Read the image
img1 = cv2.imread('D:/downloads/contrast.PNG',0)
# Create zeros array to store the stretched image
minmax_img = np.zeros((img1.shape,img1.shape),dtype = 'uint8')
# Loop over the image and apply Min-Max formulae
for i in range(img1.shape):
for j in range(img1.shape):
minmax_img[i,j] = 255*(img1[i,j]-np.min(img1))/(np.max(img1)-np.min(img1))
# Displat the stretched image
For a color image, perform this for each channel(Just add another ‘for’ loop for channels). 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.