首页 > 编程问答 >错误“对于非平面校准装置,必须在函数‘cvCalibrateCamera2Internal’中指定初始固有矩阵”

错误“对于非平面校准装置,必须在函数‘cvCalibrateCamera2Internal’中指定初始固有矩阵”

时间:2024-07-25 06:59:08浏览次数:14  
标签:python numpy opencv camera-calibration stereo-3d

我遇到的错误的完整跟踪:

在stereo_calibrate中 ret,cameraMatrix1,distCoeffs1,cameraMatrix2,distCoeffs2,R,T,E,F,perViewErrors,_,_= cv2.stereoCalibrateExtended( cv2.error: OpenCV(4.10.0) /io/opencv/modules/calib3d/src/calibration.cpp:1682: error: (-5:Bad argument) 对于非平面校准装置,必须在中指定初始内在矩阵函数'cvCalibrateCamera2Internal'

这是我的代码:


def stereo_calibrate(img, correspondences_left, correspondences_right, camera_matrix_L, camera_matrix_R, baseline=4.1, dist_coeffs=None):
    if dist_coeffs is None:
        dist_coeffs = np.zeros((4, 1))

    # Extract image points and object points from correspondences
    image_points_left = correspondences_left[:, :2].astype(np.float32)
    image_points_right = correspondences_right[:, :2].astype(np.float32)
    object_points = correspondences_left[:, 2:].astype(np.float32)  # Assuming object points are the same for both sets

    rvec_L, tvec_L = np.array([0., 0., 0.]), np.array([0, 0., 0.])
    rvec_R, tvec_R = np.array([-0.00637872, 0.00102092, 0.000673804]), np.array([0., 0., 0.])

    # Convert rotation vectors to rotation matrices
    R_L, _ = cv2.Rodrigues(rvec_L)
    R_R, _ = cv2.Rodrigues(rvec_R)
    print("after converting via Rodrigues:", R_L, R_R)

    # Initialize rotation and translation vectors
    R_init = R_R @ R_L.T
    # T_init = tvec_R - R_init @ tvec_L
    T_init = np.array([0., 0., 0.])
    print("R_init", R_init)
    print("T_init", T_init)

    # Stereo calibration to refine the extrinsic parameters
    ret, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F, perViewErrors, _, _= cv2.stereoCalibrateExtended(
        [object_points], [image_points_left], [image_points_right],
        camera_matrix_L, dist_coeffs, camera_matrix_R, dist_coeffs,
        imageSize=(img.shape[1], img.shape[0]),  # Correct image size as (width, height)
        R=R_init, T=T_init,
        criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 1e-6),
        flags=cv2.CALIB_USE_EXTRINSIC_GUESS
    )
    print("ret", ret)
    if ret:
        print("Stereo Calibration Successful")
        print("Rotation Matrix between the cameras:")
        print(R)
        print("Translation Vector between the cameras:")
        print(T)
        print("dist_coeffs1", distCoeffs1)
        print("dist_coeffs2", distCoeffs2)
        print(rvec_L)
        print(tvec_L)
    else:
        raise ValueError("stereoCalibrate failed to find a solution")

    # Reproject object points to both cameras
    reprojected_points_left, _ = cv2.projectPoints(object_points, rvec_L, tvec_L, camera_matrix_L, distCoeffs1)
    reprojected_points_right, _ = cv2.projectPoints(object_points, R, T, camera_matrix_R, distCoeffs2)
    
    return R, T, reprojected_points_left, reprojected_points_right

correspondences_left = np.array([
    [671.0889955686854, 193.80354505169868, 2.4492935982947064e-16, -4.0, 11],
...
])

correspondences_right = np.array([
    [436.2245592329106, 193.79399938137954, 2.4492935982947064e-16, -4.0, 11],
...
])

# Camera intrinsic parameters, replace with your actual camera matrix
camera_matrix_L = np.array([
    [1018.9, 0, 601.447],
    [0, 1018.9, 517.462],
    [0, 0, 1]
], dtype=np.float32)

camera_matrix_R = np.array([
    [1018.9, 0, 690.392],
    [0, 1018.9, 517.462],
    [0, 0, 1]
], dtype=np.float32)

# calling the function
R, T, reprojected_points_left, reprojected_points_right = stereo_calibrate(img_left, correspondences_left, correspondences_right, camera_matrix_L, camera_matrix_R)

这个错误真的让我困惑,因为我显然正在传递初始相机内在矩阵。为什么会发生这个错误?


The issue lies in how you are calling the cv2.stereoCalibrateExtended function and what it expects when you provide an initial guess for the extrinsic parameters.

Let's break down the problem and the solution:

Problem:

  • Non-Planar Calibration: The error message "For non-planar calibration rigs, it is required to specify initial intrinsic matrices in function 'cvCalibrateCamera2Internal'" tells us that OpenCV assumes you are using a non-planar calibration pattern (like a 3D target). Non-planar calibration needs more information to initialize the optimization process effectively.
  • Initial Guess Misinterpretation: When you provide R and T to cv2.stereoCalibrateExtended with cv2.CALIB_USE_EXTRINSIC_GUESS , you're telling it to use these as initial guesses for the stereo extrinsic parameters (relationship between the cameras). However, the underlying cvCalibrateCamera2Internal function, which handles the per-camera calibration, needs initial guesses for the intrinsic parameters of each camera if you're not using a planar target.

Solution:

To resolve this, you have two main options:

  1. Use cv2.CALIB_FIX_INTRINSIC :

  2. If you are confident that your camera_matrix_L and camera_matrix_R are accurate and don't need to be refined during stereo calibration, you can tell OpenCV to keep them fixed:

    python flags=cv2.CALIB_USE_EXTRINSIC_GUESS | cv2.CALIB_FIX_INTRINSIC

  3. Important: This assumes your camera matrices are very good. If they are off, your stereo calibration will be inaccurate.

  4. Provide Initial Intrinsic Guesses (If Necessary):

  5. If you want to refine the intrinsic parameters slightly during stereo calibration, you can provide "good enough" initial guesses. Often, using the matrices you already have is a reasonable starting point.

    python ret, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2, R, T, E, F, perViewErrors, _, _= cv2.stereoCalibrateExtended( [object_points], [image_points_left], [image_points_right], camera_matrix_L, dist_coeffs, camera_matrix_R, dist_coeffs, imageSize=(img.shape[1], img.shape[0]), R=R_init, T=T_init, criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 1e-6), flags=cv2.CALIB_USE_EXTRINSIC_GUESS | cv2.CALIB_USE_INTRINSIC_GUESS )

Additional Notes:

  • Calibration Target: The error message strongly suggests you are using a non-planar calibration pattern. If you are using a planar target, you can usually omit providing initial intrinsic guesses (unless you have a specific reason).
  • Accuracy: Accurate stereo calibration depends heavily on good input data (images, correspondences, calibration pattern). Make sure your calibration setup and image acquisition process are solid.

Let me know if you have any more questions!

标签:python,numpy,opencv,camera-calibration,stereo-3d
From: 78790863

相关文章

  • 使用 python Flask 发送邮件中的图像
    我想发送一封包含html代码和图像的电子邮件但在gmail中它说图像已附加,我不想要这样,我只想要电子邮件正文中的图像。html_content=f"<imgsrc="cid:banner"alt=""style="width:80%;">"msg=MIMEMultipart('related')html_part=MIMEText(html_c......
  • 在 python requests modul 中,如何检查页面是否使用“POST”方法或“GET”方法
    如何使用python“requests”模块检查页面是否使用“GET”方法或“POST”方法。我期望输出为True或False,或者GET或Post预期代码:importrequestsurl=f"www.get_example.com"response=requests.get(url)ifresponse.check_get==True:print("get")你......
  • VS Code Python - 如果括号(括号、大括号等)未关闭,内联建议不起作用
    我遇到的问题是,当我在未闭合的括号或方括号“内部”开始变量名称时,VSCode将不会显示任何建议。但是,如果在键入变量名称之前闭合括号,则建议效果很好。如果我可以避免它,我宁愿不将自动完成括号关闭设置为True也不使用TabOut扩展。第一个屏幕截图显示建议在闭括号/方......
  • 在 Azure 上部署代码时使用 Python 的多处理模块是否有意义?
    我们的团队在Azure机器学习(AML)上部署了一个Python脚本来处理存储在Azure存储帐户上的文件。我们的管道由一个ForEach活动组成,该活动调用每个或列出的文件的Python脚本。从Azure数据工厂(ADF)运行它会触发多个单独的管道同时运行......
  • 我已成功安装 pypdf2 但无法将其导入到我的 python 文件中
    我已经成功安装了pypdf2模块,但在导入它时,我发现该模块丢失了。我尝试使用fromPyPDF2importPdfReader导入,但它不起作用此问题的各种解决方案是什么?在尝试导入PyPDF2时遇到问题。以下是可能导致此问题的一些常见原因和解决方案:安......
  • Python3打开图片时请求ConnectionResetError(10054)
    我试图从'http://xxx.jpg'之类的网站下载图片。代码:headers={'user-agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/66.0.3359.139Safari/537.36'}url='http://xxx.jpg'resp......
  • Jupyter Notebook 环境中的 Python 版本不匹配
    我遇到Jupyter笔记本启动横幅中报告的Python版本与我在笔记本中查询python--version时显示的版本之间的差异。启动横幅指示Python3.11.9,但是当我运行!python--version时,它返回Python3.11.7。我所做的步骤:basecondahas3.11.7versio......
  • Python XML 解析:字符串中的“<”被阻塞
    我有一个使用ET.XMLParser来解析CppCheckXML报告文件的Python模块。当尝试解析字符串中包含“<”的XML元素中的属性之一时,它会令人窒息,它会将其解释为格式错误的XML,例如:<errormsg="Includefile<iostream>notfound.">(注意字符和“iostream”之间的空格必须放......
  • 任意几行代码要成为Python中的函数需要什么?
    我正在上一门计算机科学课,我的任务是创建一个程序来实现一个带有参数的函数。我的老师告诉我,下面的代码不是一个函数,这让我很困惑,对于将某些代码行归类为“函数”所需的条件,我感到很困惑。defgame(numbers,max_turns,pfl,tgl):turns=0flag=Falseprint("You......
  • 如何使用 Python 创建新的 Azure 订阅?
    我正在尝试使用PythonSDK以编程方式创建新的Azure订阅。我发现的对AzurePythonSDK的唯一引用是这个这是我最终得到的结果:importazure.mgmt.billingimportazure.mgmt.subscriptioncreds=AzureCliCredential()client_name='test'defcreat......