首页 > 解决方案 > 关于合并图像的python和opencv的问题

问题描述

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

36.jpg

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

音高.png

使用此代码,我在两个图像(右侧罚球区的 4 个角)中选择了 4 个坐标点,然后使用(cv2.warpPerspective)并显示它,我们可以显示(顶视图)中的第一张图像,如下所示:

顶视图

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

想要的

在此先感谢您的帮助

这是我的代码:

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)

标签: pythonopencvimage-processing

解决方案


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

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]

重叠扭曲图像


推荐阅读