# Apply getPerspectiveTransform and warpPerspective for bird-eye view (Python).

Hi, I'm following some tutorials to change an image of a golf green with balls to bird-eye view to measure distances in a later step.

Now when I apply the transformation to an image with some text on paper it seems to work, but when applied to the outdoor image the results are not as expected.

Here an example with the outdoor image coords and dimensions:

# targeted rectangle on original image which needs to be transformed
tl = [689, 892]
tr = [2518, 892]
br = [2518, 2071]
bl = [689, 2071]

corner_points_array = np.float32([tl,tr,br,bl])

# original image dimensions
width = 4128
height = 2322

# Create an array with the parameters (the dimensions) required to build the matrix
imgTl = [0,0]
imgTr = [width,0]
imgBr = [width,height]
imgBl = [0,height]
img_params = np.float32([imgTl,imgTr,imgBr,imgBl])

# Compute and return the transformation matrix
matrix = cv2.getPerspectiveTransform(corner_points_array,img_params)
img_transformed = cv2.warpPerspective(image,matrix,(width,height))


And here are my results for the golf image:

## output

As you can see I don't get a nice bird-eye view.

This is the result I get with text and paper using the same script just with different coords and dimensions:

## output

So what am I doing wrong with the first golf example? Any help would be greatly appreciated.

edit retag close merge delete

Sort by » oldest newest most voted

WarpPerpective will transform a rectangle seen in perspective (like a trapeziod) into a real rectangle.

For it to work, you have to define correctly the corners.

However in the golf image you map a rectangle onto a rectangle, so it won't deform it.

Take care: WarpPerspective works correctly only on planar rectangles. However the golf balls are not planar. You might get elongated golf balls, and determining correctly the real position won't be easy.

more

Hey, thx for your answer, I think I understand. But how do I know where to put my trapezoid corner points in the golf example in order to get a correct top down view?

( 2020-07-27 09:18:35 -0500 )edit
1

You'll need some markers for calibration. Either add some colored points to the field that you can use for calibration of every image, or use a fixed camera, do the calibration once, then remove the points and use the same calibration data.

If there would be some specific features on the ground, you could use a SIFT-based matching with an aerial image, but on a uniform background like the lawn it's impossible to get manually the correct perspective points.

( 2020-07-27 09:27:11 -0500 )edit

With colored points you mean for example 4 physical red balls forming a rectangle lying on the lawn for each image (perfect rectangle?)? Then in each image I take those 4 balls as my corner coords? Sorry for all the questions but I started with computer vision just a week ago. I'm trying to build an application, which should transform each "golf" image taken to bird-eye view. Images will just be taken by the users mobile phone and won't be fixed. Isn't there any solution without any extra calibration points (Just the image as is)? Your help is much appreciated.

( 2020-07-27 09:41:28 -0500 )edit
1

Yes, the best solution would be to use 4 visible and fixed red balls or points. I also like to use ARUCO markers for calibration (in other kind of applications) - they are very robust.

The other solution would be to use automatically detected keypoints (see this tutorial), but on that lawn it would be difficult to get any kind of robust keypoint.

( 2020-07-27 09:59:26 -0500 )edit

Official site

GitHub

Wiki

Documentation