我的相机有3x3 intrinsics和4x3 extrinsics矩阵,通过cv2.calibrateCamera()获得。
现在,我想使用这些参数来计算从摄像机获得的帧中任意给定坐标的BEV (Bird Eye View)变换。
哪个openCv函数可以用来计算给定点坐标和摄像机extrinsics和/或intrinsics 3x3 matrices的BEV透视图转换?
我在下面的文章中发现了一些非常相关的东西:基于https://deepnote.com/article/social-distancing-detector/的https://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/,
他们使用cv2.getPerspectiveTransform()来获取3X3 matrix,但是我不知道这个矩阵是代表intrinsics、extrinsecs还是其他什么东西。然后,他们使用这样的矩阵来转换点列表,方式如下:
#Assuming list_downoids is the list of points to be transformed and matrix is the one obtained above
list_points_to_detect = np.float32(list_downoids).reshape(-1, 1, 2)
transformed_points = cv2.perspectiveTransform(list_points_to_detect, matrix)我真的需要知道是否可以使用这个cv2.perspectiveTransform函数来计算转换,或者是否还有更好的方法使用extrinsics、intrinsics或两者,而不必重用该框架,因为我已经将帧中检测到的人的坐标保存到了数组中。
有人刚刚告诉我,如果我有至少3个3D点使用cv2.findHomography函数,我可以达到这个目标。我正在尝试这种方法。还有其他人有其他的输入吗?
发布于 2020-12-27 23:31:10
经过深入的调查,我找到了一个很好的解决办法:
projection matrix是extrinsic和intrinsic摄像机矩阵之间的乘法器。
extrinsic是一个4x3矩阵,intrinsec是一个3x3矩阵,但是我们需要projection matrix是一个3x3矩阵,那么在执行乘法之前,我们需要将extrinsic转换成3x3。当我们没有摄像头时,cv2.getPerspectiveTransform()会给我们Projection Matrix:
cv2.warpPerspective()转换图像itsef。
对于上面的问题,我们不需要这两个函数,因为我们已经有了extrinsics、intrinsecs和图像中点的坐标。
考虑到上面介绍的内容,我编写了一个函数,将给定BEV和extrinsics的list_x_y点列表转换为D31:
def compute_point_perspective_transformation(intrinsics, extrinsics, point_x_y):
"""Auxiliary function to project a specific point to BEV
Parameters
----------
intrinsics (array) : The camera intrinsics matrix
extrinsics (array) : The camera extrinsics matrix
point_x_y (tuple[x,y]) : The coordinates of the point to be projected to BEV
Returns
----------
tuple[x,y] : the projection of the point
"""
# Using the camera calibration for Bird Eye View
intrinsics_matrix = np.array(intrinsics, dtype='float32')
#In the intrinsics we have parameters such as focal length and the principal point
extrinsics_matrix = np.array(extrinsics, dtype='float32')
#The extrinsic matrix stores the position of the camera in global space
#The 1st 3 columns represents the rotation matrix and the last is a translation vector
extrinsics = extrinsics[:, [0, 1, 3]]
#We removed the 3rd column of the extrinsics because it represents the z coordinate (0)
projection_matrix = np.matmul(intrinsics_matrix, extrinsics_matrix)
# Compute the new coordinates of our points - cv2.perspectiveTransform expects shape 3
list_points_to_detect = np.array([[point_x_y]], dtype=np.float32)
transformed_points = cv2.perspectiveTransform(list_points_to_detect, projection_matrix)
return transformed_points[0][0][0], transformed_points[0][0][1]https://datascience.stackexchange.com/questions/86554
复制相似问题