warpPerspective but inverse homography

hi guys
i wrote this code by python and opencv i have 2 images (first is an image from football match 36.jpg) :

and (second is pitch.png an image (Lines of football field (Red Color)) with png format = without white background) :

with this code , i selected 4 coordinates points in both of 2 images (4 corners of right penalty area) and then with ( cv2.warpPerspective ) and show it , we can show that first image from (Top View) as below:

my question is this: what changes need in my code that (red color Lines of second image) show on first image same below images (that i draw in paint app):

thanks in advance , for your help

this is my code :

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)


you have H, the homography, and you already use warpPerspective.

you can either invert the homography explicitly using np.linalg.inv(), or you can pass WARP_INVERSE_MAP as a flag to warpPerspective.

also pass the line drawing instead of the photo.

that should warp the line drawing to match the photo.

to overlay the line drawing (which has transparency?), here’s a recipe: function for compositing two images that is aware of alpha-channel · Issue #20780 · opencv/opencv · GitHub

and will you kindly either link to your crossposts or stop crossposting?

I would also recommend that you stop deleting your questions after they’ve been answered on SO and stick to a single account.

previous thread:

ok, i got it , thanks dear christoph, i used from your solution in last my question

and i will ask new question at here for automatically find keypoint