Lab Question 1: Image Blending with Sinusoidal Masks

Task:

  1. Load two images from the given URLs: Lenna and Boat.
  2. Generate a sinusoidal mask (only x-axis) for the Lenna image.
  3. Generate a complementary sinusoidal mask (1 - sin) for the Boat image.
  4. Apply these masks to their respective images and add the two masked images together.
  5. Display the original images, the sinusoidal masks, and the resulting blended image.
  6. Plot and display histograms of the blended image for both RGB and HSV color spaces.

Helper Functions: Please check the previous one

import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import requests
from io import BytesIO
# Load images from URLs
!wget <https://raw.githubusercontent.com/yyhtbs-yye/data4wget/main/images/image_proc_lenna.png>
!wget <https://raw.githubusercontent.com/yyhtbs-yye/data4wget/main/images/image_proc_boats.png>
# Convert images to numpy arrays
lenna_image = Image.open("image_proc_lenna.png")
boats_image = Image.open("image_proc_boats.png")

lenna_image = np.array(lenna_image)
boats_image = np.array(boats_image)
# Generate sinusoidal mask for Lenna image
ff = 10

H, W, _ = lenna_image.shape
x = np.linspace(0, ff * 2 * np.pi, W)
y = np.ones((H))
X, Y = np.meshgrid(x, y)
sin_mask = (np.sin(X) * Y)
lenna_image_float = uint82float(lenna_image)
boats_image_float = uint82float(boats_image)
# Normalize the mask between 0 and 1
sin_mask_normalized = (sin_mask - sin_mask.min()) / (sin_mask.max() - sin_mask.min())

# Generate complementary mask (1 - sin_mask) for the Boat image
complementary_mask = 1 - sin_mask_normalized

# Convert the masks to 3 channels (RGB)
sin_mask_rgb = np.stack([sin_mask_normalized] * 3, 2)
complementary_mask_rgb = np.stack([complementary_mask] * 3, 2)
# Apply masks to the images (element-wise multiplication)
lenna_masked = lenna_image_float * sin_mask_rgb
boats_masked = boats_image_float * complementary_mask_rgb

# Add the two masked images together
blended_image_float = lenna_masked + boats_masked

# Convert the result back to uint8 format
blended_image = float2uint8(blended_image_float)
# Plot the original images, masks, and the blended image
plt.figure(figsize=(15, 3))

# Original Lenna image
plt.subplot(1, 5, 1)
plt.title("Original Lenna")
plt.imshow(lenna_image)

# Original Boat image
plt.subplot(1, 5, 2)
plt.title("Original Boats")
plt.imshow(boats_image)

# Sinusoidal mask for Lenna
plt.subplot(1, 5, 3)
plt.title("Sinusoidal Mask for Lenna")
plt.imshow(sin_mask_normalized, cmap='gray')

# Complementary mask for Boat
plt.subplot(1, 5, 4)
plt.title("Complementary Mask for Boat")
plt.imshow(complementary_mask, cmap='gray')

# Blended image
plt.subplot(1, 5, 5)
plt.title("Blended Image")
plt.imshow(blended_image)

plt.tight_layout()
plt.show()

image.png

Lab Question 2: Image Blending with Sinusoidal-Sinusoidal Masks

Task: