当前位置: 首页 > news >正文

OpenCV 位姿与投影变换

1. 旋转矩阵与旋转向量的相互转换

1.1. 罗德里格斯公式

符号^是向量到反对称的转换符。反之,我们也可以计算从一个旋转矩阵到旋转向量的转换。对于转角θ,有:

因此:

关于转轴n,由于旋转轴上的向量在旋转后不发生改变,说明

转轴 n 是矩阵 R 特征值 1 对应的特征向量。求解此方程,再归一化,就得到了旋转轴。

1.2. 转换函数

void Rodrigues(const cv::Mat& src,cv::Mat dst,cv::Mat jacobian=0);

参数说明

  • src——为输入的旋转向量(3x1或者1x3)或者旋转矩阵(3x3)。该参数向量表示其旋转的角度,用向量长度表示。
  • dst——为输出的旋转矩阵(3x3)或者旋转向量(3x1或者1x3)。
  • jacobian——为可选的输出雅可比矩阵(3x9或者9x3),是输入与输出数组的偏导数。

2. 图像变换

2.1. 去畸变

2.1.1. 针孔相机

调用方法

std::vector<cv::Point2f> inputDistortedPoints = ... std::vector<cv::Point2f> outputUndistortedPoints; cv::Mat cameraMatrix = ... cv::Mat distCoeffs = ... cv::undistortPoints(inputDistortedPoints, outputUndistortedPoints, cameraMatrix, distCoeffs, cv::noArray(), cameraMatrix);

不要像下面这样调用,输出的点不正确

cv::undistortPoints(inputDistortedPoints, outputUndistortedPoints, cameraMatrix, distCoeffs)

2.1.2. 鱼眼相机

cv::fisheye::undistortPoints(inputDistortedPoints, outputUndistortedPoints, cameraMatrix, distCoeffs, cv::noArray(), cameraMatrix);

2.2. 投影

2.2.1. 针孔相机

函数

void cv::projectPoints(InputArray objectPoints, InputArray rvec, InputArray tvec, InputArray cameraMatrix, InputArray distCoeffs, OutputArray imagePoints, OutputArray jacobian = noArray(), double aspectRatio = 0)

Parameters

objectPointsArray of object points expressed wrt. the world coordinate frame. A 3xN/Nx3 1-channel or 1xN/Nx1 3-channel (or vector<Point3f>), where N is the number of points in the view.
rvecThe rotation vector (Rodrigues) that, together with tvec, performs a change of basis from world to camera coordinate system, see calibrateCamera for details.
tvecThe translation vector, see parameter description above.
cameraMatrixCamera intrinsic matrix
distCoeffsInput vector of distortion coefficients (k1,k2,p1,p2[,k3[,k4,k5,k6[,s1,s2,s3,s4[,τx,τy]]]]) of 4, 5, 8, 12 or 14 elements . If the vector is empty, the zero distortion coefficients are assumed.
imagePointsOutput array of image points, 1xN/Nx1 2-channel, or vector<Point2f> .
jacobianOptional output 2Nx(10+<numDistCoeffs>) jacobian matrix of derivatives of image points with respect to components of the rotation vector, translation vector, focal lengths, coordinates of the principal point and the distortion coefficients. In the old interface different components of the jacobian are returned via different output parameters.
aspectRatioOptional "fixed aspect ratio" parameter. If the parameter is not 0, the function assumes that the aspect ratio (fx/fy) is fixed and correspondingly adjusts the jacobian matrix.

示例

#include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/calib3d/calib3d.hpp" #include "opencv2/highgui/highgui.hpp" #include <iostream> #include <string> using namespace std; vector<cv::Point3f> Generate3DPoints(); int main(int argc, char* argv[]) { // Read 3D points vector<cv::Point3f> objectPoints = Generate3DPoints(); vector<cv::Point2f> imagePoints; cv::Mat intrisicMat(3, 3, cv::DataType<float>::type); // Intrisic matrix intrisicMat.at<float>(0, 0) = 1.6415318549788924e+003; intrisicMat.at<float>(1, 0) = 0; intrisicMat.at<float>(2, 0) = 0; intrisicMat.at<float>(0, 1) = 0; intrisicMat.at<float>(1, 1) = 1.7067753507885654e+003; intrisicMat.at<float>(2, 1) = 0; intrisicMat.at<float>(0, 2) = 5.3262822453148601e+002; intrisicMat.at<float>(1, 2) = 3.8095355839052968e+002; intrisicMat.at<float>(2, 2) = 1; cv::Mat rVec(3, 1, cv::DataType<float>::type); // Rotation vector rVec.at<float>(0) = -3.9277902400761393e-002; rVec.at<float>(1) = 3.7803824407602084e-002; rVec.at<float>(2) = 2.6445674487856268e-002; cv::Mat tVec(3, 1, cv::DataType<float>::type); // Translation vector tVec.at<float>(0) = 2.1158489381208221e+000; tVec.at<float>(1) = -7.6847683212704716e+000; tVec.at<float>(2) = 2.6169795190294256e+001; cv::Mat distCoeffs(5, 1, cv::DataType<float>::type); // Distortion vector distCoeffs.at<float>(0) = -7.9134632415085826e-001; distCoeffs.at<float>(1) = 1.5623584435644169e+000; distCoeffs.at<float>(2) = -3.3916502741726508e-002; distCoeffs.at<float>(3) = -1.3921577146136694e-002; distCoeffs.at<float>(4) = 1.1430734623697941e-002; cout << "Intrisic matrix: " << intrisicMat << endl << endl; cout << "Rotation vector: " << rVec << endl << endl; cout << "Translation vector: " << tVec << endl << endl; cout << "Distortion coef: " << distCoeffs << endl << endl; std::vector<cv::Point2f> projectedPoints; cv::projectPoints(objectPoints, rVec, tVec, intrisicMat, distCoeffs, projectedPoints); cout << "Press any key to exit."; cin.ignore(); cin.get(); return 0; } vector<cv::Point3f> Generate3DPoints() { vector<cv::Point3f> points; points.push_back(cv::Point3f(.5, .5, -.5)); points.push_back(cv::Point3f(.5, .5, .5)); points.push_back(cv::Point3f(-.5, .5, .5)); points.push_back(cv::Point3f(-.5, .5, -.5)); points.push_back(cv::Point3f(.5, -.5, -.5)); points.push_back(cv::Point3f(-.5, -.5, -.5)); points.push_back(cv::Point3f(-.5, -.5, .5)); for(unsigned int i = 0; i < points.size(); ++i) { cout << points[i] << endl << endl; } return points; }

2.2.2. 鱼眼相机

函数

// 提供了两个重载函数,经验上第一个容易崩溃,建议使用第二个 void cv::fisheye::projectPoints(InputArray objectPoints, OutputArray imagePoints, InputArray rvec, InputArray tvec, InputArray K, InputArray D, double alpha = 0, OutputArray jacobian = noArray()) void cv::fisheye::projectPoints(InputArray objectPoints, OutputArray imagePoints, const Affine3d& affine, InputArray K, InputArray D, double alpha = 0, OutputArray jacobian = noArray())

Parameters

objectPointsArray of object points, 1xN/Nx1 3-channel (or vector<Point3f>), where N is the number of points in the view.
imagePointsOutput array of image points, 2xN/Nx2 1-channel or 1xN/Nx1 2-channel, or vector<Point2f>.
affine仿射变换,可以使用位姿矩阵进行初始化
KCamera intrinsic matrix cameramatrixK.
DInput vector of distortion coefficients (k1,k2,k3,k4).
alphaThe skew coefficient.
jacobianOptional output 2Nx15 jacobian matrix of derivatives of image points with respect to components of the focal lengths, coordinates of the principal point, distortion coefficients, rotation vector, translation vector, and the skew. In the old interface different components of the jacobian are returned via different output parameters.

参考文献

OpenCV的projectPoints函数用法_JIN_嫣熙的博客-CSDN博客_projectpoints

Opencv学习(12)——cv::Rodrigues()函数_令狐少侠、的博客-CSDN博客_rodrigues函数

OpenCV: Camera Calibration and 3D Reconstruction

OpenCV: Fisheye camera model

undistortPoints opencv_cvml的博客-CSDN博客

http://www.jsqmd.com/news/1160912/

相关文章:

  • ABAP 里没有 @SpringBootApplication,但有一套更 SAP 化的应用装配方式
  • 如何让ChatGPT写出导师认可的引言段?——5分钟重构Prompt结构,实现学术语义精准对齐(附CiteSpace验证图谱)
  • 劳力士官方售后服务中心地址电话一览实地考察报告+多信源验证(2026年7月更新) - 劳力士服务中心
  • 2026车窗玻璃贴膜遮阳膜选购:隔热太阳安全膜品牌厂家主机厂原厂膜供应商推荐 - 栗子测评
  • 正点原子ZYNQ7010/7020核心板选型指南:85K vs 28K逻辑单元与3大应用场景解析
  • 从bulk到空间蛋白组:CAF分类的组织原位观察思路
  • RimSort终极教程:3步彻底解决环世界模组冲突问题
  • CAF空间原型与肿瘤微环境观察:七类基质生态位的研究启发
  • 2026年7月大连正规救护车转运费用_高性价比服务 - 小校长
  • 苏州卫生间漏水不砸砖能修好吗?真相不是你想的那样 - 徽顺虹
  • 网盘下载慢的终极解决方案:本地直链解析工具完全指南
  • Markdown Viewer插件:从零开始打造你的完美文档阅读体验
  • 2026汽车改色膜哪家好/漆面保护膜哪家靠谱/车衣厂家品牌推荐汇总 - 栗子测评
  • Minecraft世界优化终极指南:使用MCA Selector进行专业级区块管理
  • OpenCV 使用问题汇总
  • 2026年7月最新重庆浪琴官方售后服务网点地址及客服电话一览 - 浪琴官方售后服务中心
  • 3步搞定GitHub精准下载:DownGit终极指南
  • 3分钟解锁Windows 11 LTSC完整应用生态:一键安装微软商店的智能解决方案
  • 五年施工经验:地坪漆的真实表现 - GrowthUME
  • 终极文档下载神器:kill-doc 浏览器脚本让30+平台文档一键免费保存
  • 3步将闲置电视盒变身高性能服务器:Armbian系统改造实战指南
  • UE5.2离线安装与项目重构实战:从环境部署到问题排查
  • 告别混乱照片库:ExifToolGUI让照片元数据管理变得简单高效
  • 告别手写Props和useEffect:Cursor AI生成高可维护React组件的6条军规(含ESLint+Prettier联动配置)
  • 百达翡丽中国官方售后服务中心|地址及官方客服服务电话权威信息声明(2026年7月更新) - 百达翡丽服务中心
  • AI获客培训效果对比:国内头部机构选型核心要点分析 - GrowthUME
  • 2026浙江品牌咨询营销公司有哪些?靠谱营销策划战略咨询服务商推荐 - 栗子测评
  • LangChain Memory 泄漏导致LLM响应延迟飙升300%?这份内存生命周期监控SOP已被8家头部AIGC团队内部采用
  • Go语言开发环境搭建
  • 【绝密】LangChain Memory 底层Hook机制逆向解析:如何绕过默认序列化劫持会话状态——仅限前500名开发者获取调试工具链