Tag Archives: contrast in image processing

Detecting low contrast images using Scikit-image

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.

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.

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.

What is Contrast in Image Processing?

According to Wikipedia, Contrast is the difference in luminance or color that makes an object distinguishable from other objects within the same field of view.

Take a look at the images shown below

Source: OpenCV

Clearly, the left image has a low contrast because it is difficult to identify the details present in the image as compared to the right image.

A real life example can be of a sunny and a foggy day. On a sunny day, everything looks clear to us, thus has a high contrast, as compared to a foggy day, where everything looks nearly of the same intensity (dull, washed-out grey look).

A more valid way to check whether an image has a low or high contrast is to plot the image histogram. Let’s plot the histogram for the above images

Clearly, from the left image histogram, we can see that the image intensity values are located in a narrow range. Because it’s hard to distinguish nearly the same intensity values (See below figure, 150 and 148 are hard to distinguish as compared to 50 and 200), thus the left image has low contrast.

The right histogram increases this gap between the intensity values and Whoo! the details in the image are now much more perceivable to us and thus yields a high contrast image.

So, for the high contrast, the image histogram should span the entire dynamic range as shown above by the right histogram. In the next blogs, we will learn different methods to do this.

There is another naive approach where we subtract the max and min intensity values and based on this difference we judge the image contrast. I will not recommend following this as this may get affected by the outliers (we will discuss in the next blogs). So, always plot the histogram to check.

Till now, we discussed contrast but we didn’t discuss the cause of low contrast images.

Low contrast images can result from Poor illumination, lack of dynamic range in the imaging sensor or even wrong setting of lens aperture during image acquisition etc.

When performing Contrast enhancement, you must first decide whether you want to do global or local contrast enhancement. Global means increasing the contrast of the whole image, While in local we divide the image into small regions and perform contrast enhancement on these regions independently. Don’t Worry, we will discuss these in detail in the next blogs.

This concept has been beautifully illustrated by the figure shown below( Taken from OpenCV Documentation)

Original Image

Clearly, on global enhancement, the details present on the face of the statue are lost. While these are preserved in the local enhancement. So you need to be careful when selecting these methods.

In the next blog, we will discuss the methods used to transform a low contrast image into a high contrast image. 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.