import cv2 import numpy as np def insert_person(left_image_path, right_image_path, person_image_path, depth="medium"): # Load left and right stereoscopic images left_image = cv2.imread(left_image_path) right_image = cv2.imread(right_image_path) # Load the segmented person image with alpha channel (transparency) person = cv2.imread(person_image_path, cv2.IMREAD_UNCHANGED) # Define scaling and disparity values for each depth level depth_settings = { "close": { "scale": 1.2, "disparity": 15, }, # Larger size and greater disparity for closer placement "medium": { "scale": 1.0, "disparity": 10, }, # Moderate size and disparity for medium placement "far": { "scale": 0.7, "disparity": 5, }, # Smaller size and lesser disparity for farther placement } # Retrieve scale and disparity based on depth level scale_factor = depth_settings[depth]["scale"] disparity = depth_settings[depth]["disparity"] # Resize the person image based on the scale factor person_h, person_w = person.shape[:2] new_size = (int(person_w * scale_factor), int(person_h * scale_factor)) person_resized = cv2.resize(person, new_size, interpolation=cv2.INTER_AREA) # Determine the positions for placing the person in left and right images # (You may adjust these values to match specific surfaces or alignments in your background) left_x, left_y = ( 50, left_image.shape[0] - person_resized.shape[0] - 50, ) # Place near bottom for realism right_x = left_x + disparity # Horizontal offset for depth effect # Overlay the person onto both left and right images with adjusted position for img, x in zip((left_image, right_image), (left_x, right_x)): # Ensure the person fits within the bounds of the image y, x = max(0, left_y), max(0, x) y_end = min(y + person_resized.shape[0], img.shape[0]) x_end = min(x + person_resized.shape[1], img.shape[1]) # Extract the region of interest (ROI) roi = img[y:y_end, x:x_end] # Apply alpha blending to combine person image with background person_alpha = ( person_resized[: y_end - y, : x_end - x, 3] / 255.0 ) # Alpha channel mask for c in range(3): roi[:, :, c] = (1 - person_alpha) * roi[ :, :, c ] + person_alpha * person_resized[: y_end - y, : x_end - x, c] # Insert modified ROI back into the original image img[y:y_end, x:x_end] = roi # Return the modified left and right images return left_image, right_image