如何使用单应性将图像投影到另一图像上

时间:2019-06-14 09:16:22

标签: python opencv

我有一个python程序,可从捕获的视频中检测矩形。现在,我想将另一个图像投影到检测到的正方形中(就像在video中一样)。

我尝试使用warpPerspective,但似乎没有用,或者我使用的方式有误。

我当前的输出看起来是like this。我希望输出看起来像like this

1 个答案:

答案 0 :(得分:1)

使用warpPerspective后,我尝试覆盖图像:

img = cv2.imread('cola.jpg')
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
background = cv2.imread('stadium.jpg')
background = cv2.cvtColor(background,cv2.COLOR_BGR2RGB)

rows,cols,ch = background.shape

pts1 = np.float32([[0,0],[974,0],[0,974],[974,974]]) # cola coords 
pts2 = np.float32([[560,383],[940, 516],[5,527],[298,733]]) # stadium tile coords

M = cv2.getPerspectiveTransform(pts1,pts2)    
dst = cv2.warpPerspective(img,M,(cols,rows))

overlay = cv2.add(background, dst)

[输出图像

Output image

我使用了OpenCV documentation