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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import cv2 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[0],img1.shape[1]),dtype = 'uint8') # Loop over the image and apply Min-Max formulae for i in range(img1.shape[0]): for j in range(img1.shape[1]): minmax_img[i,j] = 255*(img1[i,j]-np.min(img1))/(np.max(img1)-np.min(img1)) # Displat the stretched image cv2.imshow('Minmax',minmax_img) cv2.waitKey(0) |
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.