鱼眼相机 去畸变

目录

枕形畸变和去枕形畸变

去枕形畸变失败


枕形畸变和去枕形畸变

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()
相关推荐
OCR_wintone42116 小时前
易泊车牌识别相机,助力智慧工地建设
人工智能·数码相机·ocr
lrlianmengba1 天前
推荐一款可视化和检查原始数据的工具:RawDigger
人工智能·数码相机·计算机视觉
CV-X.WANG1 天前
【详细 工程向】基于Smart3D的五镜头相机三维重建
数码相机·3d
小负不负2 天前
使用kalibr_calibration标定相机(realsense)和imu(h7min)
数码相机·opencv·计算机视觉
xm一点不soso2 天前
树莓派基本设置--10.使用MIPI摄像头
人工智能·数码相机·计算机视觉
古月居GYH3 天前
【图像与点云融合教程(五)】海康相机 ROS2 多机分布式实时通信功能包
分布式·数码相机
顾北川_野4 天前
Android 解决MTK相机前摄镜像问题
数码相机
创小董7 天前
履带式排爆演习训练机器人技术详解
数码相机·机器人
shuxianshrng7 天前
违停拍照和闯红灯拍照有什么区别吗
人工智能·数码相机·计算机视觉·视觉检测
图灵追慕者8 天前
机器视觉中光源镜头和相机的关系
数码相机·相机·机器视觉·光源·镜头