未能获得完整、不失真的鱼眼图像

发布于 2025-01-20 01:54:17 字数 1992 浏览 0 评论 0原文

我正在关注本教程(https://medium .com/@kenneth Jiang/calibrate-fisheye-lens-using-opencv-part-2-13990f1b157f)了解如何消除鱼眼图像的扭曲。我已经成功获得了相机的内在价值,并且能够从第 1 部分获得未失真的图像。

然而,当我尝试遵循第二部分时,我得到的图像只是一张空白图像。我怎样才能获得整个不失真的图像。我的相机的 fov 是 210。以下是我的代码。

import sys
import cv2
import numpy as np
# You should replace these 3 lines with the output in calibration step


DIM=(640,480)
K=np.array([[165.40632165612672, 0.0, 344.73820123702507], [0.0, 166.23436436470345, 238.925308736596], [0.0, 0.0, 1.0]])
D=np.array([[0.015729113828588378], [-0.053735965539934524], [0.06693328877678231], [-0.03822388660918581]])
def undistort(img_path, balance=0.0, dim2=None, dim3=None):
    img = cv2.imread(img_path)
    dim1 = img.shape[:2][::-1]  #dim1 is the dimension of input image to un-distort
    assert dim1[0]/dim1[1] == DIM[0]/DIM[1], "Image to undistort needs to have same aspect ratio as the ones used in calibration"
    if not dim2:
        dim2 = dim1
    if not dim3:
        dim3 = dim1
    scaled_K = K * dim1[0] / DIM[0]  # The values of K is to scale with image dimension.
    scaled_K[2][2] = 1.0  # Except that K[2][2] is always 1.0
    # This is how scaled_K, dim2 and balance are used to determine the final K used to un-distort image. OpenCV document failed to make this clear!
    new_K = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(scaled_K, D, dim2, np.eye(3), balance=balance)
    map1, map2 = cv2.fisheye.initUndistortRectifyMap(scaled_K, D, np.eye(3), new_K, dim3, cv2.CV_16SC2)
    undistorted_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
    cv2.imshow("undistorted", undistorted_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
if __name__ == '__main__':
    undistort("1.jpeg")

这是我得到的结果

I am following this tutorial (https://medium.com/@kennethjiang/calibrate-fisheye-lens-using-opencv-part-2-13990f1b157f) on how to undistort a fisheye image. I have successfully get the intrinsic values of the camera and is able to get the undistorted image from part1.

However when i try to follow part two, the image I get is just a blank image. How can I get the whole undistorted image. The fov of my camera is 210. The following is my code.

import sys
import cv2
import numpy as np
# You should replace these 3 lines with the output in calibration step


DIM=(640,480)
K=np.array([[165.40632165612672, 0.0, 344.73820123702507], [0.0, 166.23436436470345, 238.925308736596], [0.0, 0.0, 1.0]])
D=np.array([[0.015729113828588378], [-0.053735965539934524], [0.06693328877678231], [-0.03822388660918581]])
def undistort(img_path, balance=0.0, dim2=None, dim3=None):
    img = cv2.imread(img_path)
    dim1 = img.shape[:2][::-1]  #dim1 is the dimension of input image to un-distort
    assert dim1[0]/dim1[1] == DIM[0]/DIM[1], "Image to undistort needs to have same aspect ratio as the ones used in calibration"
    if not dim2:
        dim2 = dim1
    if not dim3:
        dim3 = dim1
    scaled_K = K * dim1[0] / DIM[0]  # The values of K is to scale with image dimension.
    scaled_K[2][2] = 1.0  # Except that K[2][2] is always 1.0
    # This is how scaled_K, dim2 and balance are used to determine the final K used to un-distort image. OpenCV document failed to make this clear!
    new_K = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(scaled_K, D, dim2, np.eye(3), balance=balance)
    map1, map2 = cv2.fisheye.initUndistortRectifyMap(scaled_K, D, np.eye(3), new_K, dim3, cv2.CV_16SC2)
    undistorted_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
    cv2.imshow("undistorted", undistorted_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
if __name__ == '__main__':
    undistort("1.jpeg")

This is the result I get

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文