Lighting Condition Robustness
Opened this issue · 0 comments
Ishaan-Datta commented
Histogram equalization can help improve the contrast of an image, which can be beneficial for object detection in varying lighting conditions.
import cv2
def preprocess_image(image):
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply histogram equalization
equalized = cv2.equalizeHist(gray)
return equalized`
Gamma correction adjusts the brightness of an image, which can help compensate for variations in lighting conditions.
import cv2
import numpy as np
def gamma_correction(image, gamma=1.0):
# Build a lookup table mapping the pixel values [0, 255] to their adjusted gamma values
inv_gamma = 1.0 / gamma
table = np.array([((i / 255.0) ** inv_gamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
# Apply gamma correction using the lookup table
corrected = cv2.LUT(image, table)
return corrected
Adaptive thresholding can be useful for segmenting objects from the background in images with varying lighting conditions.
import cv2
def adaptive_thresholding(image):
# Convert image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply adaptive thresholding
thresholded = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
return thresholded
Color normalization techniques can help reduce the impact of varying lighting conditions on color-based features in object detection.
import cv2
def color_normalization(image):
# Convert image to LAB color space
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
# Compute mean and standard deviation of the L channel
l_mean, l_std = cv2.meanStdDev(lab[:, :, 0])
# Normalize the L channel
normalized_l = (lab[:, :, 0] - l_mean) / l_std
# Merge normalized L channel with original AB channels
normalized_lab = cv2.merge([normalized_l, lab[:, :, 1], lab[:, :, 2]])
# Convert back to BGR color space
normalized_image = cv2.cvtColor(normalized_lab, cv2.COLOR_LAB2BGR)
return normalized_image
You can combine multiple preprocessing techniques to further enhance the robustness of your object detection system against varying lighting conditions.
def preprocess_image(image):
# Apply a combination of preprocessing techniques
equalized = cv2.equalizeHist(image)
gamma_corrected = gamma_correction(equalized, gamma=1.2)
thresholded = cv2.adaptiveThreshold(gamma_corrected, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
return thresholded