Lab Question 1: Gamma Correction on the Hue Channel (H)

Task:

  1. Load an image from the given URL.
  2. Convert the image from the RGB color space to the HSV color space.
  3. Apply a positive gamma correction to the Hue (H) channel.
  4. Convert the image back to RGB and display the output image.
  5. Generate and display the histograms for both the RGB and HSV color spaces.

Helper Functions:

Codes

Main Script:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import requests
from io import BytesIO
!wget <https://raw.githubusercontent.com/yyhtbs-yye/data4wget/main/images/image_proc_lenna.png>
# Load image from URL
image = Image.open("image_proc_lenna.png")
image = np.array(image)
# Convert RGB to HSV
image_hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)

image_hsv_float = uint82float(image_hsv, is_hsv=True)

hue_gamma = 1.5  # Adjust this gamma value for experimentation

# Copy the floating point image as an output initialization
image_hsv_corrected_float = image_hsv_float.copy()

# Apply gamma correction to hue only 
image_hsv_corrected_float[:, :, 0] = (image_hsv_float[:, :, 0] ** hue_gamma)  

image_hsv_corrected = float2uint8(image_hsv_corrected_float, is_hsv=True)

image_rgb_corrected = cv2.cvtColor(image_hsv_corrected, cv2.COLOR_HSV2RGB)
# Plot original and corrected images side by side
plt.figure(figsize=(12, 6))

# Original image
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image)

# Corrected image
plt.subplot(1, 2, 2)
plt.title("Gamma Corrected Image")
plt.imshow(image_rgb_corrected)

plt.show()

image.png

plot_image_histogram_rgb(image)
plot_image_histogram_rgb(image_rgb_corrected)

image.png

image.png

plot_image_histogram_hue(image)
plot_image_histogram_hue(image_rgb_corrected)

image.png