# warpPerspective but inverse homography

**URL:** <https://forum.opencv.org/t/warpperspective-but-inverse-homography/5547>\
**Category:** Python\
**Tags:** calib3d, imgproc\
**Created:** [October 9, 2021, 10:39am UTC](https://forum.opencv.org/t/warpperspective-but-inverse-homography/5547 "2021-10-09T10:39:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![payam\_mohammadi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/payam_mohammadi/32/3335_2.png) [@payam\_mohammadi](https://forum.opencv.org/u/payam_mohammadi)\
**Post date:** [October 9, 2021, 10:39am UTC](https://forum.opencv.org/t/warpperspective-but-inverse-homography/5547/1 "2021-10-09T10:39:29Z")

</div>

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

 ![](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/5/5ad093915755fe23f42e825f36d68377647bd55f.jpeg)

and (second is pitch.png an image (Lines of football field (Red Color)) with png format = without white background) :  
[![](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/9/9fa5500e156d29ebf1cb7ca67bcb80a9cd98368a.jpeg) ](https://i.stack.imgur.com/vgbkE.jpg%5B%5E%5D)

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:  
[![](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/c/ce08318432213dea782ab1706ae5cf2164a92562.jpeg) ](https://i.stack.imgur.com/juZ3u.jpg%5B%5E%5D)

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):

[![](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/5/5643883857b7af745f37d3ff7b351f71dc4a5fec.jpeg) ](https://i.stack.imgur.com/tarG3.jpg%5B%5E%5D)

thanks in advance , for your help

this is my code :

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [October 9, 2021, 12:17pm UTC](https://forum.opencv.org/t/warpperspective-but-inverse-homography/5547/2 "2021-10-09T12:17:25Z")

</div>

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](https://github.com/opencv/opencv/issues/20780)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [October 9, 2021, 12:56pm UTC](https://forum.opencv.org/t/warpperspective-but-inverse-homography/5547/3 "2021-10-09T12:56:12Z")

</div>

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.

> <https://stackoverflow.com/questions/69504812/question-about-python-and-opencv-for-merge-images>

previous thread:

> [@Find points of 4 corners](https://forum.opencv.org/t/find-points-of-4-corners/5437):
>
> hi guys The below code segment enables user to select 4 corners of penalty area with right click then he can select a place in field to draw a line of offside, same with right click then close with press enter with my app user should select 4 corners of penalty area (manually) I need a correct suggest to app do it automatically (find points of 4 corners of penalty area) (I think it need to find intersection points of white Lines) you can find the image 35.jpg at here thanks this is…

---

<div class="post-metadata">

**Author:** ![payam\_mohammadi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/payam_mohammadi/32/3335_2.png) [@payam\_mohammadi](https://forum.opencv.org/u/payam_mohammadi)\
**Post date:** [October 9, 2021, 1:39pm UTC](https://forum.opencv.org/t/warpperspective-but-inverse-homography/5547/4 "2021-10-09T13:39:32Z")

</div>

> [@crackwitz](#):
>
> would also recommend that you stop deleting your questions after they’ve been answered on SO and stick to a single accoun

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

---

<div class="post-metadata">

**Author:** ![payam\_mohammadi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/payam_mohammadi/32/3335_2.png) [@payam\_mohammadi](https://forum.opencv.org/u/payam_mohammadi)\
**Post date:** [October 9, 2021, 1:46pm UTC](https://forum.opencv.org/t/warpperspective-but-inverse-homography/5547/5 "2021-10-09T13:46:25Z")

</div>

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