In this lesson we want to learn How to Detect Low Contrast Images with OpenCV and scikit-image, for this we are using OpenCV and scikit-image.
What is OpenCV ?
OpenCV (Open Source Computer Vision Library) is library of computer vision and machine learning algorithms. It is written in C, C++ and Python and is available as an open source project under the BSD license.
OpenCV provides large collection of algorithms for tasks such as image processing, object detection, face recognition and many more. It is widely used in computer vision applications, including real time video processing, robotics and augmented reality. OpenCV also provides interfaces to integrate with other libraries and frameworks such as TensorFlow to extend its functionality.
To install OpenCV in Python, you can use the following pip command:
1 |
pip install opencv-python |
What is Skimage and How to Install ?
Scikit image (skimage) is an image processing Python library that includes algorithms for segmentation, color space conversion, feature detection and many more. It is built on top of NumPy, SciPy and Matplotlib, and is designed to work with 2D images represented as NumPy arrays.
To install scikit-image, you can use the following pip command:
1 |
pip install scikit-image |
Low contrast images can be detected using a combination of OpenCV and scikit image in Python. this is an example of how to do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import cv2 import numpy as np from skimage import exposure # Load the image img = cv2.imread('image.jpg') # Convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Calculate the mean and standard deviation of the image mean, std = cv2.meanStdDev(gray) # Convert the image to float gray = gray.astype(np.float32) # Normalize the image using the mean and standard deviation gray = (gray - mean) / std # Apply the adaptive histogram equalization to improve the contrast gray = exposure.equalize_adapthist(gray, clip_limit=0.03) # Convert the image back to unsigned 8-bit integer gray = (gray * 255).astype(np.uint8) # Check if the contrast of the image is low if np.std(gray) < 50: print("Low contrast image") else: print("Normal image") |
In this example we have first loaded image using OpenCV’s imread function. after that we have converted the image to grayscale using the cvtColor function. Next we calculate the mean and standard deviation of the grayscale image using the meanStdDev function. after that we normalized image using the mean and standard deviation which helps to correct the contrast of the image.
Next we use scikit image’s equalize_adapthist function to apply the adaptive histogram equalization to the image, which improves contrast even further. finally we check if the contrast of the image is low by calculating the standard deviation of the grayscale image and comparing it to a threshold value of 50. If the standard deviation is less than 50, we consider the image to have low contrast. (How to Detect Low Contrast Images with OpenCV and scikit-image)
Learn More on Python
- How to Add Matplotlib in TKinter Window
- How to Make an Instagram Bot
- How to send Message to Instagram with Python
- TKinter Application with Dialogs
- Build Multi Window Application with TKinter
- How to Build Charts in TKinter
- How to Install TKinter in Windows and Linux
- How to Use ChatGPT in Python
- Which Websites are Made with Django