I am trying to get a bird's eye view transformation on a given Image.
Using cv2.calibrateCamera
and cv2.solvePnPRansac
, I know the intrinsic parameters of the camera.
I have found many posts suggesting many different answers, but none worked for me. Some examples of posts which I have tried:
- Python create image with new camera position
- Bird's eye view perspective transformation from camera calibration opencv python
- OpenCV: get perspective matrix from translation & rotation
None of them worked, some resulted in black images while others in strange warps which make no sense. I know the intrinsic parameters I computed are correct because I can re-project my model back on the original image points.
Camera matrix for this image:
Rotation vector:
Translation vector:
The latest code I have used it this:
def get_birds_eye_view(image, camera_matrix, rotation_vec, translation_vec):
dst = np.zeros_like(image)
dx, dy, dz = translation_vec
A1= np.matrix([[1, 0, -image.shape[1]/2],
[0, 1, -image.shape[0]/2],
[0, 0, 0 ],
[0, 0, 1 ]])
T = np.matrix([[1,0,0,dx],
[0,1,0,dy],
[0,0,1,dz],
[0,0,0,1]])
rotation_matrix = cv2.Rodrigues(rotation_vec)[0]
rotation_matrix = np.c_[ rotation_matrix, np.zeros(3)]
rotation_matrix = np.r_[ rotation_matrix, [[0, 0, 0, 1]] ]
camera_matrix = np.c_[ camera_matrix, np.zeros(3)]
invers_camera_matrix = np.zeros((4,3))
invers_camera_matrix[:3,:3] = np.linalg.inv(camera_matrix[:3,:3])
invers_camera_matrix[-1,:] = [0, 0, 1]
H = np.linalg.multi_dot([camera_matrix, rotation_matrix, T, invers_camera_matrix])
warped_image = cv2.warpPerspective(image, H, (image.shape[1], image.shape[0]), dst, cv2.INTER_NEAREST, cv2.BORDER_CONSTANT, 0)
return warped_image
Example image:
The desired output might look like this (but with a bad not a chess board, image taken from second link):
If any information is missing please comment and I'll provide the data.