鱼眼相机 去畸变

目录

枕形畸变和去枕形畸变

去枕形畸变失败


枕形畸变和去枕形畸变

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()
相关推荐
luoganttcc21 小时前
BEV感知中如何使用相机内外参?
数码相机
SKYDROID云卓小助手1 天前
三轴云台之相机技术篇
运维·服务器·网络·数码相机·音视频
越甲八千2 天前
相机的曝光和增益
数码相机
越甲八千2 天前
黑白彩色相机成像原理
数码相机
幻想趾于现实2 天前
机器视觉调试——现场链接相机(解决各种相机链接问题)
数码相机·工业相机
a3158238063 天前
SnapdragonCamera骁龙相机源码解析
android·数码相机·framework·高通
越甲八千3 天前
全局曝光与卷帘曝光
数码相机
博图光电3 天前
短波红外相机应用领域介绍
数码相机
中达瑞和-高光谱·多光谱3 天前
多光谱相机在农业中的应用(农作物长势、病虫害、耕地检测等)
数码相机
千野竹之卫4 天前
2025最新云渲染网渲100渲染农场使用方法,渲染100邀请码1a12
开发语言·前端·javascript·数码相机·3d·3dsmax