Hello.
So I tried to enhancing a landsat-8 image. I assume that the image given already merged from multichannel to a RGB color, but the ouput is too dark. I’ve tried to brighten the image with this code:
import cv2
import numpy as np
import matplotlib.pyplot as plt
def adjust_brightness_contrast(image, alpha, beta):
return cv2.addWeighted(image, alpha, image, 0, beta)
image = cv2.imread('input.png')
bright_image = adjust_brightness_contrast(image, 3, 40)
original_and_bright_image = np.hstack((image, bright_image))
plt.figure(figsize = [30, 30])
plt.axis('off')
plt.imshow(original_and_bright_image [:,:,::-1])
After that I applied gamma correction to the image
import cv2
import numpy as np
import matplotlib.pyplot as plt
def adjust_brightness_contrast(image, alpha, beta):
return cv2.addWeighted(image, alpha, image, 0, beta)
def gamma_correction(image, gamma=1.0):
# Normalize the image to the range [0, 1]
normalized_image = image / 255.0
# Apply gamma correction
corrected_image = np.power(normalized_image, gamma)
# Rescale the corrected image to [0, 255]
corrected_image = (corrected_image * 255).astype(np.uint8)
return corrected_image
image = cv2.imread('input.png')
bright_image = adjust_brightness_contrast(image, 3, 40)
# Define the gamma value (e.g., gamma=1.5 for brightening)
gamma_value = 1.5
# Apply gamma correction to the bright_image
gamma_corrected_image = gamma_correction(bright_image, gamma_value)
# Stack the images for display
stacked_image = np.hstack((bright_image, gamma_corrected_image))
plt.figure(figsize=[20, 10])
plt.axis('off')
plt.imshow(cv2.cvtColor(stacked_image, cv2.COLOR_BGR2RGB))
plt.show()
I have noticed that it’s getting better, clouds are now white, but the other color of the image is not detailed enough (Color not popping off) and if you notice the image is kinda hazy(gray layer on top, which kinda make it feels like blurry)
What should I do next after corrected the gamma? I’ve tried to equalized the image, but the result giving me undesired output:
import cv2
import numpy as np
import matplotlib.pyplot as plt
def adjust_brightness_contrast(image, alpha, beta):
return cv2.addWeighted(image, alpha, image, 0, beta)
def gamma_correction(image, gamma=1.0):
# Normalize the image to the range [0, 1]
normalized_image = image / 255.0
# Apply gamma correction
corrected_image = np.power(normalized_image, gamma)
# Rescale the corrected image to [0, 255]
corrected_image = (corrected_image * 255).astype(np.uint8)
return corrected_image
def equalize_histogram(image):
# Convert the image to HSV color space
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Equalize the histogram for each channel (Hue, Saturation, Value)
hsv_image[:, :, 0] = cv2.equalizeHist(hsv_image[:, :, 0]) # Hue
hsv_image[:, :, 1] = cv2.equalizeHist(hsv_image[:, :, 1]) # Saturation
hsv_image[:, :, 2] = cv2.equalizeHist(hsv_image[:, :, 2]) # Value
# Convert back to BGR color space
equalized_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)
return equalized_image
# Load the input image
image = cv2.imread('input.png')
# Adjust brightness and contrast
bright_image = adjust_brightness_contrast(image, 3, 40)
# Define the gamma value
gamma_value = 1.5
# Apply gamma correction to the bright_image
gamma_corrected_image = gamma_correction(bright_image, gamma_value)
# Equalize histogram for the enhanced image
equalized_image = equalize_histogram(gamma_corrected_image)
# Stack the original, gamma corrected, enhanced, and equalized images for display
stacked_image = np.hstack((gamma_corrected_image, equalized_image))
plt.figure(figsize=[20, 10])
plt.axis('off')
plt.imshow(cv2.cvtColor(stacked_image, cv2.COLOR_BGR2RGB))
plt.show()
Here is the reference of expected result: Elegant Figures - How To Make a True-Color Landsat 8 Image