0

I am trying to use homography for court detection. And I have detected an object (a player) in the original image and I am now trying to figure out what are it's new coordinates on the wrapped image.

(I am trying to create a minimap system)

I want to figure out new x and y coordinates of the player on the wrapped image

This is my program so far:

import cv2
import numpy as np
import matplotlib.pyplot as plt

#player coordinates - multiple players
x_coords = []
y_coords = []

print(x_coords)
print(y_coords)


#basketball court, size-1830, 910
pts_src = np.array([
    [394, 412], # top left corner
    [0, 500],
    [0, 670],     
    [910, 770],
    [1830, 720],     
    [1830,545],
    [1540, 450]      # top right corner
])

#minimap, size-500,300 
pts_dst = np.array([
    [0, 0],         # top left corner
    [0, 275],
    [25, 300],      
    [250, 300],
    [475, 300],
    [500, 275],     
    [500, 0]       # top right corner
])

#find homography
h, status = cv2.findHomography(pts_src, pts_dst)


img_src2 = cv2.imread('basketball_court.png')
img_out = cv2.warpPerspective(img_src2, h, (img_dst.shape[1], img_dst.shape[0]))

cv2.imshow("Warped", img_out)
cv2.waitKey(0)`

I have tried using this for help but just can't figure out what to do: Opencv homography to find global xy coordinates from pixel xy coordinates

https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#convertpointsfromhomogeneous

  • The homography can be used to project individual points as well as an image. – fmw42 Jan 22 '23 at 17:20
  • You may have to invert the homography matrix for projecting from the input to the output. – fmw42 Jan 22 '23 at 19:14
  • Can you please edit your post, and the images? What is `img_dst`? You are using `img_dst` in your code, but you are not reading it... Can you please post a code sample we can execute? It looks like your 7 points doesn't form a valid transformation - I recommend you to select set of 4 source and destination points that forms a rectangle and make sure the order is correct. After that, you can probably use `cv2.transform(np.array([pts_src], np.float32), h)[0]` (but I am not sure). – Rotem Jan 22 '23 at 22:27
  • The points represent part of an image with the court on (the lines) – gasper vaupot Jan 23 '23 at 12:38
  • And the program does transform successfully, I only want to figure out how to find the new x, y coordinates of a specific part of the previous image – gasper vaupot Jan 23 '23 at 13:41
  • If the question is not good enough, don't expect it to be answered. Believe me that I tried to solve it but I gave up... When asking a question, try not to think only about your current specific problem, try to also think about the person how tries to answer, and about other users how may find it useful in the future. Note: In case you want to contact me, start the comment with @Rotem. – Rotem Jan 23 '23 at 15:50

0 Answers0