鱼眼相机 去畸变

目录

枕形畸变和去枕形畸变

去枕形畸变失败


枕形畸变和去枕形畸变

python 复制代码
import cv2
import numpy as np
import matplotlib.pyplot as plt

# 创建一个带网格的原始图像
def create_grid(image_size=512, grid_size=20):
    image = np.zeros((image_size, image_size, 3), dtype=np.uint8)
    for i in range(0, image_size, grid_size):
        cv2.line(image, (i, 0), (i, image_size), (255, 255, 255), 1)
        cv2.line(image, (0, i), (image_size, i), (255, 255, 255), 1)
    return image

# 应用枕形畸变
def apply_pincushion_distortion(image, k1=0.05):
    image_size = image.shape[0]
    fx, fy = image_size * 1.0, image_size * 1.0
    cx, cy = image_size / 2, image_size / 2
    camera_matrix = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]])
    dist_coeffs = np.array([k1, 0, 0, 0, 0])  # 正值增加枕形畸变

    h, w = image.shape[:2]
    new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coeffs, (w, h), 1)
    distorted_image = cv2.undistort(image, camera_matrix, dist_coeffs, None, new_camera_matrix)
    return distorted_image

# 去除枕形畸变
def remove_pincushion_distortion(image, k1=0.0005):
    image_size = image.shape[0]
    fx, fy = image_size * 1.0, image_size * 1.0
    cx, cy = image_size / 2, image_size / 2
    camera_matrix = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]])
    dist_coeffs = np.array([-k1, 0, 0, 0, 0])  # 负值去除枕形畸变

    h, w = image.shape[:2]
    new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coeffs, (w, h), 1)
    undistorted_image = cv2.undistort(image, camera_matrix, dist_coeffs, None, new_camera_matrix)
    return undistorted_image

# 主函数
if __name__ == "__main__":
    original_grid = create_grid()
    distorted_grid = apply_pincushion_distortion(original_grid,k1=-0.2)
    undistorted_grid = remove_pincushion_distortion(distorted_grid,k1=-0.2-0.2)

    # 使用 OpenCV 展示图像
    cv2.imshow('Original Image', original_grid)
    cv2.imshow('Pincushion Distorted Image', distorted_grid)
    cv2.imshow('Undistorted Image', undistorted_grid)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

去枕形畸变失败

python 复制代码
import cv2
import numpy as np
import matplotlib.pyplot as plt

# 创建一个带有枕形畸变的网格图像
def create_pincushion_distorted_grid(image_size=512, grid_size=20):
    image = np.zeros((image_size, image_size, 3), dtype=np.uint8)
    for i in range(0, image_size, grid_size):
        cv2.line(image, (i, 0), (i, image_size), (255, 255, 255), 1)
        cv2.line(image, (0, i), (image_size, i), (255, 255, 255), 1)

    k1, k2, p1, p2, k3 = -0.2, 0, 0, 0, 0  # 负数制造枕形畸变
    fx, fy = image_size, image_size
    cx, cy = image_size / 2, image_size / 2
    camera_matrix = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]])
    dist_coeffs = np.array([k1, k2, p1, p2, k3])

    map1, map2 = cv2.initUndistortRectifyMap(camera_matrix, dist_coeffs, None, camera_matrix, (image_size, image_size), 5)
    distorted_image = cv2.remap(image, map1, map2, cv2.INTER_LINEAR)

    return image, distorted_image, camera_matrix, dist_coeffs

# 去除枕形畸变
def undistort_image(image, camera_matrix, dist_coeffs):
    h, w = image.shape[:2]
    new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(camera_matrix, dist_coeffs, (w, h), 1, (w, h))
    undistorted_image = cv2.undistort(image, camera_matrix, dist_coeffs, None, new_camera_matrix)
    return undistorted_image

# 主函数
if __name__ == "__main__":
    original_grid, distorted_grid, camera_matrix, dist_coeffs = create_pincushion_distorted_grid()

    # 手动生成去畸变的映射
    map1, map2 = cv2.initUndistortRectifyMap(camera_matrix, dist_coeffs, None, camera_matrix, (original_grid.shape[1], original_grid.shape[0]), cv2.CV_32FC1)
    undistorted_grid = cv2.remap(distorted_grid, map1, map2, cv2.INTER_LINEAR)

    # 使用 OpenCV 展示图像
    cv2.imshow('Original Image', original_grid)
    cv2.imshow('Distorted Image', distorted_grid)
    cv2.imshow('Undistorted Image', undistorted_grid)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
相关推荐
code bean1 小时前
【工控】线扫相机小结 第四篇
数码相机·工控·工业相机
那就举个栗子!1 天前
多传感器融合slam过程解析【大白话版】
数码相机
xy189901 天前
相机触发模式
数码相机
yunfanleo2 天前
代替Spinnaker 的 POINTGREY工业级相机 FLIR相机 Python编程案例
c++·python·数码相机
摆烂仙君3 天前
《Camera-free Image to PanoramaGeneration with Diffusion Model》论文解析——CamFreeDiff
人工智能·数码相机·计算机视觉
weixin_Todd_Wong20103 天前
【海思Hi3519DV500】双目网络相机套板硬件规划方案
数码相机
isyoungboy3 天前
普通单片机为什么无法直接驱动dcmi接口相机
单片机·嵌入式硬件·数码相机
爱煲汤的夏二3 天前
摄影:相机控色
数码相机
OAK中国_官方4 天前
‘视’不可挡:OAK相机助力无人机智控飞行!
数码相机·无人机
环境感知4 天前
基于MATLAB的激光雷达与相机联合标定原理及实现方法——以标定板为例
开发语言·人工智能·数码相机·算法·matlab