如何使用cv2.fisheye.undistortPoints将扭曲空间中的点转换为未扭曲空间中的点?

2024-09-30 18:15:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用opencv鱼眼函数将未变形空间的特征映射回变形空间。我可以通过以下代码成功地将我的图像从相机扭曲的鱼眼图像转换到规则空间:

DIM = (953, 720)
K = np.array(
    [
        [407.0259762535615, 0.0, 488.89663712932474],
        [0.0, 409.25366832487896, 388.1998354574297],
        [0.0, 0.0, 1.0],
    ]
)
D = np.array(
    [
        [-0.04485892302426824],
        [0.0787884305594057],
        [-0.08374236678783106],
        [0.027626067632899026],
    ]
)
img = np.zeros((720, 953, 3), dtype=np.uint8)
img = cv2.rectangle(img, (200, 150), (300, 200), (255, 255, 255), -1)
map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, DIM, cv2.CV_16SC2)
undistorted_img = cv2.remap(
    img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT
)
# The rectangle is now found at 60, 43 in undistorted_img.  This code works.

但是,当我想映射一个点(任意方向)时,我无法使用cv2.fisheye.undistortPoints或扭曲点将点从一个空间移动到另一个空间。通过使用尺寸为720x953的图像,我在x,y200150处放置了一个点。在未失真图像中,该点现在为60,43。但是,我无法使用这两个函数映射这两点。以下是我的代码和输出:

cv2.fisheye.undistortPoints(np.array([[[200, 150]]], dtype=np.float32), K, D)
# returns array([[[-1.0488918, -0.8601203]]], dtype=float32)
cv2.fisheye.distortPoints(np.array([[[200, 150]]], dtype=np.float32), K, D)
# returns array([[[1064.9419,  822.5983]]], dtype=float32)
cv2.fisheye.undistortPoints(np.array([[[60, 34]]], dtype=np.float32), K, D)
# Returns array([[[-4.061374 , -3.3357866]]], dtype=float32)
cv2.fisheye.distortPoints(np.array([[[60, 34]]], dtype=np.float32), K, D)
# array([[[1103.0706 ,  738.13654]]], dtype=float32)

这些都与我在图像转换本身中看到的不匹配。关于扭曲点和不扭曲点,我不了解什么?谢谢


Tags: 函数代码图像imgnp空间arraycv2
1条回答
网友
1楼 · 发布于 2024-09-30 18:15:33

我想你需要为不失真做些什么

points = np.array([[[200, 150]]]).astype(np.float32)
newcameramtx = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(
    K, D, DIM, None, balance=1)
dst = cv2.fisheye.undistortPoints(points, K, D, None, newcameramtx)
# [[[323.35104 242.06458]]]

相关问题 更多 >