Ask Your Question
0

Using getPerspective to warp image to forward facing

asked 2018-03-22 02:11:57 -0600

charles1208 gravatar image

I have an image that I want to stretch to cover the entire screen after transformation.

My code is as such:

src = np.array([[0, 0], [997, 102], [1000, 600], [0, 995]], np.float32)
dst = np.array([[0, 0], [997, 0], [1000, 995], [0, 995]], np.float32)
M = cv2.getPerspectiveTransform(src, dst)
warped = cv2.warpPerspective(img, M, (1000, 1000))

where the src coordinates refer to the 4 red points in the image, in clockwise order.

The dst coordinates refer to the 2 red points on the left, and the 2 blue points on the right of the image, in clockwise order.

The idea is that I want to "stretch" the image such that the 2 red points on the right correspond to the 2 blue points.

enter image description here

However, the warped image turns out to be:

enter image description here

Is there something I did wrong, or assumed wrongly about the usage of the functions?

edit retag flag offensive close merge delete

Comments

  • your image is 610x618 here, so your coords are all way off
  • please read docs !
berak gravatar imageberak ( 2018-03-22 02:36:45 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-03-22 03:19:14 -0600

berak gravatar image

updated 2018-03-24 02:56:44 -0600

your coords are simply wrong. it also gets easier, if you use image measurements, like W and H from your original image:

H=im.shape[0]
W=im.shape[1]
src = np.array([[32, 25], [585,81], [585,356], [41,570]], np.float32)
dst = np.array([[0, 0],   [W, 0],   [W, H],    [0, H]], np.float32)
M = cv2.getPerspectiveTransform(src, dst)
warped = cv2.warpPerspective(im, M, (W, H))

image description

edit flag offensive delete link more

Comments

can you explain warped and how it's working?

Akhil Patel gravatar imageAkhil Patel ( 2018-05-12 01:53:17 -0600 )edit

@Akhil Patel please do not post answers here, if you have a question or comment, thank you.

berak gravatar imageberak ( 2018-05-12 02:22:07 -0600 )edit

also, what's unclear above ? and please: read docs

berak gravatar imageberak ( 2018-05-12 02:28:09 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-03-22 02:11:57 -0600

Seen: 546 times

Last updated: May 12 '18