OpenCV立体声使用ChArUco校准两个摄像机

时间:2020-10-30 17:12:23

标签: python opencv computer-vision camera-calibration stereo-3d

我想使用charuco board的多个帧来计算两个摄像机之间的相对转换([R | t]矩阵)。我的想法是从所有帧中获取图像对象点对,然后使用一个函数,该函数获取所有检测到的点对并输出相机之间的相对变换(例如stereoCalibrate)。

做到这一点的最佳方法是什么?我无法使stereoCalibrate工作,因为它总是会引发断言错误-> bugreport

当前实施(无效)

imagePointsA = []
imagePointsB = []
objectPoints = []
    for frameA, frameB in color_framesets(...):
        try:            
            # Find corners
            cornersA, idsA, rejected = cv2.aruco.detectMarkers(frameA, charucoDict)
            cornersB, idsB, rejected = cv2.aruco.detectMarkers(frameB, charucoDict)
            if not cornersA or not cornersB: raise Exception("No markers detected")

            retA, cornersA, idsA = cv2.aruco.interpolateCornersCharuco(cornersA, idsA, frameA, charucoBoard)
            retB, cornersB, idsB = cv2.aruco.interpolateCornersCharuco(cornersB, idsB, frameB, charucoBoard)
            if not retA or not retB: raise Exception("Can't interpolate corners")


            # Find common points in both frames (is there a nicer way?)
            objPtsA, imgPtsA = cv2.aruco.getBoardObjectAndImagePoints(charucoBoard, cornersA, idsA)
            objPtsB, imgPtsB = cv2.aruco.getBoardObjectAndImagePoints(charucoBoard, cornersB, idsB)

            # Create dictionary for each frame objectPoint:imagePoint
            ptsA = {tuple(a):tuple(b) for a, b in zip(objPtsA[:,0], imgPtsA[:,0])}
            ptsB = {tuple(a):tuple(b) for a, b in zip(objPtsB[:,0], imgPtsB[:,0])}
            common = set(ptsA.keys()) & set(ptsB.keys())    # intersection between obj points
            
            for objP in common:
                objectPoints.append(np.reshape(objP, (1, 3)))
                imagePointsA.append(np.reshape(ptsA[objP], (1, 2)))
                imagePointsB.append(np.reshape(ptsB[objP], (1, 2)))

        except Exception as e:
            print(f"Skipped frame: {e}")
            continue

    result = cv2.stereoCalibrateExtended(objectPoints, imagePointsA, imagePointsB, intrA, distA, intrB, distB, (848, 480), flags=cv2.CALIB_FIX_INTRINSIC+cv2.CALIB_USE_EXTRINSIC_GUESS)
   

1 个答案:

答案 0 :(得分:0)

我今天早些时候刚刚做了类似的事情。我假设您至少解决了部分问题,因为您关闭了提到的错误。无论如何,在我看来,问题在于您正在传递一个点数组,而它应该是一个点数组数组(具有足够数据的每一帧的点数组)。

在相关说明中,cv2.aruco.getBoardObjectAndImagePoints 可能不是您要找的,cornersAcornersB 已经是图像点(棋盘图案角)和对象点(位置棋盘图案角)可从 aruco 标记 id 计算,而 getBoardObjectAndImagePoints 是关于 aruco 标记角,据我所知。

在内部,cv2.aruco.calibrateCameraCharuco 只是简单地调用 cv2.calibrateCamera,将传递的角点作为图像点,对象点为 computed from the passed aruco IDs。不幸的是,从 aruco ID 获取对象点并未在 API 中公开,但计算起来非常容易:https://github.com/opencv/opencv_contrib/blob/master/modules/aruco/src/charuco.cpp#L157-L166