# Author: 两只程序猿
# Date: 2023/06/24
# Description: 将鱼眼图像转换为指定视角角度的针孔透视图像
import cv2
import glob
import yaml
import numpy as np
def _mat3d_rotate(mat:np.ndarray, rot:np.ndarray):
"""将每个像素对应的3D点做空间旋转
Args:
mat (np.ndarray): float64 array with [h x w x 3]
rot (np.ndarray): float64 array with [3 x 3]
"""
# faster when using @ operation
return np.transpose(rot @ np.transpose(mat.reshape(-1, 3), axes=(1, 0)), axes=(1, 0)).reshape(*mat.shape)
# 载入鱼眼相机的内参和畸变参数(这里的畸变参数是cv2.fisheye校准出的畸变参数)
conf = yaml.load(open('480P.yaml'), Loader=yaml.FullLoader)
cam = conf['cam0']
K = np.array([[cam['intrinsic']['fx'], 0, cam['intrinsic']['cx']],
[0, cam['intrinsic']['fy'], cam['intrinsic']['cy']],
[0, 0, 1]])
D = np.array([[cam['distortion']['k1']], [cam['distortion']['k2']], [cam['distortion']['k3']], [cam['distortion']['k4']]])
print(K)
print(D)
# 配置透视图的参数(虚拟针孔相机)
angle_phi = 30 # 俯仰角
angle_theta = 0 # 偏航角
perspective_img_h = 480 # 透视图的高度
perspective_img_w = 640 # 透视图的宽度
perspective_fx = 415.692 # 透视图的横轴焦距(内参)
perspective_fy = 415.692 # 透视图的纵轴焦距(内参)
perspective_FOV_x = 2 * np.arctan(perspective_img_w / (2 * perspective_fx)) * 180 / np.pi # 透视图的横轴视场角
perspective_FOV_y = 2 * np.arctan(perspective_img_h / (2 * perspective_fy)) * 180 / np.pi # 透视图的纵轴视场角
phi = np.radians(angle_phi)
theta = np.radians(angle_theta)
################################
## 计算透视图-鱼眼图像素映射关系
################################
# 每个像素对应的归一化的空间3D坐标
X = (((np.tile(np.arange(0, perspective_img_w), (perspective_img_h, 1))) / perspective_img_w)*2 - 1) * np.tan(np.radians(perspective_FOV_x) * 0.5)
Y = (((np.tile(np.arange(0, perspective_img_h), (perspective_img_w, 1)).T) / perspective_img_h)*2 - 1) * np.tan(np.radians(perspective_FOV_y) * 0.5)
Z = np.ones((perspective_img_h, perspective_img_w), np.float64)
XYZ = cv2.merge([X, Y, Z])
# 旋转矩阵
Rx = np.array([[1, 0, 0],
[0, np.cos(phi), -np.sin(phi)],
[0, np.sin(phi), np.cos(phi)]], dtype=np.float64)
Ry = np.array([[np.cos(-theta), 0, np.sin(-theta)],
[0, 1, 0],
[-np.sin(-theta), 0, np.cos(-theta)]], dtype=np.float64)
R_combine = Ry @ Rx
# 旋转到鱼眼相机视角下的3D坐标
XYZ_rotated = _mat3d_rotate(XYZ, R_combine)
XYZ_rotated = XYZ_rotated/XYZ_rotated[...,2:3]
# 计算鱼眼图像的畸变
r = np.sqrt(XYZ_rotated[..., 0]**2 + XYZ_rotated[..., 1]**2)
theta = np.arctan(r)
theta2 = theta**2
theta4 = theta2**2
theta6 = theta4*theta2
theta8 = theta4*theta4
theta_d = theta * (1 + D[0]*theta2 + D[1]*theta4 + D[2]*theta6 + D[3]*theta8)
scale = theta_d / r
# 计算透视图到鱼眼图像的映射关系
map1 = (XYZ_rotated[..., 0] * scale * K[0, 0] + K[0, 2]).astype(np.float32)
map2 = (XYZ_rotated[..., 1] * scale * K[1, 1] + K[1, 2]).astype(np.float32)
################################
## 读取鱼眼图像并转换为透视图
################################
images = glob.glob('calib_imgs_480p_9x6/*.jpg')
for image_path in images:
img = cv2.imread(image_path)
h, w = img.shape[:2]
persp_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR)
cv2.imshow('original', img)
cv2.imshow('persp', persp_img)
key = cv2.waitKey(0)
if key == ord('q'):
break
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
# 鱼眼图像转换为任意视角针孔相机图像Python代码 1. 通过鱼眼相机图像和鱼眼相机的内参和畸变参数; 2. 配置仰角和偏角,将鱼眼图像转换到该视角下的针孔相机图像。
资源推荐
资源详情
资源评论
收起资源包目录
鱼眼图像转换为任意视角针孔相机图像Python代码.zip (53个子文件)
fish2persp
480P.yaml 269B
calib_imgs_480p_9x6
frame_8.jpg 85KB
frame_12.jpg 89KB
frame_7.jpg 79KB
frame_35.jpg 86KB
frame_25.jpg 91KB
frame_34.jpg 93KB
frame_39.jpg 82KB
frame_33.jpg 89KB
frame_11.jpg 94KB
frame_23.jpg 80KB
frame_5.jpg 84KB
frame_29.jpg 88KB
frame_37.jpg 90KB
frame_45.jpg 86KB
frame_32.jpg 83KB
frame_40.jpg 86KB
frame_13.jpg 89KB
frame_30.jpg 86KB
frame_3.jpg 93KB
frame_41.jpg 79KB
frame_24.jpg 88KB
frame_21.jpg 85KB
frame_38.jpg 84KB
frame_47.jpg 80KB
frame_20.jpg 90KB
frame_4.jpg 94KB
frame_26.jpg 92KB
frame_46.jpg 85KB
frame_48.jpg 77KB
frame_6.jpg 81KB
frame_31.jpg 84KB
frame_16.jpg 102KB
frame_10.jpg 95KB
frame_18.jpg 95KB
frame_36.jpg 83KB
frame_50.jpg 91KB
frame_43.jpg 84KB
frame_9.jpg 91KB
frame_42.jpg 81KB
frame_49.jpg 89KB
frame_22.jpg 79KB
frame_28.jpg 88KB
frame_2.jpg 94KB
frame_14.jpg 87KB
frame_44.jpg 86KB
frame_19.jpg 95KB
frame_1.jpg 89KB
frame_15.jpg 75KB
frame_17.jpg 95KB
frame_27.jpg 90KB
frame_0.jpg 91KB
fish2persp.py 4KB
共 53 条
- 1
两只程序猿
- 粉丝: 338
- 资源: 158
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页