Inactive Tutor answered 09/06/23
Tutor
New to Wyzant
import cv2
import numpy as np
from skimage import data
import matplotlib.pyplot as plt
def display_image_from_array(image_array, title='Image', cmap=None):
"""
Display an image from a given numpy array without altering the color.
Parameters
----------
image_array : numpy.ndarray
The image data. Shape (height, width, 3) for color images or (height, width) for grayscale.
title : str, optional
Title for the image window.
cmap : str, optional
Colormap for grayscale images.
"""
if cmap:
plt.imshow(image_array, cmap=cmap)
else:
plt.imshow(image_array)
plt.title(title)
plt.axis('off')
plt.show()
def gamma_correction(image, gamma=1.0):
"""
Apply Gamma Correction to the input image.
Parameters
----------
image : numpy.ndarray
The input image data. Shape (height, width, 3).
gamma : float, optional
The gamma value to adjust the image brightness.
> 1: darken, < 1: brighten, = 1: no change.
Returns
-------
numpy.ndarray
The gamma-corrected image.
"""
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
corrected = cv2.LUT(image, table)
return corrected
# Reload the astronaut image from scikit-image for demonstration
image = data.astronaut()
# Display the original image correctly
display_image_from_array(image, 'Original Image')
# Apply and display the Gamma Correction technique correctly
gamma_corrected_image = gamma_correction(image, 2)
display_image_from_array(gamma_corrected_image, 'Gamma Corrected Image')