OpenCV中是否存在基于鱼眼镜头参数计算射线方向的函数?
鱼眼镜头下计算3D射线方向的OpenCV解决方案
OpenCV提供了专门适配鱼眼镜头的函数来完成这个需求,无需自行实现畸变逆运算:
核心思路
要从鱼眼相机的像素点得到相机坐标系下的3D射线方向,只需两步:
- 用
fisheye::undistortPoints将畸变的像素坐标转换为归一化相机平面坐标(对应Z=1平面上的点); - 基于归一化坐标构造射线方向向量,按需归一化为单位向量即可。
Python代码示例
import cv2 import numpy as np # 鱼眼相机内参矩阵(K)和畸变系数(D) K = np.array([[500, 0, 320], [0, 500, 240], [0, 0, 1]], dtype=np.float32) D = np.array([0.1, -0.05, 0.02, 0.01], dtype=np.float32) # 目标像素点 pixel = np.array([[300, 220]], dtype=np.float32) # 去畸变得到归一化平面坐标 undistorted_points = cv2.fisheye.undistortPoints(pixel, K, D) # 构造射线方向并归一化 ray_dir = np.array([undistorted_points[0][0][0], undistorted_points[0][0][1], 1.0]) ray_dir /= np.linalg.norm(ray_dir) print("单位射线方向:", ray_dir)
C++代码示例
#include <opencv2/opencv.hpp> #include <opencv2/fisheye.hpp> #include <iostream> int main() { // 鱼眼相机内参与畸变系数 cv::Mat K = (cv::Mat_<float>(3,3) << 500, 0, 320, 0, 500, 240, 0, 0, 1); cv::Mat D = (cv::Mat_<float>(1,4) << 0.1, -0.05, 0.02, 0.01); // 输入像素点 cv::Mat pixel = (cv::Mat_<float>(1,2) << 300, 220); cv::Mat undistorted; // 执行鱼眼去畸变,得到归一化坐标 cv::fisheye::undistortPoints(pixel, undistorted, K, D); // 构造并归一化射线方向向量 cv::Vec3f ray_dir(undistorted.at<float>(0,0), undistorted.at<float>(0,1), 1.0f); ray_dir /= cv::norm(ray_dir); std::cout << "单位射线方向:" << ray_dir << std::endl; return 0; }
说明
fisheye::undistortPoints内置了OpenCV采用的Kannala-Brandt鱼眼畸变模型的逆运算,完全覆盖了从畸变像素到归一化相机坐标的转换逻辑,直接调用即可避免重复造轮子。
内容的提问来源于stack exchange,提问作者Chanoh Park
相关产品推荐
相关产品推荐

