当前位置:   article > 正文

【opencv】图像畸变校正

【opencv】图像畸变校正

接上篇文章:【鱼眼+普通相机】相机标定

附代码:

方法一:

使用cv2.undistort

"""
    Create May 11, 2024
    @author Wang Jiajun
"""

import cv2
import numpy as np


def correct(img,camera_file='E:/calib_yaml/ship_calib.yaml'):
    """
    矫正图像,去除畸变
        img: 输入图像
        camera_file: 相机参数文件
        return: 矫正后的图像
    """
    fs = cv2.FileStorage(camera_file, cv2.FILE_STORAGE_READ)
    camera_matrix = fs.getNode("camera_matrix").mat()
    dist_coeffs = fs.getNode("dist_coeffs").mat()  # 畸变参数--顺序很重要!!!:k1,k2,p1,p2,k3
    resolution = fs.getNode("resolution").mat()  # 图像分辨率
    imSize = (resolution[0][0], resolution[1][0])
    fs.release()

    # 矫正图像
    img_distort = cv2.undistort(img, np.array(camera_matrix), np.array(dist_coeffs))
    return img_distort 


if __name__ == '__main__':
    camera_file = 'E:/calib_yaml/ship_calib.yaml'
    video = cv2.VideoCapture(0)
    while True:
        ret, img = video.read()
        # 3. 矫正图像
        img = cv2.resize(img, (640, 480))
        cv2.putText(img, "Press q to exit", (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 200, 0), 2)
        img_distort = cv2.correct(img, camera_file )
        # 4. 显示原图和矫正后的图像
        cv2.imshow('img_original', img)
        cv2.imshow('img_distort', img_distort)
        key = cv2.waitKey(1)
        # 5. 按q退出
        if key & 0xFF == ord('q'):
            break
    # 6. 关闭窗口
    video.release()
    cv2.destroyAllWindows()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

方法二:

使用cv2.initUndistortRectifyMap建立映射关系,就不用每次读取内参了,速度更快。

"""
    Create May 11, 2024
    @author Wang Jiajun
"""

import cv2
import numpy as np


def correct(camera_file='E:/calib_yaml/ship_calib.yaml'):
    """
    矫正图像,去除畸变
        img: 输入图像
        camera_file: 相机参数文件
        return: 矫正后的图像
    """
    fs = cv2.FileStorage(camera_file, cv2.FILE_STORAGE_READ)
    camera_matrix = fs.getNode("camera_matrix").mat()
    dist_coeffs = fs.getNode("dist_coeffs").mat()  # 畸变参数--顺序很重要!!!:k1,k2,p1,p2,k3
    resolution = fs.getNode("resolution").mat()  # 图像分辨率
    imSize = (resolution[0][0], resolution[1][0])
    fs.release()

    # 矫正图像
    new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(np.array(camera_matrix), np.array(dist_coeffs),
                                                           imSize, 0,
                                                           imSize)
    map1, map2 = cv2.initUndistortRectifyMap(np.array(camera_matrix), np.array(dist_coeffs), None, new_camera_matrix,
                                             imSize, cv2.CV_32FC1)
    # img_distort = cv2.undistort(img, np.array(camera_matrix), np.array(dist_coeffs))
    return map1, map2


if __name__ == '__main__':
    camera_file = 'E:/calib_yaml/ship_calib.yaml'
    map1, map2 = correct(camera_file)
    video = cv2.VideoCapture(0)
    while True:
        ret, img = video.read()
        # 3. 矫正图像
        img = cv2.resize(img, (640, 480))
        cv2.putText(img, "Press q to exit", (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 200, 0), 2)
        img_distort = cv2.remap(img, map1, map2, cv2.INTER_LINEAR)
        # 4. 显示原图和矫正后的图像
        cv2.imshow('img_original', img)
        cv2.imshow('img_distort', img_distort)
        key = cv2.waitKey(1)
        # 5. 按q退出
        if key & 0xFF == ord('q'):
            break
    # 6. 关闭窗口
    video.release()
    cv2.destroyAllWindows()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/591974
推荐阅读
相关标签
  

闽ICP备14008679号