opencv 鱼眼图像的矫正(动态参数调整)

一:棋盘校准参数说明(内参)

棋盘校准的方法及代码很多,参见其他连接

1:内参矩阵

2:畸变系数

针对鱼眼相机此处是4个参数,在其校准代码中也可以知道,其通常的定义如下:

复制代码
       data.camera_mat = np.eye(3, 3)
       data.dist_coeff = np.zeros((4, 1))

二:基于生成的参数D和K进行调整

直接上代码,

复制代码
import os
import numpy as np
import cv2
#下面的值是来自棋盘格校准后的内参矩阵K
initial_camera_matrix = np.array([[389.3455401867884, 0.0, 630.3678577531273],
                                  [0.0, 388.5686773894828, 361.167452606629],
                                  [0.0, 0.0, 1.0]])
                     
camera_mat = np.array([[302.116935,0.0,521.926531],
                       [  0.0, 358.512097, 363.652721],
                       [  0.0, 0.0,     1.0      ]])

#下面的值是来自棋盘格校准后的D
dist_coeff = np.array([[0.05648235312901486],[-0.024826520405491565],[-0.002416551582982325],[0.0010672440368159684]])

def adjust_parameters(x):
# 获取当前滑动条的值
    fx = cv2.getTrackbarPos('fx', 'Parameters')/ 100.0 
    fy = cv2.getTrackbarPos('fy', 'Parameters')/ 100.0 
    cx = cv2.getTrackbarPos('cx', 'Parameters')/ 100.0 
    cy = cv2.getTrackbarPos('cy', 'Parameters')/ 100.0 
    k1 = cv2.getTrackbarPos('k1', 'Parameters') / 2000000  # 调整范围以便于滑动条控制
    k2 = cv2.getTrackbarPos('k2', 'Parameters') / 2000000
    p1 = cv2.getTrackbarPos('p1', 'Parameters') / 2000000
    p2 = cv2.getTrackbarPos('p2', 'Parameters') / 2000000

# 更新相机矩阵和畸变系数
    new_camera_matrix = np.array([[fx, 0.0, cx],
                                  [0.0, fy, cy],
                                  [0.0, 0.0, 1.0]])
    new_distortion_coeff = np.array([[k1], [-k2], [-p1], [p2]])

    map1, map2 = cv2.fisheye.initUndistortRectifyMap(
                initial_camera_matrix, new_distortion_coeff, np.eye(3, 3), new_camera_matrix, 
                (args.width * args.sizescale, args.height * args.sizescale), cv2.CV_16SC2) 

  
    img = np.load("./calibrate_img/img_cam5.npy")
    print(new_camera_matrix)
    print(new_distortion_coeff)
    undistort_img = cv2.remap(img, map1, map2, cv2.INTER_LINEAR)
    undistort_img=cv2.resize(undistort_img,(img.shape[1],img.shape[0]))

    cv2.imshow("undistort_img",undistort_img)

    
    cv2.imshow("src_img",img)




def main():

    cv2.namedWindow('Parameters')
    
# 添加相机矩阵的滑动条
    cv2.createTrackbar('fx', 'Parameters', int(camera_mat[0, 0]* 10000), 90*100000, adjust_parameters)
    cv2.createTrackbar('fy', 'Parameters', int(camera_mat[1, 1]* 10000), 90*100000, adjust_parameters)
    cv2.createTrackbar('cx', 'Parameters', int(camera_mat[0, 2]* 10000), 90*1000000, adjust_parameters)
    cv2.createTrackbar('cy', 'Parameters', int(camera_mat[1, 2]* 10000), 90*100000, adjust_parameters)
    
# 添加畸变系数的滑动条
    cv2.createTrackbar('k1', 'Parameters', int(dist_coeff[0, 0] * 100000), 4000000, adjust_parameters)
    cv2.createTrackbar('k2', 'Parameters', int(dist_coeff[1, 0] * 100000), 4000000, adjust_parameters)
    cv2.createTrackbar('p1', 'Parameters', int(dist_coeff[2, 0] * 100000), 4000000, adjust_parameters)
    cv2.createTrackbar('p2', 'Parameters', int(dist_coeff[3, 0] * 100000), 4000000, adjust_parameters)
    while True:
        key = cv2.waitKey(1) & 0xFF
        if key == ord('q'):
            break

    cv2.destroyAllWindows()  
    
if __name__ == '__main__':
    main()    
相关推荐
吴佳浩3 小时前
Python入门指南(七) - YOLO检测API进阶实战
人工智能·后端·python
tap.AI3 小时前
RAG系列(二)数据准备与向量索引
开发语言·人工智能
老蒋新思维4 小时前
知识IP的长期主义:当AI成为跨越增长曲线的“第二曲线引擎”|创客匠人
大数据·人工智能·tcp/ip·机器学习·创始人ip·创客匠人·知识变现
货拉拉技术4 小时前
出海技术挑战——Lalamove智能告警降噪
人工智能·后端·监控
wei20234 小时前
汽车智能体Agent:国务院“人工智能+”行动意见 对汽车智能体领域 革命性重塑
人工智能·汽车·agent·智能体
LinkTime_Cloud4 小时前
快手遭遇T0级“黑色闪电”:一场教科书式的“协同打击”,披上了AI“智能外衣”的攻击
人工智能
PPIO派欧云4 小时前
PPIO上线MiniMax-M2.1:聚焦多语言编程与真实世界复杂任务
人工智能
隔壁阿布都4 小时前
使用LangChain4j +Springboot 实现大模型与向量化数据库协同回答
人工智能·spring boot·后端
Coding茶水间4 小时前
基于深度学习的水面垃圾检测系统演示与介绍(YOLOv12/v11/v8/v5模型+Pyqt5界面+训练代码+数据集)
图像处理·人工智能·深度学习·yolo·目标检测·机器学习·计算机视觉
乐迪信息5 小时前
乐迪信息:煤矿皮带区域安全管控:人员违规闯入智能识别
大数据·运维·人工智能·物联网·安全