First time here? Check out the FAQ!

Ask Your Question
0

Using getPerspective to warp image to forward facing

asked Mar 22 '18

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?

Preview: (hide)

Comments

  • your image is 610x618 here, so your coords are all way off
  • please read docs !
berak gravatar imageberak (Mar 22 '18)edit

1 answer

Sort by » oldest newest most voted
1

answered Mar 22 '18

berak gravatar image

updated Mar 24 '18

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

Preview: (hide)

Comments

can you explain warped and how it's working?

Akhil Patel gravatar imageAkhil Patel (May 12 '18)edit

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

berak gravatar imageberak (May 12 '18)edit

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

berak gravatar imageberak (May 12 '18)edit

Question Tools

1 follower

Stats

Asked: Mar 22 '18

Seen: 559 times

Last updated: May 12 '18