In this article, we are going to see how to detect low contrast images with OpenCV, scikit-image using Python
A low contrast image has the minimal distinction between light and dark parts, making it difficult to tell where an object’s boundary begins and the scene’s background begins. For example
The left one is low contrast and the right one is a high contrast image. Using low contrast image detection, you can programmatically detect images that are not sufficient for your image processing pipeline.
First Let’s see how we can manually see the contrast of a picture using histograms. The histogram for a high contrast image(Right) spans the entire dynamic range, but the histogram for a low contrast image(Left) just covers a narrow range, as seen below.
Now let’s do it programmatically using the is_low_contrast method of scikit-image library.
Approach:
- Import all the libraries needed.
- Read the image in grayscale mode.
- Check if image is low contrast image or high contrast image using some threshold value provided.
Syntax: is_low_contrast(img,”threshold value”)
Example 1: Low contrast Image
Input Image:
Code:
Python3
import cv2 from skimage.exposure import is_low_contrast img = cv2.imread( "low_contrast_img(1).jpg" ) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if (is_low_contrast(gray, 0.35 )): cv2.putText(img, "low contrast image" , ( 5 , 25 ), cv2.FONT_HERSHEY_SIMPLEX, 0.8 , ( 0 , 0 , 0 ), 2 ) else : cv2.putText(img, "high contrast image" , ( 5 , 25 ), cv2.FONT_HERSHEY_SIMPLEX, 0.8 , ( 0 , 0 , 0 ), 2 ) cv2.imshow( "output" ,img) cv2.imwrite( "output.jpg" ,img) cv2.waitKey( 0 ) # closing all open windows cv2.destroyAllWindows() |
Output:
Example 2: High Contrast Image
Input:
Code:
Python3
import cv2 from skimage.exposure import is_low_contrast img = cv2.imread( "high contrast image(2).jpg" ) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if (is_low_contrast(gray, 0.35 )): cv2.putText(img, "low contrast image" , ( 5 , 25 ), cv2.FONT_HERSHEY_SIMPLEX, 0.8 , ( 0 , 0 , 0 ), 2 ) else : cv2.putText(img, "high contrast image" , ( 5 , 25 ), cv2.FONT_HERSHEY_SIMPLEX, 0.8 , ( 0 , 0 , 0 ), 2 ) cv2.imshow( "output" ,img) cv2.imwrite( "output.jpg" ,img) cv2.waitKey( 0 ) # closing all open windows cv2.destroyAllWindows() |
Output:
You can further find the contours on high contrast images for image preprocessing and extract the object from the image.