鱼眼相机 去畸变

目录

枕形畸变和去枕形畸变

去枕形畸变失败


枕形畸变和去枕形畸变

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()
相关推荐
只待花开1 天前
ROS2 python编写 intel realsense D405相机节点通过launch.py启动多个相机并发送图像话题,基于pyrealsense2库
数码相机
KeyPan1 天前
【ORB-SLAM3:相机针孔模型和相机K8模型】
数码相机
千穹凌帝1 天前
基于深度学习多图像融合的屏幕缺陷检测方案
人工智能·深度学习·数码相机
传说故事2 天前
相机内外参知识
数码相机·相机·相机参数
妄想出头的工业炼药师2 天前
imu相机EKF
数码相机
合方圆~小文2 天前
工业摄像机基于电荷耦合器件的相机
人工智能·深度学习·数码相机·目标检测
资源补给站3 天前
大恒相机开发(1)—Python调用采集彩色图像并另存为本地
开发语言·python·数码相机
OAK中国_官方3 天前
四相机设计实现全向视觉感知的开源空中机器人无人机
数码相机·机器人·无人机
s_daqing3 天前
解锁BL后的K40降级
数码相机
Stark-C6 天前
相机与NAS的奇妙组合,如何使用相机拍照自动上传或备份到NAS
数码相机