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()