信息:
我已经校准了我的相机,发现相机的内部矩阵(K)及其失真系数(d)如下:
import numpy as np
K = np.asarray([[556.3834638575809,0,955.3259939726225],[0,556.2366649196925,547.3011305411478],[0,0,1]])
d = np.asarray([[-0.05165940570900624],[0.0031093602070252167],[-0.0034036648250202746],[0.0003390345044343793]])
从这里,我可以使用以下三行取消对图像的扭曲:
final_K = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(K, d, (1920, 1080), np.eye(3), balance=1.0)
map_1, map_2 = cv2.fisheye.initUndistortRectifyMap(K, d, np.eye(3), final_K, (1920, 1080), cv2.CV_32FC1)
undistorted_image = cv2.remap(image, map_1, map_2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
生成的未失真图像看起来是正确的Left image is distorted, right is undistorted,但是当我尝试使用cv2.remap()
来取消图像点的失真时,点不会映射到与其在图像中对应的像素相同的位置。我在左图中检测到校准板点,使用
ret, corners = cv2.findChessboardCorners(gray, (6,8),cv2.CALIB_CB_ADAPTIVE_THRESH+cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE)
corners2 = cv2.cornerSubPix(gray, corners, (3,3), (-1,-1), (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1))
然后按以下方式重新映射这些点:
remapped_points = []
for corner in corners2:
remapped_points.append(
(map_1[int(corner[0][1]), int(corner[0][0])], map_2[int(corner[0][1]), int(corner[0][0])])
)
In these horizontally concatenated images,左侧图像显示扭曲图像中检测到的点,而右侧图像显示右侧图像中点的重新映射位置
此外,我还无法使用cv2.fisheye.undistortPoints()
获得正确的结果。我有以下功能来消除点的扭曲:
def undistort_list_of_points(point_list, in_K, in_d):
K = np.asarray(in_K)
d = np.asarray(in_d)
# Input can be list of bbox coords, poly coords, etc.
# TODO -- Check if point behind camera?
points_2d = np.asarray(point_list)
points_2d = points_2d[:, 0:2].astype('float32')
points2d_undist = np.empty_like(points_2d)
points_2d = np.expand_dims(points_2d, axis=1)
result = np.squeeze(cv2.fisheye.undistortPoints(points_2d, K, d))
fx = K[0, 0]
fy = K[1, 1]
cx = K[0, 2]
cy = K[1, 2]
for i, (px, py) in enumerate(result):
points2d_undist[i, 0] = px * fx + cx
points2d_undist[i, 1] = py * fy + cy
return points2d_undist
This image显示使用上述函数取消失真时的结果
(这些都是在Python3.6.8版本的Ubuntu18.04上的OpenCV 4.2.0中运行的)
问题
为什么图像坐标的重新映射不能正常工作?我是否错误地使用了map_1
和map_2
为什么使用cv2.fisheye.undistortPoints()
与使用map_1
和map_2
的结果不同
对问题1的回答:
您没有正确使用map_1和map_2
cv2.fisheye.initundistortic整流map函数生成的映射应该是目标图像的像素位置到源图像的像素位置的映射,即dst(x,y)=src(mapx(x,y),mapy(x,y))。请参见OpenCV中的remap
在代码中,map_1用于x方向像素映射,map_2用于y方向像素映射。例如 (X\u未失真,Y\u未失真)是未失真图像中的像素位置map_1[Y_未失真,X_未失真]提供此像素应映射到扭曲图像中X坐标的位置,而map_2将提供相应的Y坐标
因此,map_1和map_2对于从失真图像构建未失真图像非常有用,但并不真正适用于反向处理
这段代码查找未变形的像素角点位置不正确。您需要使用无失真点功能
对问题2的回答:
映射和不失真是不同的
可以将贴图视为基于像素贴图的未失真图像中的像素位置构建未失真图像,而未失真是使用镜头失真模型使用原始像素位置查找未失真的像素位置
以便在未失真图像中找到角点的正确像素位置。您需要使用新估计的K将未失真点的标准化坐标转换回像素坐标,在您的情况下,这是最终的_K,因为未失真的图像可以被视为是由具有最终的_K的相机拍摄的,没有失真(有一个小的缩放效果)
以下是修改后的不失真函数:
这是我做同样事情的代码。
相关问题 更多 >
编程相关推荐