How can I merge two images using OpenCV?
You can combine two images using OpenCV by using the cv2.addWeighted() function, which blends the two images together based on a specified weight.
Below is an example code that blends two images with weights of 0.7 and 0.3.
import cv2
# 读取两个图片
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
# 调整img2的尺寸与img1相同
img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))
# 设置混合权重
alpha = 0.7
beta = 0.3
# 混合图片
blended = cv2.addWeighted(img1, alpha, img2, beta, 0)
# 显示混合结果
cv2.imshow('Blended Image', blended)
cv2.waitKey(0)
cv2.destroyAllWindows()
Please make sure to replace image1.jpg and image2.jpg with the actual image paths. Adjusting the values of alpha and beta can control the blending effect, where alpha represents the weight of img1 and beta represents the weight of img2.