3D视觉(六):PnP问题(pespective-n-point)

2024-04-28 18:32

本文主要是介绍3D视觉(六):PnP问题(pespective-n-point),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

3D视觉(六):PnP问题(pespective-n-point)

PnP问题,是指已知3D点(x, y, z)及其在相机上的投影(u,v),求解相机位姿变换R、T。
投影方程可表示为:
在这里插入图片描述这里K为相机内参矩阵,是已知的。我们要做的就是,从n对这样的2D-3D对应关系中,恢复出相机姿态变换,即旋转矩阵R和平移向量t。

文章目录

  • 3D视觉(六):PnP问题(pespective-n-point)
  • 一、算法原理
  • 二、实验过程
  • 三、源码
  • 四、项目链接

一、算法原理

典型的PnP问题求解方式有很多种,例如P3P、直接线性变换DLT、EPnP、UPnP,另外还有非线性的Bundle Adjustment。下面简单推导一下直接线性变换DLT的原理。

考虑某个空间点P,它的齐次坐标为P=(X, Y, Z, 1).T,投影到图像中得到特征点x1=(u1, v1, 1).T。我们定义增广矩阵 [R|t] 为一个3*4矩阵,模型的数学表达式为:

在这里插入图片描述
用最后一行把s消去,得到两个约束:

在这里插入图片描述
为简化表示,定义T的行向量:

在这里插入图片描述
则上面两个约束可以转化成矩阵形式:

在这里插入图片描述
可以看到,每个特征点能提供两个关于旋转平移矩阵T的线性约束。假设一共拥有N个特征点,则可列出如下线性方程组:

在这里插入图片描述
旋转平移矩阵T一共有12维,因此最少通过6对匹配点即可实现矩阵T的线性求解,这种方法称为DLT。当匹配点大于6对时,也可以使用SVD等方法对超定方程求最小二乘解。

二、实验过程

利用人脸关键点2D图像坐标,和3D人脸模板关键点坐标,求解头部姿态。

人脸2D关键点图像坐标如下:

在这里插入图片描述
在这里插入图片描述
3D人脸模板关键点的3D坐标如下:

在这里插入图片描述
利用cv::solvePnP函数,求解位姿变换结果:

在这里插入图片描述
头部姿态可视化效果如下:

在这里插入图片描述

三、源码

#include <opencv2/opencv.hpp>using namespace std;
using namespace cv;// reference: https://learnopencv.com/head-pose-estimation-using-opencv-and-dlib/int main(int argc, char **argv)
{// Read input imagecv::Mat im = cv::imread("../headPose.jpg");cout << "img cols and rows: " << im.cols << "  " << im.rows << endl;// 2D image points coordinate. If you change the image, you need to change vectorstd::vector<cv::Point2d> image_points;image_points.push_back( cv::Point2d(359, 391) );    // Nose tipimage_points.push_back( cv::Point2d(399, 561) );    // Chinimage_points.push_back( cv::Point2d(337, 297) );    // Left eye left cornerimage_points.push_back( cv::Point2d(513, 301) );    // Right eye right cornerimage_points.push_back( cv::Point2d(345, 465) );    // Left Mouth cornerimage_points.push_back( cv::Point2d(453, 469) );    // Right mouth corner// 3D model points coordinate.std::vector<cv::Point3d> model_points;model_points.push_back(cv::Point3d(0.0f, 0.0f, 0.0f));               // Nose tipmodel_points.push_back(cv::Point3d(0.0f, -330.0f, -65.0f));          // Chinmodel_points.push_back(cv::Point3d(-225.0f, 170.0f, -135.0f));       // Left eye left cornermodel_points.push_back(cv::Point3d(225.0f, 170.0f, -135.0f));        // Right eye right cornermodel_points.push_back(cv::Point3d(-150.0f, -150.0f, -125.0f));      // Left Mouth cornermodel_points.push_back(cv::Point3d(150.0f, -150.0f, -125.0f));       // Right mouth corner// Camera internals parameter matrix.// Approximate focal length.// Assuming no lens distortion.double focal_length = im.cols; Point2d center = cv::Point2d(im.cols/2, im.rows/2);cv::Mat camera_matrix = (cv::Mat_<double>(3,3) << focal_length, 0, center.x, 0 , focal_length, center.y, 0, 0, 1);cv::Mat dist_coeffs = cv::Mat::zeros(4,1,cv::DataType<double>::type); cout << endl << "Approximate Camera Matrix: " << endl << camera_matrix << endl;cout << endl << "Approximate Distort Coeffs: " << endl << dist_coeffs.t() << endl << endl;// Output rotation and translation, Rotation is in axis-angle form and matrix form.cv::Mat rotation_vector; cv::Mat rotation_matrix; cv::Mat translation_vector;// Solve for pose.// The output result of cv::solvepnp function is a rotation vector, which needs to be converted into a matrix by Rodrigues formula.cv::solvePnP(model_points, image_points, camera_matrix, dist_coeffs, rotation_vector, translation_vector);cv::Rodrigues(rotation_vector, rotation_matrix);cout << "Rotation Vector: " << endl << rotation_vector << endl << endl;cout << "Rotation Matrix: " << endl << rotation_matrix << endl << endl;cout << "Translation Vector:" << endl << translation_vector << endl << endl;// Project a 3D point (0, 0, 1000.0) onto the image plane, we use this to draw a line sticking out of the nose.vector<Point3d> nose_end_point3D;vector<Point2d> nose_end_point2D;nose_end_point3D.push_back(Point3d(0,0,1000.0));projectPoints(nose_end_point3D, rotation_vector, translation_vector, camera_matrix, dist_coeffs, nose_end_point2D);cout << "project results: " << nose_end_point2D << endl << endl;// Draw landmark points and projecting linefor(int i=0; i < image_points.size(); i++){circle(im, image_points[i], 3, Scalar(0, 255, 255), -1);}cv::line(im,image_points[0], nose_end_point2D[0], cv::Scalar(0, 0, 255), 3);// Display image.cv::imshow("im", im);cv::waitKey(0);cv::imwrite("../result.png", im);}

四、项目链接

如果代码跑不通,或者想直接使用数据集,可以去下载项目链接:
https://blog.csdn.net/Twilight737

这篇关于3D视觉(六):PnP问题(pespective-n-point)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/943951

相关文章

IDEA和GIT关于文件中LF和CRLF问题及解决

《IDEA和GIT关于文件中LF和CRLF问题及解决》文章总结:因IDEA默认使用CRLF换行符导致Shell脚本在Linux运行报错,需在编辑器和Git中统一为LF,通过调整Git的core.aut... 目录问题描述问题思考解决过程总结问题描述项目软件安装shell脚本上git仓库管理,但拉取后,上l

idea npm install很慢问题及解决(nodejs)

《ideanpminstall很慢问题及解决(nodejs)》npm安装速度慢可通过配置国内镜像源(如淘宝)、清理缓存及切换工具解决,建议设置全局镜像(npmconfigsetregistryht... 目录idea npm install很慢(nodejs)配置国内镜像源清理缓存总结idea npm in

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

idea突然报错Malformed \uxxxx encoding问题及解决

《idea突然报错Malformeduxxxxencoding问题及解决》Maven项目在切换Git分支时报错,提示project元素为描述符根元素,解决方法:删除Maven仓库中的resolv... 目www.chinasem.cn录问题解决方式总结问题idea 上的 maven China编程项目突然报错,是

Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题

《Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题》在爬虫工程里,“HTTPS”是绕不开的话题,HTTPS为传输加密提供保护,同时也给爬虫带来证书校验、... 目录一、核心问题与优先级检查(先问三件事)二、基础示例:requests 与证书处理三、高并发选型:

前端导出Excel文件出现乱码或文件损坏问题的解决办法

《前端导出Excel文件出现乱码或文件损坏问题的解决办法》在现代网页应用程序中,前端有时需要与后端进行数据交互,包括下载文件,:本文主要介绍前端导出Excel文件出现乱码或文件损坏问题的解决办法,... 目录1. 检查后端返回的数据格式2. 前端正确处理二进制数据方案 1:直接下载(推荐)方案 2:手动构造

Python绘制TSP、VRP问题求解结果图全过程

《Python绘制TSP、VRP问题求解结果图全过程》本文介绍用Python绘制TSP和VRP问题的静态与动态结果图,静态图展示路径,动态图通过matplotlib.animation模块实现动画效果... 目录一、静态图二、动态图总结【代码】python绘制TSP、VRP问题求解结果图(包含静态图与动态图

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

k8s容器放开锁内存限制问题

《k8s容器放开锁内存限制问题》nccl-test容器运行mpirun时因NCCL_BUFFSIZE过大导致OOM,需通过修改docker服务配置文件,将LimitMEMLOCK设为infinity并... 目录问题问题确认放开容器max locked memory限制总结参考:https://Access

Java中字符编码问题的解决方法详解

《Java中字符编码问题的解决方法详解》在日常Java开发中,字符编码问题是一个非常常见却又特别容易踩坑的地方,这篇文章就带你一步一步看清楚字符编码的来龙去脉,并结合可运行的代码,看看如何在Java项... 目录前言背景:为什么会出现编码问题常见场景分析控制台输出乱码文件读写乱码数据库存取乱码解决方案统一使