关于合并图像的 python 和 opencv 的问题

question about python and opencv for merge images

我用 python 和 opencv 写了这段代码 我有 2 张图片(第一张是足球比赛的图片 36.jpg):

36.jpg

和(第二个是 pitch.png 一张图像(足球场线(红色)),png 格式 = 没有白色背景):

pitch.png

使用这段代码,我在两张图片中都选择了 4 个坐标点(右侧罚球区的 4 个角) 然后使用 ( cv2.warpPerspective ) 并显示它,我们可以显示来自 (Top View) 的第一张图像 如下:

top view

我的问题是: 我的代码(第二张图片的红色线)在下面的第一张图片上显示相同的图片(我在绘画应用程序中绘制)需要进行哪些更改:

desired

在此先感谢您的帮助

这是我的代码:

import cv2
import numpy as np

if __name__ == '__main__' :

 # Read source image.
 im_src = cv2.imread('c:/36.jpg')
 # Four corners of penalty area in first image
 pts_src = np.array([[314, 108], [693, 108], [903, 493],[311, 490]])

 # Read destination image.
 im_dst = cv2.imread('c:pitch.png')
 # Four corners of right penalty area in pitch image.
 pts_dst = np.array([[480, 76],[569, 76],[569, 292],[480, 292]])

 # Calculate Homography
 h, status = cv2.findHomography(pts_src, pts_dst)

 # Warp source image to destination based on homography
 im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0]))

 # Display images
 cv2.imshow("Source Image", im_src)
 cv2.imshow("Destination Image", im_dst)
 cv2.imshow("Warped Source Image", im_out)

 cv2.waitKey(0)

交换源和目标图像和点。然后,扭曲源图像:

im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0]), borderValue=[255,255,255])

并添加此代码

mask = im_out[:,:,0] < 100

im_out_overlapped = im_dst.copy()
im_out_overlapped[mask] = [0,0,255]