In the previous blog, we discussed what is contrast in image processing and how histograms can help us distinguish between low and high contrast images. If you remember, for a high contrast image, the histogram spans the entire dynamic range while for low contrast the histogram covers only a narrow range as shown below
So, just by looking at the histogram of an image, we can tell whether this has low or high contrast.
Problem
But what if you have a large number of images such as in computer vision when training a model. In that case, we generally want to remove these low contrast images as they don’t provide us enough knowledge about the task. But manually examining the histogram of each image will be a tedious and time-consuming task. So, we need to find a way to automate this process.
Solution
Luckily, scikit-image provides a built-in function is_low_contrast() that determines if an image is a low contrast or not. This function returns a boolean where True indicates low contrast. Below is the syntax of this function.
1 |
skimage.exposure.is_low_contrast(image, fraction_threshold=0.05, lower_percentile=1, upper_percentile=99, method='linear') |
Below is the algorithm that this function uses
- First, this function converts the image to greyscale
- Then this disregards the image intensity values below lower_percentile and above upper_percentile. This is similar to percentile stretching that we did earlier (See here)
- Then this calculate the full brightness range for a given image datatype. For instance, for 8-bit, the full brightness range is [0,255]
- Finally, this calculates the ratio of image brightness range and full brightness range. If this is less than a set threshold (see fraction_threshold argument above), then the image is considered low contrast. For instance, for a 8-bit image if the image brightness range is [100-150] and the threshold is 0.1 then the ratio will be 50/255 that is 0.19 approx. So, this image is having a high contrast. You need to change this threshold according to your application
I hope you understood this. Now, let’s take an example and see how to implement this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import cv2 from skimage.exposure import is_low_contrast # Read the image img = cv2.imread('D:/downloads/stretch_original.jpg') # Check if it is low contrast or not out = is_low_contrast(img, fraction_threshold=0.3) # if true print low contrast otherwise high contrast if out: print('image has low contrast') else: print('image has high contrast') |
So, for the below image, this function outputs ‘image has low contrast’ corresponding to the given threshold.
I hope you understood this. Now, in the pre-processing step, you can check whether the image has high or low contrast and then take action accordingly. Hope you enjoy reading.
If you have any doubts/suggestions please feel free to ask and I will do my best to help or improve myself. Goodbye until next time.