I am trying to create a function using OpenCV that overlays an image onto a defined polygon (using 4 points) in another image. How can I handle the rotation of the image so that the overlay looks natural?
Example:
enter image description here
My code:
def photo_editor(original_image, new_ad, coords_array, image_id):
ordered_coords = order_points(coords_array)
new_ad_h, new_ad_w = new_ad.shape[:2]
src_points = np.float32([[0, 0], [new_ad_w, 0], [new_ad_w, new_ad_h], [0, new_ad_h]])
dst_points = ordered_coords
xmin, ymin = np.min(ordered_coords, axis=0)
xmax, ymax = np.max(ordered_coords, axis=0)
target_region = original_image[int(ymin):int(ymax), int(xmin):int(xmax)]
target_brightness, target_contrast = calculate_brightness_contrast(target_region)
adjusted_image = adjust_brightness_contrast(new_ad, target_brightness, target_contrast)
M = cv2.getPerspectiveTransform(src_points, dst_points)
warped_image = cv2.warpPerspective(adjusted_image, M, (original_image.shape[1], original_image.shape[0]))
mask = np.zeros_like(original_image, dtype=np.uint8)
cv2.fillConvexPoly(mask, np.int32(dst_points), (255, 255, 255))
mask_path = os.path.join(OUTPUT_IMAGE_PATH, f"{image_id}_mask.jpg")
cv2.imwrite(mask_path, mask)
inverse_mask = cv2.bitwise_not(cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY))
roi = cv2.bitwise_and(original_image, original_image, mask=inverse_mask)
roi_path = os.path.join(OUTPUT_IMAGE_PATH, f"{image_id}_roi.jpg")
cv2.imwrite(roi_path, roi)
warped_image_gray = cv2.cvtColor(warped_image, cv2.COLOR_BGR2GRAY)
_, mask_warped = cv2.threshold(warped_image_gray, 1, 255, cv2.THRESH_BINARY)
warped_image_masked = cv2.bitwise_and(warped_image, warped_image, mask=mask_warped)
warped_path = os.path.join(OUTPUT_IMAGE_PATH, f"{image_id}_warped.jpg")
cv2.imwrite(warped_path, warped_image_masked)
result_image = cv2.add(roi, warped_image_masked)
fileName = f"{image_id}.jpg"
filePath = os.path.join(OUTPUT_IMAGE_PATH, fileName)
cv2.imwrite(filePath, result_image)
coords_array = np.array([
[int(json_data.get("up_left_x")), int(json_data.get("up_left_y"))],
[int(json_data.get("up_right_x")), int(json_data.get("up_right_y"))],
[int(json_data.get("bottom_right_x")), int(json_data.get("bottom_right_y"))],
[int(json_data.get("bottom_left_x")), int(json_data.get("bottom_left_y"))]
], dtype=np.float32)
I tried writing various functions to calculate the angle or slope using the points without success.
New contributor
Melody is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.