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

相关文章

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

Web服务器-Nginx-高并发问题

《Web服务器-Nginx-高并发问题》Nginx通过事件驱动、I/O多路复用和异步非阻塞技术高效处理高并发,结合动静分离和限流策略,提升性能与稳定性... 目录前言一、架构1. 原生多进程架构2. 事件驱动模型3. IO多路复用4. 异步非阻塞 I/O5. Nginx高并发配置实战二、动静分离1. 职责2

解决升级JDK报错:module java.base does not“opens java.lang.reflect“to unnamed module问题

《解决升级JDK报错:modulejava.basedoesnot“opensjava.lang.reflect“tounnamedmodule问题》SpringBoot启动错误源于Jav... 目录问题描述原因分析解决方案总结问题描述启动sprintboot时报以下错误原因分析编程异js常是由Ja

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec

SysMain服务可以关吗? 解决SysMain服务导致的高CPU使用率问题

《SysMain服务可以关吗?解决SysMain服务导致的高CPU使用率问题》SysMain服务是超级预读取,该服务会记录您打开应用程序的模式,并预先将它们加载到内存中以节省时间,但它可能占用大量... 在使用电脑的过程中,CPU使用率居高不下是许多用户都遇到过的问题,其中名为SysMain的服务往往是罪魁

MySQ中出现幻读问题的解决过程

《MySQ中出现幻读问题的解决过程》文章解析MySQLInnoDB通过MVCC与间隙锁机制在可重复读隔离级别下解决幻读,确保事务一致性,同时指出性能影响及乐观锁等替代方案,帮助开发者优化数据库应用... 目录一、幻读的准确定义与核心特征幻读 vs 不可重复读二、mysql隔离级别深度解析各隔离级别的实现差异

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

Python多线程应用中的卡死问题优化方案指南

《Python多线程应用中的卡死问题优化方案指南》在利用Python语言开发某查询软件时,遇到了点击搜索按钮后软件卡死的问题,本文将简单分析一下出现的原因以及对应的优化方案,希望对大家有所帮助... 目录问题描述优化方案1. 网络请求优化2. 多线程架构优化3. 全局异常处理4. 配置管理优化优化效果1.