OpenCV校准鱼眼镜头错误(病态矩阵)

2024-06-01 17:26:41 发布

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

我试着按照这些说明校准鱼眼镜头 https://medium.com/@kennethjiang/calibrate-fisheye-lens-using-opencv-333b05afa0b0 你可以找到我用来校准零件的完整代码。在

我到达这里时:

N_OK = len(objpoints)
K = np.zeros((3, 3))
D = np.zeros((4, 1))
rvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]
tvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]  
rms, _, _, _, _ = \
    cv2.fisheye.calibrate(
        objpoints,
        imgpoints,
        gray.shape[::-1],
        K,
        D,
        rvecs,
        tvecs,
        calibration_flags,
        (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-3)
    )
print("Found " + str(N_OK) + " valid images for calibration")
print("DIM=" + str(_img_shape[::-1]))
print("K=np.array(" + str(K.tolist()) + ")")
print("D=np.array(" + str(D.tolist()) + ")")

我得到这个错误:

^{pr2}$

我不知道发生了什么,我只能在网上找到那么少的信息,有没有人经历过类似的事情,知道如何解决这个问题?在

谢谢

以下是我使用的棋盘图像:


Tags: httpscomforstacknpzerosokcv2
3条回答

正如@Ahmadiah提到的,当棋盘落在图像边缘附近时,“病态”的事情就会发生。解决这个问题的一种方法是逐个删除图像,当它们导致校准失败时再试一次。下面是一个我们这样做的例子:

def calibrate_fisheye(all_image_points, all_true_points, image_size):
    """ Calibrate a fisheye camera from matching points.
    :param all_image_points: Sequence[Array(N, 2)[float32]] of (x, y) image coordinates of the points.  (see  cv2.findChessboardCorners)
    :param all_true_points: Sequence[Array(N, 3)[float32]] of (x,y,z) points.  (If from a grid, just put (x,y) on a regular grid and z=0)
        Note that each of these sets of points can be in its own reference frame,
    :param image_size: The (size_y, size_x) of the image.
    :return: (rms, mtx, dist, rvecs, tvecs) where
        rms: float - The root-mean-squared error
        mtx: array[3x3] A 3x3 camera intrinsics matrix
        dst: array[4x1] A (4x1) array of distortion coefficients
        rvecs: Sequence[array[N,3,1]] of estimated rotation vectors for each set of true points
        tvecs: Sequence[array[N,3,1]] of estimated translation vectors for each set of true points
    """
    assert len(all_true_points) == len(all_image_points)
    all_true_points = list(all_true_points)  # Because we'll modify it in place
    all_image_points = list(all_image_points)
    while True:
        assert len(all_true_points) > 0, "There are no valid images from which to calibrate."
        try:
            rms, mtx, dist, rvecs, tvecs = cv2.fisheye.calibrate(
                objectPoints=[p[None, :, :] for p in all_true_points],
                imagePoints=[p[:, None, :] for p in all_image_points],
                image_size=image_size,
                K=np.zeros((3, 3)),
                D=np.zeros((4, 1)),
                flags=cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC + cv2.fisheye.CALIB_CHECK_COND + cv2.fisheye.CALIB_FIX_SKEW,
                criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6),
            )
            print('Found a calibration based on {} well-conditioned images.'.format(len(all_true_points)))
            return rms, mtx, dist, rvecs, tvecs
        except cv2.error as err:
            try:
                idx = int(err.message.split('array ')[1][0])  # Parse index of invalid image from error message
                all_true_points.pop(idx)
                all_image_points.pop(idx)
                print("Removed ill-conditioned image {} from the data.  Trying again...".format(idx))
            except IndexError:
                raise err

我认为这是因为你的变量校准标记为校准检查条件设置。 尝试禁用此标志。没有它,我可以不失真你的图像(见下面的链接)。在

我不确定这个检查是为了什么(这个documentation不是很明确)。这个旗子拒绝了一些我的gopro英雄3的图像,即使棋盘是可见的和检测到的。在我的例子中,20张图片中有一张没有通过这个测试。这张图片的棋盘靠近左边的边界。在

¹在OpenCV版本>;=3.4.1中,error message会告诉您哪个映像没有通过测试

我在python中没有找到代码,所以我手动检查边缘有棋盘的图像,然后逐个删除,直到错误消失。在

相关问题 更多 >